Skip to content

Commit 332e205

Browse files
committed
String, StringBuffer, StringBuilder to Java8
1 parent 42c4265 commit 332e205

File tree

6 files changed

+3274
-2968
lines changed

6 files changed

+3274
-2968
lines changed
Lines changed: 149 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,172 @@
11
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0
6-
* (the "License"); you may not use this file except in compliance with
7-
* the License. You may obtain a copy of the License at
2+
* Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
84
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
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
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
1010
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
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+
*
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.
1624
*/
1725

1826
package java.io;
1927

20-
2128
/**
22-
* StringBufferInputStream is a class for to allow a String to be used as an
23-
* InputStream.
24-
*
25-
* @deprecated Use StringReader
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
32+
* <code>ByteArrayInputStream</code>.
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
40+
* @since JDK1.0
41+
* @deprecated This class does not properly convert characters into bytes. As
42+
* of JDK&nbsp;1.1, the preferred way to create a stream from a
43+
* string is via the <code>StringReader</code> class.
2644
*/
2745
@Deprecated
28-
public class StringBufferInputStream extends InputStream {
29-
/**
30-
* The String containing the data to read.
31-
*/
32-
protected String buffer;
46+
public
47+
class StringBufferInputStream extends InputStream {
48+
/**
49+
* The string from which bytes are read.
50+
*/
51+
protected String buffer;
3352

34-
/**
35-
* The total number of characters inside the buffer.
36-
*/
37-
protected int count;
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;
3859

39-
/**
40-
* The current position within the String buffer.
41-
*/
42-
protected int pos;
60+
/**
61+
* The number of valid characters in the input stream buffer.
62+
*
63+
* @see java.io.StringBufferInputStream#buffer
64+
*/
65+
protected int count;
4366

44-
/**
45-
* Constructs a new StringBufferInputStream on the String <code>str</code>.
46-
*
47-
* @param str
48-
* the String to read characters from.
49-
*/
50-
public StringBufferInputStream(String str) {
51-
if (str != null) {
52-
buffer = str;
53-
count = str.length();
54-
} else {
55-
throw new NullPointerException();
56-
}
57-
}
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+
}
5876

59-
/**
60-
* Answers an int representing then number of characters that are available
61-
* to read.
62-
*
63-
* @return the number of characters available.
64-
*
65-
*/
66-
@Override
67-
public synchronized int available() {
68-
return count - pos;
69-
}
70-
71-
/**
72-
* Reads a single byte from this InputStream and returns the result as an
73-
* int. The low-order byte is returned or -1 of the end of stream was
74-
* encountered.
75-
*
76-
* @return the byte read or -1 if end of stream.
77-
*/
78-
@Override
77+
/**
78+
* Reads the next byte of data from this input stream. The value
79+
* byte is returned as an <code>int</code> in the range
80+
* <code>0</code> to <code>255</code>. If no byte is available
81+
* because the end of the stream has been reached, the value
82+
* <code>-1</code> is returned.
83+
* <p>
84+
* The <code>read</code> method of
85+
* <code>StringBufferInputStream</code> cannot block. It returns the
86+
* low eight bits of the next character in this input stream's buffer.
87+
*
88+
* @return the next byte of data, or <code>-1</code> if the end of the
89+
* stream is reached.
90+
*/
7991
public synchronized int read() {
80-
return pos < count ? buffer.charAt(pos++) & 0xFF : -1;
81-
}
82-
83-
/**
84-
* Reads at most <code>length</code> bytes from this InputStream and
85-
* stores them in byte array <code>b</code> starting at
86-
* <code>offset</code>. Answer the number of bytes actually read or -1 if
87-
* no bytes were read and end of stream was encountered.
88-
*
89-
* @param b
90-
* the byte array in which to store the read bytes.
91-
* @param offset
92-
* the offset in <code>b</code> to store the read bytes.
93-
* @param length
94-
* the maximum number of bytes to store in <code>b</code>.
95-
* @return the number of bytes actually read or -1 if end of stream.
96-
*/
97-
@Override
98-
public synchronized int read(byte b[], int offset, int length) {
99-
// According to 22.7.6 should return -1 before checking other
100-
// parameters.
101-
if (pos >= count) {
102-
return -1;
103-
}
104-
if (b != null) {
105-
// avoid int overflow
106-
if (0 <= offset && offset <= b.length && 0 <= length
107-
&& length <= b.length - offset) {
108-
if (length == 0) {
109-
return 0;
110-
}
92+
return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
93+
}
11194

112-
int copylen = count - pos < length ? count - pos : length;
113-
for (int i = 0; i < copylen; i++) {
114-
b[offset + i] = (byte) buffer.charAt(pos + i);
115-
}
116-
pos += copylen;
117-
return copylen;
118-
}
119-
throw new ArrayIndexOutOfBoundsException();
120-
}
121-
throw new NullPointerException(org.apache.harmony.luni.util.Msg.getString("K0047")); //$NON-NLS-1$
122-
}
95+
/**
96+
* Reads up to <code>len</code> bytes of data from this input stream
97+
* into an array of bytes.
98+
* <p>
99+
* 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+
}
123132

124-
/**
125-
* Reset this InputStream to position 0. Reads/Skips will now take place
126-
* from this position.
127-
*
128-
*/
129-
@Override
130-
public synchronized void reset() {
131-
pos = 0;
132-
}
133+
return len;
134+
}
133135

134-
/**
135-
* Skips <code>count</code> number of characters in this InputStream.
136-
* Subsequent <code>read()</code>'s will not return these characters
137-
* unless <code>reset()</code> is used.
138-
*
139-
* @param n
140-
* the number of characters to skip.
141-
* @return the number of characters actually skipped.
142-
*/
143-
@Override
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+
*/
144143
public synchronized long skip(long n) {
145-
if (n <= 0) {
144+
if (n < 0) {
146145
return 0;
147146
}
147+
if (n > count - pos) {
148+
n = count - pos;
149+
}
150+
pos += n;
151+
return n;
152+
}
148153

149-
int numskipped;
150-
if (this.count - pos < n) {
151-
numskipped = this.count - pos;
152-
pos = this.count;
153-
} else {
154-
numskipped = (int) n;
155-
pos += n;
156-
}
157-
return numskipped;
158-
}
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&nbsp;-&nbsp;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+
}
159172
}

0 commit comments

Comments
 (0)