Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use partial read results to avoid I/O block when reading at the end o…
…f a stream
  • Loading branch information
xerial committed May 25, 2015
commit af428dee61c3468e0db593e26980df2d03444f17
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,13 @@ public MessageBuffer next() throws IOException {
if(reachedEOF)
return null;

byte[] buffer = null;
int cursor = 0;
while(!reachedEOF && cursor < bufferSize) {
if(buffer == null) {
buffer = new byte[bufferSize];
}

int readLen = -1;
// available() == 0 means, it reached the end of the stream
if(in.available() == 0 ||
(readLen = in.read(buffer, cursor, bufferSize - cursor)) == -1) {
reachedEOF = true;
break;
}
cursor += readLen;
byte[] buffer = new byte[bufferSize];
int readLen = in.read(buffer);
if(readLen == -1) {
reachedEOF = true;
return null;
}

return buffer == null ? null : MessageBuffer.wrap(buffer).slice(0, cursor);
return MessageBuffer.wrap(buffer).slice(0, readLen);
}

@Override
Expand Down