Skip to content

Commit af428de

Browse files
committed
Use partial read results to avoid I/O block when reading at the end of a stream
1 parent 16a38b0 commit af428de

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

msgpack-core/src/main/java/org/msgpack/core/buffer/InputStreamBufferInput.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,13 @@ public MessageBuffer next() throws IOException {
5454
if(reachedEOF)
5555
return null;
5656

57-
byte[] buffer = null;
58-
int cursor = 0;
59-
while(!reachedEOF && cursor < bufferSize) {
60-
if(buffer == null) {
61-
buffer = new byte[bufferSize];
62-
}
63-
64-
int readLen = -1;
65-
// available() == 0 means, it reached the end of the stream
66-
if(in.available() == 0 ||
67-
(readLen = in.read(buffer, cursor, bufferSize - cursor)) == -1) {
68-
reachedEOF = true;
69-
break;
70-
}
71-
cursor += readLen;
57+
byte[] buffer = new byte[bufferSize];
58+
int readLen = in.read(buffer);
59+
if(readLen == -1) {
60+
reachedEOF = true;
61+
return null;
7262
}
73-
74-
return buffer == null ? null : MessageBuffer.wrap(buffer).slice(0, cursor);
63+
return MessageBuffer.wrap(buffer).slice(0, readLen);
7564
}
7665

7766
@Override

0 commit comments

Comments
 (0)