001: /*
002: * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.io;
027:
028: /**
029: * This class allows an application to create an input stream in
030: * which the bytes read are supplied by the contents of a string.
031: * Applications can also read bytes from a byte array by using a
032: * <code>ByteArrayInputStream</code>.
033: * <p>
034: * Only the low eight bits of each character in the string are used by
035: * this class.
036: *
037: * @author Arthur van Hoff
038: * @version 1.33, 05/05/07
039: * @see java.io.ByteArrayInputStream
040: * @see java.io.StringReader
041: * @since JDK1.0
042: * @deprecated This class does not properly convert characters into bytes. As
043: * of JDK 1.1, the preferred way to create a stream from a
044: * string is via the <code>StringReader</code> class.
045: */
046: @Deprecated
047: public class StringBufferInputStream extends InputStream {
048: /**
049: * The string from which bytes are read.
050: */
051: protected String buffer;
052:
053: /**
054: * The index of the next character to read from the input stream buffer.
055: *
056: * @see java.io.StringBufferInputStream#buffer
057: */
058: protected int pos;
059:
060: /**
061: * The number of valid characters in the input stream buffer.
062: *
063: * @see java.io.StringBufferInputStream#buffer
064: */
065: protected int count;
066:
067: /**
068: * Creates a string input stream to read data from the specified string.
069: *
070: * @param s the underlying input buffer.
071: */
072: public StringBufferInputStream(String s) {
073: this .buffer = s;
074: count = s.length();
075: }
076:
077: /**
078: * Reads the next byte of data from this input stream. The value
079: * byte is returned as an <code>int</code> in the range
080: * <code>0</code> to <code>255</code>. If no byte is available
081: * because the end of the stream has been reached, the value
082: * <code>-1</code> is returned.
083: * <p>
084: * The <code>read</code> method of
085: * <code>StringBufferInputStream</code> cannot block. It returns the
086: * low eight bits of the next character in this input stream's buffer.
087: *
088: * @return the next byte of data, or <code>-1</code> if the end of the
089: * stream is reached.
090: */
091: public synchronized int read() {
092: return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
093: }
094:
095: /**
096: * Reads up to <code>len</code> bytes of data from this input stream
097: * into an array of bytes.
098: * <p>
099: * The <code>read</code> method of
100: * <code>StringBufferInputStream</code> cannot block. It copies the
101: * low eight bits from the characters in this input stream's buffer into
102: * the byte array argument.
103: *
104: * @param b the buffer into which the data is read.
105: * @param off the start offset of the data.
106: * @param len the maximum number of bytes read.
107: * @return the total number of bytes read into the buffer, or
108: * <code>-1</code> if there is no more data because the end of
109: * the stream has been reached.
110: */
111: public synchronized int read(byte b[], int off, int len) {
112: if (b == null) {
113: throw new NullPointerException();
114: } else if ((off < 0) || (off > b.length) || (len < 0)
115: || ((off + len) > b.length) || ((off + len) < 0)) {
116: throw new IndexOutOfBoundsException();
117: }
118: if (pos >= count) {
119: return -1;
120: }
121: if (pos + len > count) {
122: len = count - pos;
123: }
124: if (len <= 0) {
125: return 0;
126: }
127: String s = buffer;
128: int cnt = len;
129: while (--cnt >= 0) {
130: b[off++] = (byte) s.charAt(pos++);
131: }
132:
133: return len;
134: }
135:
136: /**
137: * Skips <code>n</code> bytes of input from this input stream. Fewer
138: * bytes might be skipped if the end of the input stream is reached.
139: *
140: * @param n the number of bytes to be skipped.
141: * @return the actual number of bytes skipped.
142: */
143: public synchronized long skip(long n) {
144: if (n < 0) {
145: return 0;
146: }
147: if (n > count - pos) {
148: n = count - pos;
149: }
150: pos += n;
151: return n;
152: }
153:
154: /**
155: * Returns the number of bytes that can be read from the input
156: * stream without blocking.
157: *
158: * @return the value of <code>count - pos</code>, which is the
159: * number of bytes remaining to be read from the input buffer.
160: */
161: public synchronized int available() {
162: return count - pos;
163: }
164:
165: /**
166: * Resets the input stream to begin reading from the first character
167: * of this input stream's underlying buffer.
168: */
169: public synchronized void reset() {
170: pos = 0;
171: }
172: }
|