Clover coverage report - Diasparsoft Toolkit - 0.22
Coverage timestamp: Tue Jun 8 2004 12:41:26 EDT
file stats: LOC: 155   Methods: 4
NCLOC: 86   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
StreamUtil.java 100% 100% 100% 100%
coverage
 1   
 /*
 2   
  *  Copyright (C) 1998, 2003 Gargoyle Software. All rights reserved.
 3   
  *
 4   
  *  This file is part of GSBase. For details on use and redistribution
 5   
  *  please refer to the license.html file included with these sources.
 6   
  */
 7   
 package com.diasparsoftware.gsbase;
 8   
 
 9   
 import java.io.*;
 10   
 import java.util.*;
 11   
 
 12   
 /**
 13   
  * Provides some utility methods for working with streams.
 14   
  * 
 15   
  * @version $Revision: 1.1 $
 16   
  * @author <a href="mailto:jbr@diasparsoftware.com">J. B. Rainsberger</a>
 17   
  */
 18   
 public class StreamUtil {
 19   
 
 20   
     /**
 21   
      * Reads the content coming from the specified input stream
 22   
      * and presents it as a string.
 23   
      * 
 24   
      * @param stream  Any input stream containing text
 25   
      * @return  A string representing the contents of the
 26   
      * underlying stream
 27   
      * @throws IOException  Thrown by the underlying I/O operations
 28   
      */
 29  198
     public static String getContentAsString(InputStream characterStream)
 30   
         throws IOException {
 31   
 
 32  198
         Assert.notNull("characterStream", characterStream);
 33   
 
 34  187
         BufferedReader bufferedReader =
 35   
             new BufferedReader(new InputStreamReader(characterStream));
 36   
 
 37  187
         StringWriter stringWriter = new StringWriter();
 38  187
         while (true) {
 39  17743
             int character = bufferedReader.read();
 40  17743
             if (character == -1)
 41  187
                 break;
 42   
 
 43  17556
             stringWriter.write(character);
 44   
         }
 45   
 
 46  187
         return stringWriter.toString();
 47   
     }
 48   
 
 49   
     /**
 50   
      * Reads the content coming from the specified input stream
 51   
      * and presents it as an array of bytes.
 52   
      * 
 53   
      * @param stream  Any input stream
 54   
      * @return  An array of bytes representing the contents of the
 55   
      * underlying stream
 56   
      * @throws IOException  Thrown by the underlying I/O operations
 57   
      */
 58  88
     public static byte[] getContentAsBytes(InputStream byteStream)
 59   
         throws IOException {
 60   
 
 61  88
         Assert.notNull("byteStream", byteStream);
 62   
 
 63  77
         DataInputStream dataInputStream =
 64   
             new DataInputStream(byteStream);
 65   
 
 66  77
         List segments = new ArrayList();
 67   
 
 68  77
         boolean endOfStream = false;
 69  77
         int segmentIndex = 0;
 70  77
         final int segmentLength = 4096;
 71  77
         int lastSegmentLength = 0;
 72  77
         while (!endOfStream) {
 73  2915
             int offset = segmentIndex * segmentLength;
 74  2915
             byte[] buffer = new byte[segmentLength];
 75   
 
 76  2915
             int thisSegmentLength =
 77   
                 dataInputStream.read(buffer, offset, segmentLength);
 78   
 
 79  2915
             if (thisSegmentLength == -1) {
 80  77
                 endOfStream = true;
 81   
             }
 82   
             else {
 83  2838
                 segments.add(buffer);
 84  2838
                 lastSegmentLength = thisSegmentLength;
 85   
             }
 86   
         }
 87   
 
 88  77
         return flattenByteSegments(
 89   
             segments,
 90   
             segmentLength,
 91   
             lastSegmentLength);
 92   
     }
 93   
 
 94   
     /**
 95   
      * Turns a List of equally-sized byte segments (buffers) 
 96   
      * into a single buffer
 97   
      * by concatenating them one by one.
 98   
      * 
 99   
      * @param byteSegments  The buffers to "flatten"
 100   
      * @param eachSegmentButLastLength  The length of each buffer
 101   
      * but the last -- they are assumed all to be the same size
 102   
      * @param lastSegmentLength  The length of the last buffer,
 103   
      * since it is likely not the same length as the others
 104   
      * @return
 105   
      */
 106  132
     public static byte[] flattenByteSegments(
 107   
         List byteSegments,
 108   
         int eachSegmentButLastLength,
 109   
         int lastSegmentLength) {
 110   
 
 111  132
         if (byteSegments.isEmpty())
 112  44
             return new byte[0];
 113   
         else {
 114  88
             int totalBytes =
 115   
                 eachSegmentButLastLength * (byteSegments.size() - 1)
 116   
                     + lastSegmentLength;
 117   
 
 118  88
             byte[] flattenedBytes = new byte[totalBytes];
 119   
 
 120  88
             int nextSegmentOffset = 0;
 121   
 
 122  88
             for (int i = 0; i < byteSegments.size() - 1; i++) {
 123  2827
                 System.arraycopy(
 124   
                     byteSegments.get(i),
 125   
                     0,
 126   
                     flattenedBytes,
 127   
                     nextSegmentOffset,
 128   
                     eachSegmentButLastLength);
 129   
 
 130  2827
                 nextSegmentOffset += eachSegmentButLastLength;
 131   
             }
 132   
 
 133  88
             System.arraycopy(
 134   
                 truncateByteSegment(
 135   
                     (byte[]) byteSegments.get(byteSegments.size() - 1),
 136   
                     lastSegmentLength),
 137   
                 0,
 138   
                 flattenedBytes,
 139   
                 nextSegmentOffset,
 140   
                 lastSegmentLength);
 141   
 
 142  88
             return flattenedBytes;
 143   
         }
 144   
     }
 145   
 
 146  88
     private static byte[] truncateByteSegment(
 147   
         byte[] byteSegment,
 148   
         int toSize) {
 149   
 
 150  88
         byte[] newSegment = new byte[toSize];
 151  88
         System.arraycopy(byteSegment, 0, newSegment, 0, toSize);
 152  88
         return newSegment;
 153   
     }
 154   
 }
 155