001: /*
002: * @(#)ReadStringStream.java
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.StringWriter;
032: import java.io.Reader;
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 Alpha 0.9.1d (sometime in 2001)
040: * @version $Date: 2003/02/10 22:52:45 $
041: */
042: public class ReadStringStream {
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 Reader m_is;
061: private int m_maxSize;
062: private int m_bufferSize;
063:
064: //----------------------------
065: // constructors
066:
067: /**
068: * Create a new stream using the default parameters and the given
069: * reader.
070: *
071: * @param input reader to pull from.
072: */
073: public ReadStringStream(Reader input) {
074: this (input, READ_TO_END_OF_STREAM, DEFAULT_BLOCK_READ_SIZE);
075: }
076:
077: /**
078: *
079: * @param input reader to pull from.
080: */
081: public ReadStringStream(Reader input, int maxReadSize,
082: int blockReadSize) {
083: setReader(input);
084: setSizes(maxReadSize, blockReadSize);
085: }
086:
087: //----------------------------
088: // Public methods
089:
090: /**
091: * Sets the internal input stream.
092: *
093: * @param input reader to pull from.
094: */
095: public void setReader(Reader input) {
096: if (input == null) {
097: throw new IllegalArgumentException("Reader is null");
098: }
099: this .m_is = input;
100: }
101:
102: /**
103: * Sets the internal sizes.
104: */
105: public void setSizes(int maxReadSize, int blockReadSize) {
106: if (blockReadSize <= 0) {
107: blockReadSize = DEFAULT_BLOCK_READ_SIZE;
108: }
109: if (maxReadSize <= 0 || maxReadSize > READ_TO_END_OF_STREAM) {
110: maxReadSize = READ_TO_END_OF_STREAM;
111: }
112: if (maxReadSize < blockReadSize) {
113: blockReadSize = maxReadSize;
114: }
115: this .m_maxSize = maxReadSize;
116: this .m_bufferSize = blockReadSize;
117: }
118:
119: /**
120: * Read in the byte stream, using the current settings.
121: *
122: * @return the read-in string
123: */
124: public String readStringStream() throws IOException {
125: return readStringStream(this .m_is, this .m_maxSize,
126: this .m_bufferSize);
127: }
128:
129: /**
130: * Read in the byte stream. Does not close the stream after it has
131: * finished reading. Uses the default sizes.
132: *
133: * @param input reader to pull from.
134: * @return the read-in string
135: * @see #readStringStream( Reader, int, int )
136: */
137: public static String readStringStream(Reader input)
138: throws IOException {
139: return readStringStream(input, READ_TO_END_OF_STREAM,
140: DEFAULT_BLOCK_READ_SIZE);
141: }
142:
143: /**
144: * Read in the stream to a String. Does not close the stream after it has
145: * finished reading.
146: * <P>
147: * Note that there is no variable checking, for performance reasons.
148: * The user needs to verify that:
149: * @param input the reader, which cannot be <tt>null</tt>.
150: * @param maxReadSize the maximum number of bytes to read, which
151: * must be positive, and must be modulo 0 of <tt>blockReadSize</tt>.
152: * This is an "estimation", and may actually read in more than this
153: * many bytes if it is not modulo 0 of <tt>blockReadSize</tt>, but
154: * will always return all the bytes read.
155: * @param blockReadSize the number of bytes to read in per read command,
156: * which cannot be more than <tt>maxReadSize</tt>, and cannot be
157: * less than or equal to zero.
158: */
159: public static String readStringStream(Reader input,
160: int maxReadSize, int blockReadSize) throws IOException {
161: StringWriter sw = new StringWriter();
162: char buffer[] = new char[blockReadSize];
163: int size = input.read(buffer, 0, blockReadSize);
164: int totSize = size;
165: while (size > 0 && totSize < maxReadSize) {
166: sw.write(buffer, 0, size);
167: size = input.read(buffer, 0, blockReadSize);
168: totSize += size;
169: }
170: sw.close();
171: return sw.toString();
172: }
173: }
|