Skip to content

Commit 65c19c4

Browse files
marschallBrian Burkhalter
authored andcommitted
4926314: Optimize Reader.read(CharBuffer)
Reviewed-by: alanb, bpb
1 parent 082abbd commit 65c19c4

6 files changed

Lines changed: 371 additions & 10 deletions

File tree

src/java.base/share/classes/java/io/CharArrayReader.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package java.io;
2727

28+
import java.nio.CharBuffer;
2829
import java.util.Objects;
2930

3031
/**
@@ -152,6 +153,23 @@ public int read(char[] cbuf, int off, int len) throws IOException {
152153
}
153154
}
154155

156+
@Override
157+
public int read(CharBuffer target) throws IOException {
158+
synchronized (lock) {
159+
ensureOpen();
160+
161+
if (pos >= count) {
162+
return -1;
163+
}
164+
165+
int avail = count - pos;
166+
int len = Math.min(avail, target.remaining());
167+
target.put(buf, pos, len);
168+
pos += len;
169+
return len;
170+
}
171+
}
172+
155173
/**
156174
* Skips characters. If the stream is already at its end before this method
157175
* is invoked, then no characters are skipped and zero is returned.

src/java.base/share/classes/java/io/InputStreamReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package java.io;
2727

28+
import java.nio.CharBuffer;
2829
import java.nio.charset.Charset;
2930
import java.nio.charset.CharsetDecoder;
3031
import sun.nio.cs.StreamDecoder;
@@ -152,6 +153,10 @@ public String getEncoding() {
152153
return sd.getEncoding();
153154
}
154155

156+
public int read(CharBuffer target) throws IOException {
157+
return sd.read(target);
158+
}
159+
155160
/**
156161
* Reads a single character.
157162
*

src/java.base/share/classes/java/io/Reader.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,25 @@ protected Reader(Object lock) {
184184
* @since 1.5
185185
*/
186186
public int read(CharBuffer target) throws IOException {
187-
int len = target.remaining();
188-
char[] cbuf = new char[len];
189-
int n = read(cbuf, 0, len);
190-
if (n > 0)
191-
target.put(cbuf, 0, n);
192-
return n;
187+
int nread;
188+
if (target.hasArray()) {
189+
char[] cbuf = target.array();
190+
int pos = target.position();
191+
int rem = target.limit() - pos;
192+
if (rem <= 0)
193+
return -1;
194+
int off = target.arrayOffset() + pos;
195+
nread = this.read(cbuf, off, rem);
196+
if (nread > 0)
197+
target.position(pos + nread);
198+
} else {
199+
int len = target.remaining();
200+
char[] cbuf = new char[len];
201+
nread = read(cbuf, 0, len);
202+
if (nread > 0)
203+
target.put(cbuf, 0, nread);
204+
}
205+
return nread;
193206
}
194207

195208
/**
@@ -206,7 +219,7 @@ public int read(CharBuffer target) throws IOException {
206219
* @throws IOException If an I/O error occurs
207220
*/
208221
public int read() throws IOException {
209-
char cb[] = new char[1];
222+
char[] cb = new char[1];
210223
if (read(cb, 0, 1) == -1)
211224
return -1;
212225
else
@@ -231,7 +244,7 @@ public int read() throws IOException {
231244
*
232245
* @throws IOException If an I/O error occurs
233246
*/
234-
public int read(char cbuf[]) throws IOException {
247+
public int read(char[] cbuf) throws IOException {
235248
return read(cbuf, 0, cbuf.length);
236249
}
237250

@@ -258,13 +271,13 @@ public int read(char cbuf[]) throws IOException {
258271
* or {@code len} is greater than {@code cbuf.length - off}
259272
* @throws IOException If an I/O error occurs
260273
*/
261-
public abstract int read(char cbuf[], int off, int len) throws IOException;
274+
public abstract int read(char[] cbuf, int off, int len) throws IOException;
262275

263276
/** Maximum skip-buffer size */
264277
private static final int maxSkipBufferSize = 8192;
265278

266279
/** Skip buffer, null until allocated */
267-
private char skipBuffer[] = null;
280+
private char[] skipBuffer = null;
268281

269282
/**
270283
* Skips characters. This method will block until some characters are
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
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
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4926314
27+
* @summary Test for CharArrayReader#read(CharBuffer).
28+
* @run testng ReadCharBuffer
29+
*/
30+
31+
import org.testng.annotations.DataProvider;
32+
import org.testng.annotations.Test;
33+
34+
35+
import java.io.CharArrayReader;
36+
import java.io.IOException;
37+
import java.io.Reader;
38+
import java.nio.ByteBuffer;
39+
import java.nio.CharBuffer;
40+
import java.util.Arrays;
41+
42+
import static org.testng.Assert.assertEquals;
43+
44+
public class ReadCharBuffer {
45+
46+
private static final int BUFFER_SIZE = 7;
47+
48+
@DataProvider(name = "buffers")
49+
public Object[][] createBuffers() {
50+
// test both on-heap and off-heap buffers as they may use different code paths
51+
return new Object[][]{
52+
new Object[]{CharBuffer.allocate(BUFFER_SIZE)},
53+
new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()}
54+
};
55+
}
56+
57+
@Test(dataProvider = "buffers")
58+
public void read(CharBuffer buffer) throws IOException {
59+
fillBuffer(buffer);
60+
61+
try (Reader reader = new CharArrayReader("ABCD".toCharArray())) {
62+
buffer.limit(3);
63+
buffer.position(1);
64+
assertEquals(reader.read(buffer), 2);
65+
assertEquals(buffer.position(), 3);
66+
assertEquals(buffer.limit(), 3);
67+
68+
buffer.limit(7);
69+
buffer.position(4);
70+
assertEquals(reader.read(buffer), 2);
71+
assertEquals(buffer.position(), 6);
72+
assertEquals(buffer.limit(), 7);
73+
74+
assertEquals(reader.read(buffer), -1);
75+
}
76+
77+
buffer.clear();
78+
assertEquals(buffer.toString(), "xABxCDx");
79+
}
80+
81+
private void fillBuffer(CharBuffer buffer) {
82+
char[] filler = new char[BUFFER_SIZE];
83+
Arrays.fill(filler, 'x');
84+
buffer.put(filler);
85+
buffer.clear();
86+
}
87+
88+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
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
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4926314
27+
* @summary Test for InputStreamReader#read(CharBuffer).
28+
* @run testng ReadCharBuffer
29+
*/
30+
31+
import org.testng.annotations.DataProvider;
32+
import org.testng.annotations.Test;
33+
34+
35+
import java.io.ByteArrayInputStream;
36+
import java.io.IOException;
37+
import java.io.InputStreamReader;
38+
import java.io.Reader;
39+
import java.nio.ByteBuffer;
40+
import java.nio.CharBuffer;
41+
import java.util.Arrays;
42+
43+
import static java.nio.charset.StandardCharsets.US_ASCII;
44+
import static org.testng.Assert.assertEquals;
45+
46+
public class ReadCharBuffer {
47+
48+
private static final int BUFFER_SIZE = 24;
49+
50+
@DataProvider(name = "buffers")
51+
public Object[][] createBuffers() {
52+
// test both on-heap and off-heap buffers as they make use different code paths
53+
return new Object[][]{
54+
new Object[]{CharBuffer.allocate(BUFFER_SIZE)},
55+
new Object[]{ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer()}
56+
};
57+
}
58+
59+
@Test(dataProvider = "buffers")
60+
public void read(CharBuffer buffer) throws IOException {
61+
fillBuffer(buffer);
62+
63+
try (Reader reader = new InputStreamReader(new ByteArrayInputStream("ABCDEFGHIJKLMNOPQRTUVWXYZ".getBytes(US_ASCII)), US_ASCII)) {
64+
buffer.limit(7);
65+
buffer.position(1);
66+
assertEquals(reader.read(buffer), 6);
67+
assertEquals(buffer.position(), 7);
68+
assertEquals(buffer.limit(), 7);
69+
70+
buffer.limit(16);
71+
buffer.position(8);
72+
assertEquals(reader.read(buffer), 8);
73+
assertEquals(buffer.position(), 16);
74+
assertEquals(buffer.limit(), 16);
75+
}
76+
77+
buffer.clear();
78+
assertEquals(buffer.toString(), "xABCDEFxGHIJKLMNxxxxxxxx");
79+
}
80+
81+
private void fillBuffer(CharBuffer buffer) {
82+
char[] filler = new char[BUFFER_SIZE];
83+
Arrays.fill(filler, 'x');
84+
buffer.put(filler);
85+
buffer.clear();
86+
}
87+
88+
}

0 commit comments

Comments
 (0)