Skip to content

Propagate flush to underlying stream #344

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

Merged
merged 1 commit into from
Sep 3, 2015
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/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public void flush () throws KryoException {
if (outputStream == null) return;
try {
outputStream.write(buffer, 0, position);
outputStream.flush();
} catch (IOException ex) {
throw new KryoException(ex);
}
Expand Down
1 change: 1 addition & 0 deletions src/com/esotericsoftware/kryo/io/OutputChunked.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void flush () throws KryoException {
if (position() > 0) {
try {
writeChunkSize();
super.flush();
} catch (IOException ex) {
throw new KryoException(ex);
}
Expand Down
31 changes: 31 additions & 0 deletions test/com/esotericsoftware/kryo/InputOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.util.Random;

Expand Down Expand Up @@ -826,4 +828,33 @@ public void testZeroLengthOutputs() throws Exception {
Output byteBufferOutput = new ByteBufferOutput(0, 10000);
kryo.writeClassAndObject(byteBufferOutput, "Test string");
}

public void testFlushRoundTrip() throws Exception {

Kryo kryo = new Kryo();

String s1 = "12345";

ByteArrayOutputStream os=new ByteArrayOutputStream();
ObjectOutputStream objOutput = new ObjectOutputStream(os);
Output output = new Output(objOutput);

kryo.writeClass(output, s1.getClass());
kryo.writeObject(output, s1);
output.flush();
// objOutput.flush(); // this layer wasn't flushed prior to this bugfix, add it for a workaround

byte [] b = os.toByteArray();
System.out.println("size: " + b.length);

ByteArrayInputStream in = new ByteArrayInputStream(b);
ObjectInputStream objIn = new ObjectInputStream(in);
Input input = new Input(objIn);

Registration r = kryo.readClass(input);
String s2 = kryo.readObject(input,r.getType());

assertEquals(s1, s2);

}
}