Coverage Report - org.kuali.rice.core.api.impex.xml.StreamXmlDoc
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamXmlDoc
0%
0/22
0%
0/6
1.667
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.api.impex.xml;
 17  
 
 18  
 import java.io.ByteArrayInputStream;
 19  
 import java.io.ByteArrayOutputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.io.OutputStream;
 23  
 
 24  
 /**
 25  
  * Stream-based XML doc.
 26  
  * 
 27  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 28  
  */
 29  0
 public class StreamXmlDoc extends BaseXmlDoc {
 30  
         private byte[] bytes;
 31  
         private InputStream stream;
 32  
 
 33  
         public StreamXmlDoc(InputStream stream, XmlDocCollection collection) {
 34  0
                 super(collection);
 35  0
                 this.stream = stream;
 36  0
         }
 37  
 
 38  
         public synchronized InputStream getStream() throws IOException {
 39  
                 // this sucks, but does it suck less than copying the file?
 40  
                 // or dealing with concurrency issues on a surveilling input stream
 41  0
                 if (bytes == null) {
 42  0
                         bytes = StreamXmlDoc.toByteArray(stream);
 43  
                 }
 44  0
                 return new ByteArrayInputStream(bytes);
 45  
         }
 46  
 
 47  
         public String getName() {
 48  0
                 return stream.toString();
 49  
         }
 50  
 
 51  
         /**
 52  
          * The following code copied from commons-io IOUtils to avoid a core-api dependency on that library.
 53  
          */
 54  
         
 55  
         private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 56  
 
 57  
         private static byte[] toByteArray(InputStream input) throws IOException {
 58  0
                 ByteArrayOutputStream output = new ByteArrayOutputStream();
 59  0
                 copy(input, output);
 60  0
                 return output.toByteArray();
 61  
         }
 62  
 
 63  
         private static int copy(InputStream input, OutputStream output)
 64  
                         throws IOException {
 65  0
                 long count = copyLarge(input, output);
 66  0
                 if (count > Integer.MAX_VALUE) {
 67  0
                         return -1;
 68  
                 }
 69  0
                 return (int) count;
 70  
         }
 71  
 
 72  
         public static long copyLarge(InputStream input, OutputStream output)
 73  
                         throws IOException {
 74  0
                 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
 75  0
                 long count = 0;
 76  0
                 int n = 0;
 77  0
                 while (-1 != (n = input.read(buffer))) {
 78  0
                         output.write(buffer, 0, n);
 79  0
                         count += n;
 80  
                 }
 81  0
                 return count;
 82  
         }
 83  
         
 84  
         /**
 85  
          * End code copied from common-io IOUtils
 86  
          */
 87  
 
 88  
 }