Skip to content

Commit

Permalink
#834 Support skipping input chunks after a buffer underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
theigl committed Aug 4, 2021
1 parent ee19971 commit b093eca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/com/esotericsoftware/kryo/io/InputChunked.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private boolean readChunkSize () {

/** Advances the stream to the next chunk. InputChunked will appear to hit the end of the data until this method is called. */
public void nextChunk () {
position = limit; // Underflow resets the position to 0. Ensure we are at the end of the chunk.
if (chunkSize == -1) readChunkSize(); // No current chunk, expect a new chunk.
while (chunkSize > 0)
skip(chunkSize);
Expand Down
24 changes: 24 additions & 0 deletions test/com/esotericsoftware/kryo/io/ChunkedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.jupiter.api.Assertions.*;

import com.esotericsoftware.kryo.KryoException;
import org.junit.jupiter.api.Test;

/** @author Nathan Sweet */
Expand Down Expand Up @@ -56,4 +57,27 @@ void testChunks () {
assertEquals(5678, input.readInt());
input.close();
}

@Test
void testSkipAfterUnderFlow () {
Output output = new Output(512);
OutputChunked outputChunked = new OutputChunked(output);
outputChunked.writeInt(1);
outputChunked.endChunk();
outputChunked.writeInt(2);
outputChunked.endChunk();
output.close();

Input input = new Input(output.getBuffer());
InputChunked inputChunked = new InputChunked(input);
assertEquals(1, inputChunked.readInt());
try {
inputChunked.readInt();
} catch (KryoException ex) {
// underflow is desired
}
inputChunked.nextChunk();
assertEquals(2, inputChunked.readInt());
input.close();
}
}

0 comments on commit b093eca

Please sign in to comment.