001: /*
002: * @(#)ReadByteStream.java 1.0.0 04/13/2001 - 11:44:03
003: *
004: * Copyright (C) 2001,,2003 2002 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.io.v1;
028:
029: import java.io.File;
030: import java.io.IOException;
031: import java.io.ByteArrayOutputStream;
032: import java.io.InputStream;
033:
034: /**
035: * Reads a byte array from a stream until the stream is finished.
036: * You can specify a maximum size to read, and the block read size.
037: *
038: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039: * @since April 13, 2001
040: * @version $Date: 2003/05/19 20:31:47 $
041: */
042: public class ReadByteStream {
043: //----------------------------
044: // Public data
045:
046: /**
047: * Read in an unlimited number of bytes. This can be very
048: * dangerous.
049: */
050: public static final int READ_TO_END_OF_STREAM = Integer.MAX_VALUE;
051:
052: /**
053: * Default block read size.
054: */
055: public static final int DEFAULT_BLOCK_READ_SIZE = 4096;
056:
057: //----------------------------
058: // Private data
059:
060: private InputStream m_is;
061: private int m_maxSize;
062: private int m_bufferSize;
063:
064: //----------------------------
065: // constructors
066:
067: /**
068: * Default constructor
069: */
070: public ReadByteStream(InputStream is) {
071: this (is, READ_TO_END_OF_STREAM, DEFAULT_BLOCK_READ_SIZE);
072: }
073:
074: /**
075: *
076: */
077: public ReadByteStream(InputStream is, int maxReadSize,
078: int blockReadSize) {
079: setInputStream(is);
080: setSizes(maxReadSize, blockReadSize);
081: }
082:
083: //----------------------------
084: // Public methods
085:
086: /**
087: * Sets the internal input stream.
088: */
089: public void setInputStream(InputStream is) {
090: if (is == null) {
091: throw new IllegalArgumentException("input stream is null");
092: }
093: this .m_is = is;
094: }
095:
096: /**
097: * Sets the internal sizes.
098: */
099: public void setSizes(int maxReadSize, int blockReadSize) {
100: if (blockReadSize <= 0) {
101: blockReadSize = DEFAULT_BLOCK_READ_SIZE;
102: }
103: if (maxReadSize <= 0 || maxReadSize > READ_TO_END_OF_STREAM) {
104: maxReadSize = READ_TO_END_OF_STREAM;
105: }
106: if (maxReadSize < blockReadSize) {
107: blockReadSize = maxReadSize;
108: }
109: this .m_maxSize = maxReadSize;
110: this .m_bufferSize = blockReadSize;
111: }
112:
113: /**
114: * Read in the byte stream, using the current settings.
115: */
116: public byte[] readByteStream() throws IOException {
117: return readByteStream(this .m_is, this .m_maxSize,
118: this .m_bufferSize);
119: }
120:
121: /**
122: * Read in the byte stream. Does not close the stream after it has
123: * finished reading. Uses the default sizes.
124: *
125: * @see #readByteStream( InputStream, int, int )
126: */
127: public static byte[] readByteStream(InputStream is)
128: throws IOException {
129: return readByteStream(is, READ_TO_END_OF_STREAM,
130: DEFAULT_BLOCK_READ_SIZE);
131: }
132:
133: /**
134: * Read in the byte stream. Does not close the stream after it has
135: * finished reading.
136: * <P>
137: * Note that there is no variable checking, for performance reasons.
138: * The user needs to verify that:
139: * @param is the input stream, which cannot be <tt>null</tt>.
140: * @param maxReadSize the maximum number of bytes to read, which
141: * must be positive, and must be modulo 0 of <tt>blockReadSize</tt>.
142: * This is an "estimation", and may actually read in more than this
143: * many bytes if it is not modulo 0 of <tt>blockReadSize</tt>, but
144: * will always return all the bytes read.
145: * @param blockReadSize the number of bytes to read in per read command,
146: * which cannot be more than <tt>maxReadSize</tt>, and cannot be
147: * less than or equal to zero.
148: */
149: public static byte[] readByteStream(InputStream is,
150: int maxReadSize, int blockReadSize) throws IOException {
151: ByteArrayOutputStream baos = new ByteArrayOutputStream();
152: byte buffer[] = new byte[blockReadSize];
153: int size = is.read(buffer, 0, blockReadSize);
154: int totSize = size;
155: while (size > 0 && totSize < maxReadSize) {
156: baos.write(buffer, 0, size);
157: size = is.read(buffer, 0, blockReadSize);
158: totSize += size;
159: }
160: baos.close();
161: return baos.toByteArray();
162: }
163: }
|