Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#834 Support skipping input chunks after a buffer underflow #850

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
21 changes: 21 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,24 @@ 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());
// trigger buffer underflow
assertThrows(KryoException.class, inputChunked::readInt);
inputChunked.nextChunk();
assertEquals(2, inputChunked.readInt());
input.close();
}
}