Nov11 / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Output.writeAscii writing a single character string will fail to be deserialized using Input.readString #146

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Put this into a junit test case with appropriate imports and execute:

private static class SingleCharacterClass implements KryoSerializable {
  private static final String END = "END";
  private char c = '\0';
  private boolean writeAscii = false;

  public SingleCharacterClass() {
  }

  public SingleCharacterClass(char c) {
    this.c = c;
  }

  @Override
  public void write(Kryo kryo, Output output) {
    String s = new String(new char[] {c});
    if (writeAscii) {
      output.writeAscii(s);
    } else {
      output.writeString(s);
    }
    output.writeString(END);
  }

  @Override
  public void read(Kryo kryo, Input input) {
    String s = input.readString();
    this.c = s.charAt(0);
    s = input.readString(); // read END
  }
}

@Test
public void testSingleCharacterSerializationFailure() {
  SingleCharacterClass s = new SingleCharacterClass('A');

  Kryo kryo = new Kryo();
  byte[] bytes = new byte[100];
  Output output = new Output(bytes);
  kryo.writeObject(output, s);
  Input input = new Input(bytes);
  SingleCharacterClass result = kryo.readObject(input, SingleCharacterClass.class);
  Assert.assertEquals('A', result.c);

  s.writeAscii=true;

  output = new Output(bytes);
  kryo.writeObject(output, s);
  input = new Input(bytes);

  // this will now throw a KryoException: Buffer underflow
  result = kryo.readObject(input, SingleCharacterClass.class);

  Assert.assertEquals('A', result.c);
}

What is the expected output? What do you see instead?
I expect the test case above to succeed instead of throwing an exception.

What version of the Kryo are you using?
version 2.20

Please provide any additional information below.

Original issue reported on code.google.com by i...@bella.name on 12 Nov 2013 at 3:43

GoogleCodeExporter commented 8 years ago
From a quick look, you need to close the Output, which has buffered the data.

Original comment by onesweet...@gmail.com on 12 Nov 2013 at 3:48

GoogleCodeExporter commented 8 years ago
Sorry, I was writing a quick side test case to demonstrate the problem.  The 
problem still demostrates itself adding output.close() in the appropriate 
places....

Original comment by i...@bella.name on 12 Nov 2013 at 4:00

GoogleCodeExporter commented 8 years ago
Gotcha, fixed on github.
https://github.com/EsotericSoftware/kryo/commit/84366e7ac9d575460a94562d7c8755e6
104e5937

Original comment by nathan.s...@gmail.com on 12 Nov 2013 at 4:28

GoogleCodeExporter commented 8 years ago
Sweet, that's what I cause service....good job!

Original comment by i...@bella.name on 12 Nov 2013 at 4:32

GoogleCodeExporter commented 8 years ago
I mean that's what I call service..;)

Original comment by i...@bella.name on 12 Nov 2013 at 4:32