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