KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ReadStringStream.java
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.StringWriter JavaDoc;
33 import java.io.Reader JavaDoc;
34
35
36
37
38 /**
39  * Reads a byte array from a stream until the stream is finished.
40  * You can specify a maximum size to read, and the block read size.
41  *
42  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43  * @since Alpha 0.9.1d (sometime in 2001)
44  * @version $Date: 2003/02/10 22:52:45 $
45  */

46 public class ReadStringStream
47 {
48     //----------------------------
49
// Public data
50

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

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

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

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

73     /**
74      * Create a new stream using the default parameters and the given
75      * reader.
76      *
77      * @param input reader to pull from.
78      */

79     public ReadStringStream( Reader JavaDoc input )
80     {
81         this( input, READ_TO_END_OF_STREAM, DEFAULT_BLOCK_READ_SIZE );
82     }
83     
84     
85     /**
86      *
87      * @param input reader to pull from.
88      */

89     public ReadStringStream( Reader JavaDoc input, int maxReadSize, int blockReadSize )
90     {
91         setReader( input );
92         setSizes( maxReadSize, blockReadSize );
93     }
94     
95     
96     //----------------------------
97
// Public methods
98

99     
100     /**
101      * Sets the internal input stream.
102      *
103      * @param input reader to pull from.
104      */

105     public void setReader( Reader JavaDoc input )
106     {
107         if (input == null)
108         {
109             throw new IllegalArgumentException JavaDoc( "Reader is null" );
110         }
111         this.m_is = input;
112     }
113     
114     
115     /**
116      * Sets the internal sizes.
117      */

118     public void setSizes( int maxReadSize, int blockReadSize )
119     {
120         if (blockReadSize <= 0)
121         {
122             blockReadSize = DEFAULT_BLOCK_READ_SIZE;
123         }
124         if (maxReadSize <= 0 || maxReadSize > READ_TO_END_OF_STREAM)
125         {
126             maxReadSize = READ_TO_END_OF_STREAM;
127         }
128         if (maxReadSize < blockReadSize)
129         {
130             blockReadSize = maxReadSize;
131         }
132         this.m_maxSize = maxReadSize;
133         this.m_bufferSize = blockReadSize;
134     }
135     
136     
137     /**
138      * Read in the byte stream, using the current settings.
139      *
140      * @return the read-in string
141      */

142     public String JavaDoc readStringStream()
143             throws IOException JavaDoc
144     {
145         return readStringStream( this.m_is, this.m_maxSize, this.m_bufferSize );
146     }
147     
148     
149     /**
150      * Read in the byte stream. Does not close the stream after it has
151      * finished reading. Uses the default sizes.
152      *
153      * @param input reader to pull from.
154      * @return the read-in string
155      * @see #readStringStream( Reader, int, int )
156      */

157     public static String JavaDoc readStringStream( Reader JavaDoc input )
158             throws IOException JavaDoc
159     {
160         return readStringStream( input, READ_TO_END_OF_STREAM,
161             DEFAULT_BLOCK_READ_SIZE );
162     }
163     
164     
165     /**
166      * Read in the stream to a String. Does not close the stream after it has
167      * finished reading.
168      * <P>
169      * Note that there is no variable checking, for performance reasons.
170      * The user needs to verify that:
171      * @param input the reader, which cannot be <tt>null</tt>.
172      * @param maxReadSize the maximum number of bytes to read, which
173      * must be positive, and must be modulo 0 of <tt>blockReadSize</tt>.
174      * This is an "estimation", and may actually read in more than this
175      * many bytes if it is not modulo 0 of <tt>blockReadSize</tt>, but
176      * will always return all the bytes read.
177      * @param blockReadSize the number of bytes to read in per read command,
178      * which cannot be more than <tt>maxReadSize</tt>, and cannot be
179      * less than or equal to zero.
180      */

181     public static String JavaDoc readStringStream( Reader JavaDoc input, int maxReadSize,
182             int blockReadSize )
183             throws IOException JavaDoc
184     {
185         StringWriter JavaDoc sw = new StringWriter JavaDoc();
186         char buffer[] = new char[ blockReadSize ];
187         int size = input.read( buffer, 0, blockReadSize );
188         int totSize = size;
189         while (size > 0 && totSize < maxReadSize)
190         {
191             sw.write( buffer, 0, size );
192             size = input.read( buffer, 0, blockReadSize );
193             totSize += size;
194         }
195         sw.close();
196         return sw.toString();
197     }
198 }
199
200
Popular Tags