Skip to content

Commit 1130df7

Browse files
committed
Without Fast-Path
1 parent a5995e5 commit 1130df7

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/java.base/share/classes/sun/nio/cs/StreamDecoder.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class StreamDecoder extends Reader {
4949
private static final int MIN_BYTE_BUFFER_SIZE = 32;
5050
private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
5151

52-
private volatile boolean opened;
5352
private volatile boolean closed;
5453

5554
private void ensureOpen() throws IOException {
@@ -117,7 +116,6 @@ public String getEncoding() {
117116
}
118117

119118
public int read() throws IOException {
120-
opened = true;
121119
return read0();
122120
}
123121

@@ -151,7 +149,6 @@ private int read0() throws IOException {
151149

152150
public int read(char[] cbuf, int offset, int length) throws IOException {
153151
synchronized (lock) {
154-
opened = true;
155152
int off = offset;
156153
int len = length;
157154

@@ -195,21 +192,41 @@ public int read(char[] cbuf, int offset, int length) throws IOException {
195192
}
196193
}
197194

195+
private static CharBuffer ensureFree(CharBuffer cb, int minFree) {
196+
return cb.remaining() < minFree ? CharBuffer.allocate(cb.position() + minFree).put(cb.flip()) : cb;
197+
}
198+
198199
@Override
199200
public String readAllAsString() throws IOException {
200201
synchronized (lock) {
201202
ensureOpen();
202-
final String s = (in == null || opened) ? super.readAllAsString() : new String(in.readAllBytes(), cs);
203-
opened = true;
204-
return s;
203+
204+
int estimateSize = (haveLeftoverChar ? 1 : 0) + (int) Math.ceil((bb.remaining() + in.available()) * decoder.maxCharsPerByte());
205+
int initialSize = Math.max(estimateSize, DEFAULT_BYTE_BUFFER_SIZE);
206+
CharBuffer cb = CharBuffer.allocate(initialSize);
207+
208+
if (haveLeftoverChar) {
209+
cb.put(leftoverChar);
210+
haveLeftoverChar = false;
211+
}
212+
213+
while (bb.hasRemaining()) {
214+
CoderResult cr = decoder.decode(bb, cb, false);
215+
if (cr.isError()) cr.throwException();
216+
if (cr.isOverflow()) cb = ensureFree(cb, bb.remaining());
217+
}
218+
219+
ByteBuffer bbuf = ByteBuffer.wrap(in.readAllBytes());
220+
while (bbuf.hasRemaining()) {
221+
CoderResult cr = decoder.decode(bbuf, cb, false);
222+
if (cr.isError()) cr.throwException();
223+
if (cr.isOverflow()) cb = ensureFree(cb, bbuf.remaining());
224+
}
225+
226+
return cb.flip().toString();
205227
}
206228
}
207-
/*
208-
@Override
209-
public String readAllAsString() throws IOException {
210-
throw new RuntimeException();
211-
}
212-
*/
229+
213230
public boolean ready() throws IOException {
214231
synchronized (lock) {
215232
ensureOpen();

0 commit comments

Comments
 (0)