Skip to content

Commit 84189eb

Browse files
committed
Improved filling InputChunked buffer.
closes #651
1 parent 4d793a0 commit 84189eb

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/com/esotericsoftware/kryo/io/InputChunked.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,34 @@ public void rewind () {
6666
}
6767

6868
protected int fill (byte[] buffer, int offset, int count) throws KryoException {
69-
if (chunkSize == -1) // No current chunk, expect a new chunk.
70-
readChunkSize();
71-
else if (chunkSize == 0) // End of chunks.
69+
if (chunkSize == -1) { // No current chunk, expect a new chunk.
70+
if (!readChunkSize()) return -1;
71+
} else if (chunkSize == 0) // End of chunk.
7272
return -1;
7373
int actual = super.fill(buffer, offset, Math.min(chunkSize, count));
7474
chunkSize -= actual;
75-
if (chunkSize == 0) readChunkSize(); // Read next chunk size.
75+
if (chunkSize == 0 && !readChunkSize()) return -1;
7676
return actual;
7777
}
7878

79-
private void readChunkSize () {
79+
/** @return false if the end of the stream was reached. */
80+
private boolean readChunkSize () {
8081
try {
8182
InputStream inputStream = getInputStream();
8283
for (int offset = 0, result = 0; offset < 32; offset += 7) {
8384
int b = inputStream.read();
84-
if (b == -1) throw new KryoException("Buffer underflow.");
85+
if (b == -1) return false;
8586
result |= (b & 0x7F) << offset;
8687
if ((b & 0x80) == 0) {
8788
chunkSize = result;
88-
if (TRACE) trace("kryo", "Read chunk: " + chunkSize);
89-
return;
89+
if (TRACE && chunkSize > 0) trace("kryo", "Read chunk: " + chunkSize);
90+
return true;
9091
}
9192
}
9293
} catch (IOException ex) {
93-
throw new KryoException(ex);
94+
throw new KryoException("Unable to read chunk size.", ex);
9495
}
95-
throw new KryoException("Malformed integer.");
96+
throw new KryoException("Unable to read chunk size: malformed integer");
9697
}
9798

9899
/** Advances the stream to the next set of chunks. InputChunked will appear to hit the end of the data until this method is

0 commit comments

Comments
 (0)