author | igerasim |
Wed, 16 Oct 2019 14:32:17 -0700 | |
changeset 58653 | 71fef5fae9cc |
parent 58288 | 48e480e56aad |
child 59201 | b24f4caa1411 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
/** |
|
29 |
* This class allows an application to create an input stream in |
|
30 |
* which the bytes read are supplied by the contents of a string. |
|
31 |
* Applications can also read bytes from a byte array by using a |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
32 |
* {@code ByteArrayInputStream}. |
2 | 33 |
* <p> |
34 |
* Only the low eight bits of each character in the string are used by |
|
35 |
* this class. |
|
36 |
* |
|
37 |
* @author Arthur van Hoff |
|
38 |
* @see java.io.ByteArrayInputStream |
|
39 |
* @see java.io.StringReader |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
5506
diff
changeset
|
40 |
* @since 1.0 |
2 | 41 |
* @deprecated This class does not properly convert characters into bytes. As |
42 |
* of JDK 1.1, the preferred way to create a stream from a |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
43 |
* string is via the {@code StringReader} class. |
2 | 44 |
*/ |
45 |
@Deprecated |
|
46 |
public |
|
47 |
class StringBufferInputStream extends InputStream { |
|
48 |
/** |
|
49 |
* The string from which bytes are read. |
|
50 |
*/ |
|
51 |
protected String buffer; |
|
52 |
||
53 |
/** |
|
54 |
* The index of the next character to read from the input stream buffer. |
|
55 |
* |
|
56 |
* @see java.io.StringBufferInputStream#buffer |
|
57 |
*/ |
|
58 |
protected int pos; |
|
59 |
||
60 |
/** |
|
61 |
* The number of valid characters in the input stream buffer. |
|
62 |
* |
|
63 |
* @see java.io.StringBufferInputStream#buffer |
|
64 |
*/ |
|
65 |
protected int count; |
|
66 |
||
67 |
/** |
|
68 |
* Creates a string input stream to read data from the specified string. |
|
69 |
* |
|
70 |
* @param s the underlying input buffer. |
|
71 |
*/ |
|
72 |
public StringBufferInputStream(String s) { |
|
73 |
this.buffer = s; |
|
74 |
count = s.length(); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Reads the next byte of data from this input stream. The value |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
79 |
* byte is returned as an {@code int} in the range |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
80 |
* {@code 0} to {@code 255}. If no byte is available |
2 | 81 |
* because the end of the stream has been reached, the value |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
82 |
* {@code -1} is returned. |
2 | 83 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
84 |
* The {@code read} method of |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
85 |
* {@code StringBufferInputStream} cannot block. It returns the |
2 | 86 |
* low eight bits of the next character in this input stream's buffer. |
87 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
88 |
* @return the next byte of data, or {@code -1} if the end of the |
2 | 89 |
* stream is reached. |
90 |
*/ |
|
91 |
public synchronized int read() { |
|
92 |
return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1; |
|
93 |
} |
|
94 |
||
95 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
96 |
* Reads up to {@code len} bytes of data from this input stream |
2 | 97 |
* into an array of bytes. |
98 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
99 |
* The {@code read} method of |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
100 |
* {@code StringBufferInputStream} cannot block. It copies the |
2 | 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 |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
108 |
* {@code -1} if there is no more data because the end of |
2 | 109 |
* the stream has been reached. |
110 |
*/ |
|
40410 | 111 |
@SuppressWarnings("deprecation") |
2 | 112 |
public synchronized int read(byte b[], int off, int len) { |
113 |
if (b == null) { |
|
114 |
throw new NullPointerException(); |
|
115 |
} else if ((off < 0) || (off > b.length) || (len < 0) || |
|
116 |
((off + len) > b.length) || ((off + len) < 0)) { |
|
117 |
throw new IndexOutOfBoundsException(); |
|
118 |
} |
|
119 |
if (pos >= count) { |
|
120 |
return -1; |
|
121 |
} |
|
40263
6d435de7abbc
8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
25859
diff
changeset
|
122 |
|
6d435de7abbc
8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
25859
diff
changeset
|
123 |
int avail = count - pos; |
6d435de7abbc
8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
25859
diff
changeset
|
124 |
if (len > avail) { |
6d435de7abbc
8163518: Integer overflow in StringBufferInputStream.read() and CharArrayReader.read/skip()
igerasim
parents:
25859
diff
changeset
|
125 |
len = avail; |
2 | 126 |
} |
127 |
if (len <= 0) { |
|
128 |
return 0; |
|
129 |
} |
|
40410 | 130 |
buffer.getBytes(pos, pos + len, b, off); |
131 |
pos += len; |
|
2 | 132 |
return len; |
133 |
} |
|
134 |
||
135 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
47216
diff
changeset
|
136 |
* Skips {@code n} bytes of input from this input stream. Fewer |
2 | 137 |
* bytes might be skipped if the end of the input stream is reached. |
138 |
* |
|
139 |
* @param n the number of bytes to be skipped. |
|
140 |
* @return the actual number of bytes skipped. |
|
141 |
*/ |
|
142 |
public synchronized long skip(long n) { |
|
143 |
if (n < 0) { |
|
144 |
return 0; |
|
145 |
} |
|
146 |
if (n > count - pos) { |
|
147 |
n = count - pos; |
|
148 |
} |
|
149 |
pos += n; |
|
150 |
return n; |
|
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* Returns the number of bytes that can be read from the input |
|
155 |
* stream without blocking. |
|
156 |
* |
|
157 |
* @return the value of <code>count - pos</code>, which is the |
|
158 |
* number of bytes remaining to be read from the input buffer. |
|
159 |
*/ |
|
160 |
public synchronized int available() { |
|
161 |
return count - pos; |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Resets the input stream to begin reading from the first character |
|
166 |
* of this input stream's underlying buffer. |
|
167 |
*/ |
|
168 |
public synchronized void reset() { |
|
169 |
pos = 0; |
|
170 |
} |
|
171 |
} |