Clover coverage report - Diasparsoft Toolkit - 0.22
Coverage timestamp: Mon Jun 7 2004 22:02:31 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  144
     public static String getContentAsString(InputStream characterStream)
 30   
         throws IOException {
 31   
 
 32  144
         Assert.notNull("characterStream", characterStream);
 33   
 
 34  136
         BufferedReader bufferedReader =
 35   
             new BufferedReader(new InputStreamReader(characterStream));
 36   
 
 37  136
         StringWriter stringWriter = new StringWriter();
 38  136
         while (true) {
 39  12904
             int character = bufferedReader.read();
 40  12904
             if (character == -1)
 41  136
                 break;
 42   
 
 43  12768
             stringWriter.write(character);
 44   
         }
 45   
 
 46  136
         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  64
     public static byte[] getContentAsBytes(InputStream byteStream)
 59   
         throws IOException {
 60   
 
 61  64
         Assert.notNull("byteStream", byteStream);
 62   
 
 63  56
         DataInputStream dataInputStream =
 64   
             new DataInputStream(byteStream);
 65   
 
 66  56
         List segments = new ArrayList();
 67   
 
 68  56
         boolean endOfStream = false;
 69  56
         int segmentIndex = 0;
 70  56
         final int segmentLength = 4096;
 71  56
         int lastSegmentLength = 0;
 72  56
         while (!endOfStream) {
 73  2120
             int offset = segmentIndex * segmentLength;
 74  2120
             byte[] buffer = new byte[segmentLength];
 75   
 
 76  2120
             int thisSegmentLength =
 77   
                 dataInputStream.read(buffer, offset, segmentLength);
 78   
 
 79  2120
             if (thisSegmentLength == -1) {
 80  56
                 endOfStream = true;
 81   
             }
 82   
             else {
 83  2064
                 segments.add(buffer);
 84  2064
                 lastSegmentLength = thisSegmentLength;
 85   
             }
 86   
         }
 87   
 
 88  56
         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  96
     public static byte[] flattenByteSegments(
 107   
         List byteSegments,
 108   
         int eachSegmentButLastLength,
 109   
         int lastSegmentLength) {
 110   
 
 111  96
         if (byteSegments.isEmpty())
 112  32
             return new byte[0];
 113   
         else {
 114  64
             int totalBytes =
 115   
                 eachSegmentButLastLength * (byteSegments.size() - 1)
 116   
                     + lastSegmentLength;
 117   
 
 118  64
             byte[] flattenedBytes = new byte[totalBytes];
 119   
 
 120  64
             int nextSegmentOffset = 0;
 121   
 
 122  64
             for (int i = 0; i < byteSegments.size() - 1; i++) {
 123  2056
                 System.arraycopy(
 124   
                     byteSegments.get(i),
 125   
                     0,
 126   
                     flattenedBytes,
 127   
                     nextSegmentOffset,
 128   
                     eachSegmentButLastLength);
 129   
 
 130  2056
                 nextSegmentOffset += eachSegmentButLastLength;
 131   
             }
 132   
 
 133  64
             System.arraycopy(
 134   
                 truncateByteSegment(
 135   
                     (byte[]) byteSegments.get(byteSegments.size() - 1),
 136   
                     lastSegmentLength),
 137   
                 0,
 138   
                 flattenedBytes,
 139   
                 nextSegmentOffset,
 140   
                 lastSegmentLength);
 141   
 
 142  64
             return flattenedBytes;
 143   
         }
 144   
     }
 145   
 
 146  64
     private static byte[] truncateByteSegment(
 147   
         byte[] byteSegment,
 148   
         int toSize) {
 149   
 
 150  64
         byte[] newSegment = new byte[toSize];
 151  64
         System.arraycopy(byteSegment, 0, newSegment, 0, toSize);
 152  64
         return newSegment;
 153   
     }
 154   
 }
 155