KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > io > v1 > ReadByteStream


1 /*
2  * @(#)ReadByteStream.java 1.0.0 04/13/2001 - 11:44:03
3  *
4  * Copyright (C) 2001,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26  
27
28 package net.sourceforge.groboutils.util.io.v1;
29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import java.io.InputStream JavaDoc;
34
35
36
37
38
39 /**
40  * Reads a byte array from a stream until the stream is finished.
41  * You can specify a maximum size to read, and the block read size.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @since April 13, 2001
45  * @version $Date: 2003/05/19 20:31:47 $
46  */

47 public class ReadByteStream
48 {
49     //----------------------------
50
// Public data
51

52     /**
53      * Read in an unlimited number of bytes. This can be very
54      * dangerous.
55      */

56     public static final int READ_TO_END_OF_STREAM = Integer.MAX_VALUE;
57     
58     /**
59      * Default block read size.
60      */

61     public static final int DEFAULT_BLOCK_READ_SIZE = 4096;
62     
63     //----------------------------
64
// Private data
65

66     private InputStream JavaDoc m_is;
67     private int m_maxSize;
68     private int m_bufferSize;
69     
70     
71     //----------------------------
72
// constructors
73

74     /**
75      * Default constructor
76      */

77     public ReadByteStream( InputStream JavaDoc is )
78     {
79         this( is, READ_TO_END_OF_STREAM, DEFAULT_BLOCK_READ_SIZE );
80     }
81     
82     
83     /**
84      *
85      */

86     public ReadByteStream( InputStream JavaDoc is, int maxReadSize, int blockReadSize )
87     {
88         setInputStream( is );
89         setSizes( maxReadSize, blockReadSize );
90     }
91     
92     
93     //----------------------------
94
// Public methods
95

96     
97     /**
98      * Sets the internal input stream.
99      */

100     public void setInputStream( InputStream JavaDoc is )
101     {
102         if (is == null)
103         {
104             throw new IllegalArgumentException JavaDoc( "input stream is null" );
105         }
106         this.m_is = is;
107     }
108     
109     
110     /**
111      * Sets the internal sizes.
112      */

113     public void setSizes( int maxReadSize, int blockReadSize )
114     {
115         if (blockReadSize <= 0)
116         {
117             blockReadSize = DEFAULT_BLOCK_READ_SIZE;
118         }
119         if (maxReadSize <= 0 || maxReadSize > READ_TO_END_OF_STREAM)
120         {
121             maxReadSize = READ_TO_END_OF_STREAM;
122         }
123         if (maxReadSize < blockReadSize)
124         {
125             blockReadSize = maxReadSize;
126         }
127         this.m_maxSize = maxReadSize;
128         this.m_bufferSize = blockReadSize;
129     }
130     
131     
132     /**
133      * Read in the byte stream, using the current settings.
134      */

135     public byte[] readByteStream()
136             throws IOException JavaDoc
137     {
138         return readByteStream( this.m_is, this.m_maxSize, this.m_bufferSize );
139     }
140     
141     
142     /**
143      * Read in the byte stream. Does not close the stream after it has
144      * finished reading. Uses the default sizes.
145      *
146      * @see #readByteStream( InputStream, int, int )
147      */

148     public static byte[] readByteStream( InputStream JavaDoc is )
149             throws IOException JavaDoc
150     {
151         return readByteStream( is, READ_TO_END_OF_STREAM,
152             DEFAULT_BLOCK_READ_SIZE );
153     }
154     
155     
156     /**
157      * Read in the byte stream. Does not close the stream after it has
158      * finished reading.
159      * <P>
160      * Note that there is no variable checking, for performance reasons.
161      * The user needs to verify that:
162      * @param is the input stream, which cannot be <tt>null</tt>.
163      * @param maxReadSize the maximum number of bytes to read, which
164      * must be positive, and must be modulo 0 of <tt>blockReadSize</tt>.
165      * This is an "estimation", and may actually read in more than this
166      * many bytes if it is not modulo 0 of <tt>blockReadSize</tt>, but
167      * will always return all the bytes read.
168      * @param blockReadSize the number of bytes to read in per read command,
169      * which cannot be more than <tt>maxReadSize</tt>, and cannot be
170      * less than or equal to zero.
171      */

172     public static byte[] readByteStream( InputStream JavaDoc is, int maxReadSize,
173             int blockReadSize )
174             throws IOException JavaDoc
175     {
176         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
177         byte buffer[] = new byte[ blockReadSize ];
178         int size = is.read( buffer, 0, blockReadSize );
179         int totSize = size;
180         while (size > 0 && totSize < maxReadSize)
181         {
182             baos.write( buffer, 0, size );
183             size = is.read( buffer, 0, blockReadSize );
184             totSize += size;
185         }
186         baos.close();
187         return baos.toByteArray();
188     }
189 }
190  
191
Popular Tags