KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > StringBufferInputStream


1 /*
2  * @(#)StringBufferInputStream.java 1.26 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * This class allows an application to create an input stream in
12  * which the bytes read are supplied by the contents of a string.
13  * Applications can also read bytes from a byte array by using a
14  * <code>ByteArrayInputStream</code>.
15  * <p>
16  * Only the low eight bits of each character in the string are used by
17  * this class.
18  *
19  * @author Arthur van Hoff
20  * @version 1.26, 05/18/04
21  * @see java.io.ByteArrayInputStream
22  * @see java.io.StringReader
23  * @since JDK1.0
24  * @deprecated This class does not properly convert characters into bytes. As
25  * of JDK&nbsp;1.1, the preferred way to create a stream from a
26  * string is via the <code>StringReader</code> class.
27  */

28 @Deprecated JavaDoc
29 public
30 class StringBufferInputStream extends InputStream JavaDoc {
31     /**
32      * The string from which bytes are read.
33      */

34     protected String JavaDoc buffer;
35
36     /**
37      * The index of the next character to read from the input stream buffer.
38      *
39      * @see java.io.StringBufferInputStream#buffer
40      */

41     protected int pos;
42
43     /**
44      * The number of valid characters in the input stream buffer.
45      *
46      * @see java.io.StringBufferInputStream#buffer
47      */

48     protected int count;
49
50     /**
51      * Creates a string input stream to read data from the specified string.
52      *
53      * @param s the underlying input buffer.
54      */

55     public StringBufferInputStream(String JavaDoc s) {
56     this.buffer = s;
57     count = s.length();
58     }
59
60     /**
61      * Reads the next byte of data from this input stream. The value
62      * byte is returned as an <code>int</code> in the range
63      * <code>0</code> to <code>255</code>. If no byte is available
64      * because the end of the stream has been reached, the value
65      * <code>-1</code> is returned.
66      * <p>
67      * The <code>read</code> method of
68      * <code>StringBufferInputStream</code> cannot block. It returns the
69      * low eight bits of the next character in this input stream's buffer.
70      *
71      * @return the next byte of data, or <code>-1</code> if the end of the
72      * stream is reached.
73      */

74     public synchronized int read() {
75     return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
76     }
77
78     /**
79      * Reads up to <code>len</code> bytes of data from this input stream
80      * into an array of bytes.
81      * <p>
82      * The <code>read</code> method of
83      * <code>StringBufferInputStream</code> cannot block. It copies the
84      * low eight bits from the characters in this input stream's buffer into
85      * the byte array argument.
86      *
87      * @param b the buffer into which the data is read.
88      * @param off the start offset of the data.
89      * @param len the maximum number of bytes read.
90      * @return the total number of bytes read into the buffer, or
91      * <code>-1</code> if there is no more data because the end of
92      * the stream has been reached.
93      */

94     public synchronized int read(byte b[], int off, int len) {
95     if (b == null) {
96         throw new NullPointerException JavaDoc();
97     } else if ((off < 0) || (off > b.length) || (len < 0) ||
98            ((off + len) > b.length) || ((off + len) < 0)) {
99         throw new IndexOutOfBoundsException JavaDoc();
100     }
101     if (pos >= count) {
102         return -1;
103     }
104     if (pos + len > count) {
105         len = count - pos;
106     }
107     if (len <= 0) {
108         return 0;
109     }
110     String JavaDoc s = buffer;
111     int cnt = len;
112     while (--cnt >= 0) {
113         b[off++] = (byte)s.charAt(pos++);
114     }
115
116     return len;
117     }
118
119     /**
120      * Skips <code>n</code> bytes of input from this input stream. Fewer
121      * bytes might be skipped if the end of the input stream is reached.
122      *
123      * @param n the number of bytes to be skipped.
124      * @return the actual number of bytes skipped.
125      */

126     public synchronized long skip(long n) {
127     if (n < 0) {
128         return 0;
129     }
130     if (n > count - pos) {
131         n = count - pos;
132     }
133     pos += n;
134     return n;
135     }
136
137     /**
138      * Returns the number of bytes that can be read from the input
139      * stream without blocking.
140      *
141      * @return the value of <code>count&nbsp;-&nbsp;pos</code>, which is the
142      * number of bytes remaining to be read from the input buffer.
143      */

144     public synchronized int available() {
145     return count - pos;
146     }
147
148     /**
149      * Resets the input stream to begin reading from the first character
150      * of this input stream's underlying buffer.
151      */

152     public synchronized void reset() {
153     pos = 0;
154     }
155 }
156
Popular Tags