mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
4926314: Optimize Reader.read(CharBuffer)
Reviewed-by: alanb, bpb
This commit is contained in:
parent
082abbdaf7
commit
65c19c4094
6 changed files with 371 additions and 10 deletions
|
@ -25,6 +25,7 @@
|
|||
|
||||
package java.io;
|
||||
|
||||
import java.nio.CharBuffer;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
|
@ -152,6 +153,23 @@ public class CharArrayReader extends Reader {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(CharBuffer target) throws IOException {
|
||||
synchronized (lock) {
|
||||
ensureOpen();
|
||||
|
||||
if (pos >= count) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int avail = count - pos;
|
||||
int len = Math.min(avail, target.remaining());
|
||||
target.put(buf, pos, len);
|
||||
pos += len;
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips characters. If the stream is already at its end before this method
|
||||
* is invoked, then no characters are skipped and zero is returned.
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
package java.io;
|
||||
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import sun.nio.cs.StreamDecoder;
|
||||
|
@ -152,6 +153,10 @@ public class InputStreamReader extends Reader {
|
|||
return sd.getEncoding();
|
||||
}
|
||||
|
||||
public int read(CharBuffer target) throws IOException {
|
||||
return sd.read(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a single character.
|
||||
*
|
||||
|
|
|
@ -184,12 +184,25 @@ public abstract class Reader implements Readable, Closeable {
|
|||
* @since 1.5
|
||||
*/
|
||||
public int read(CharBuffer target) throws IOException {
|
||||
int len = target.remaining();
|
||||
char[] cbuf = new char[len];
|
||||
int n = read(cbuf, 0, len);
|
||||
if (n > 0)
|
||||
target.put(cbuf, 0, n);
|
||||
return n;
|
||||
int nread;
|
||||
if (target.hasArray()) {
|
||||
char[] cbuf = target.array();
|
||||
int pos = target.position();
|
||||
int rem = target.limit() - pos;
|
||||
if (rem <= 0)
|
||||
return -1;
|
||||
int off = target.arrayOffset() + pos;
|
||||
nread = this.read(cbuf, off, rem);
|
||||
if (nread > 0)
|
||||
target.position(pos + nread);
|
||||
} else {
|
||||
int len = target.remaining();
|
||||
char[] cbuf = new char[len];
|
||||
nread = read(cbuf, 0, len);
|
||||
if (nread > 0)
|
||||
target.put(cbuf, 0, nread);
|
||||
}
|
||||
return nread;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -206,7 +219,7 @@ public abstract class Reader implements Readable, Closeable {
|
|||
* @throws IOException If an I/O error occurs
|
||||
*/
|
||||
public int read() throws IOException {
|
||||
char cb[] = new char[1];
|
||||
char[] cb = new char[1];
|
||||
if (read(cb, 0, 1) == -1)
|
||||
return -1;
|
||||
else
|
||||
|
@ -231,7 +244,7 @@ public abstract class Reader implements Readable, Closeable {
|
|||
*
|
||||
* @throws IOException If an I/O error occurs
|
||||
*/
|
||||
public int read(char cbuf[]) throws IOException {
|
||||
public int read(char[] cbuf) throws IOException {
|
||||
return read(cbuf, 0, cbuf.length);
|
||||
}
|
||||
|
||||
|
@ -258,13 +271,13 @@ public abstract class Reader implements Readable, Closeable {
|
|||
* or {@code len} is greater than {@code cbuf.length - off}
|
||||
* @throws IOException If an I/O error occurs
|
||||
*/
|
||||
public abstract int read(char cbuf[], int off, int len) throws IOException;
|
||||
public abstract int read(char[] cbuf, int off, int len) throws IOException;
|
||||
|
||||
/** Maximum skip-buffer size */
|
||||
private static final int maxSkipBufferSize = 8192;
|
||||
|
||||
/** Skip buffer, null until allocated */
|
||||
private char skipBuffer[] = null;
|
||||
private char[] skipBuffer = null;
|
||||
|
||||
/**
|
||||
* Skips characters. This method will block until some characters are
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue