001: package com.mockrunner.util.common;
002:
003: import java.io.BufferedReader;
004: import java.io.ByteArrayInputStream;
005: import java.io.ByteArrayOutputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.io.Reader;
009: import java.io.StringReader;
010: import java.util.ArrayList;
011: import java.util.Arrays;
012: import java.util.List;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: import com.mockrunner.base.NestedApplicationException;
018:
019: /**
020: * Simple util class for manipulating streams
021: */
022: public class StreamUtil {
023: private final static Log log = LogFactory.getLog(StreamUtil.class);
024:
025: /**
026: * Returns the contents of the input stream as byte array.
027: * @param stream the <code>InputStream</code>
028: * @return the stream content as byte array
029: */
030: public static byte[] getStreamAsByteArray(InputStream stream) {
031: return getStreamAsByteArray(stream, -1);
032: }
033:
034: /**
035: * Returns the contents of the input stream as byte array.
036: * @param stream the <code>InputStream</code>
037: * @param length the number of bytes to copy, if length < 0,
038: * the number is unlimited
039: * @return the stream content as byte array
040: */
041: public static byte[] getStreamAsByteArray(InputStream stream,
042: int length) {
043: if (length == 0)
044: return new byte[0];
045: boolean checkLength = true;
046: if (length < 0) {
047: length = Integer.MAX_VALUE;
048: checkLength = false;
049: }
050: ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
051: try {
052: int nextValue = stream.read();
053: if (checkLength)
054: length--;
055: while (-1 != nextValue && length >= 0) {
056: byteStream.write(nextValue);
057: nextValue = stream.read();
058: if (checkLength)
059: length--;
060: }
061: byteStream.flush();
062: } catch (IOException exc) {
063: log.error("Exception while reading from stream", exc);
064: }
065: return byteStream.toByteArray();
066: }
067:
068: /**
069: * Returns the contents of the reader as a string.
070: * @param reader the <code>Reader</code>
071: * @return the reader content as <code>String</code>
072: */
073: public static String getReaderAsString(Reader reader) {
074: return getReaderAsString(reader, -1);
075: }
076:
077: /**
078: * Returns the contents of the reader as a string.
079: * @param reader the <code>Reader</code>
080: * @param length the number of chars to copy, if length < 0,
081: * the number is unlimited
082: * @return the reader content as <code>String</code>
083: */
084: public static String getReaderAsString(Reader reader, int length) {
085: if (length == 0)
086: return "";
087: boolean checkLength = true;
088: if (length < 0) {
089: length = Integer.MAX_VALUE;
090: checkLength = false;
091: }
092: StringBuffer buffer = new StringBuffer();
093: try {
094: int nextValue = reader.read();
095: if (checkLength)
096: length--;
097: while (-1 != nextValue && length >= 0) {
098: buffer.append((char) nextValue);
099: nextValue = reader.read();
100: if (checkLength)
101: length--;
102: }
103: } catch (IOException exc) {
104: log.error("Exception while reading from reader", exc);
105: }
106: return buffer.toString();
107: }
108:
109: /**
110: * Returns a copy of the specified stream. If the specified stream supports
111: * marking, it will be reset after the copy.
112: * @param sourceStream the stream to copy
113: * @return a copy of the stream
114: */
115: public static InputStream copyStream(InputStream sourceStream) {
116: try {
117: if (sourceStream.markSupported())
118: sourceStream.mark(sourceStream.available());
119: byte[] sourceData = getStreamAsByteArray(sourceStream);
120: if (sourceStream.markSupported())
121: sourceStream.reset();
122: return new ByteArrayInputStream(sourceData);
123: } catch (IOException exc) {
124: log.error("Exception while copying stream", exc);
125: return null;
126: }
127: }
128:
129: /**
130: * Returns a copy of the specified reader. If the specified reader supports
131: * marking, it will be reset after the copy.
132: * @param sourceReader the stream to reader
133: * @return a copy of the reader
134: */
135: public static Reader copyReader(Reader sourceReader) {
136: try {
137: if (sourceReader.markSupported())
138: sourceReader.mark(Integer.MAX_VALUE);
139: String sourceData = getReaderAsString(sourceReader);
140: if (sourceReader.markSupported())
141: sourceReader.reset();
142: return new StringReader(sourceData);
143: } catch (IOException exc) {
144: log.error("Exception while copying reader", exc);
145: return null;
146: }
147: }
148:
149: /**
150: * Compares the content of the streams. If the streams support
151: * marking, they will be reset after the comparison.
152: * @param sourceStream the source stream
153: * @param targetStream the target stream
154: * @return <code>true</code>, if the streams are identical, <code>false</code> otherwise
155: */
156: public static boolean compareStreams(InputStream sourceStream,
157: InputStream targetStream) {
158: try {
159: if (sourceStream.markSupported())
160: sourceStream.mark(sourceStream.available());
161: if (targetStream.markSupported())
162: targetStream.mark(targetStream.available());
163: byte[] sourceArray = getStreamAsByteArray(sourceStream);
164: byte[] targetArray = getStreamAsByteArray(targetStream);
165: if (sourceStream.markSupported())
166: sourceStream.reset();
167: if (targetStream.markSupported())
168: targetStream.reset();
169: return Arrays.equals(sourceArray, targetArray);
170: } catch (IOException exc) {
171: log.error("Exception while comparing streams", exc);
172: return false;
173: }
174: }
175:
176: /**
177: * Compares the content of the readers. If the readers support
178: * marking, they will be reset after the comparison.
179: * @param sourceReader the source stream
180: * @param targetReader the target stream
181: * @return <code>true</code>, if the streams are identical, <code>false</code> otherwise
182: */
183: public static boolean compareReaders(Reader sourceReader,
184: Reader targetReader) {
185: try {
186: if (sourceReader.markSupported())
187: sourceReader.mark(Integer.MAX_VALUE);
188: if (targetReader.markSupported())
189: targetReader.mark(Integer.MAX_VALUE);
190: String sourceString = getReaderAsString(sourceReader);
191: String targetString = getReaderAsString(targetReader);
192: if (sourceReader.markSupported())
193: sourceReader.reset();
194: if (targetReader.markSupported())
195: targetReader.reset();
196: return sourceString.equals(targetString);
197: } catch (IOException exc) {
198: log.error("Exception while comparing readers", exc);
199: return false;
200: }
201: }
202:
203: /**
204: * Reads the lines from the specified reader and adds them to a <code>List</code>.
205: * @param reader the reader
206: * @return the <code>List</code> with the lines
207: */
208: public static List getLinesFromReader(Reader reader) {
209: List resultList = new ArrayList();
210: try {
211: BufferedReader bufferedReader = new BufferedReader(reader);
212: String line = bufferedReader.readLine();
213: while (line != null) {
214: resultList.add(line);
215: line = bufferedReader.readLine();
216: }
217: } catch (IOException exc) {
218: throw new NestedApplicationException(exc);
219: }
220: return resultList;
221: }
222: }
|