Skip to content

Commit

Permalink
Fix readUnsignedByte and readUnsignedShort for ObjectInputStream. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
joehni committed Feb 20, 2018
1 parent 87eb9b9 commit 3305b44
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions xstream-distribution/src/content/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ <h2>Minor changes</h2>
Märt Bakhoff).</li>
<li>GHI:#2: Unneeded contention in DefaultConverterLookup.</li>
<li>GHI:#94: Fix PathConverter containing absolute Windows paths.</li>
<li>GHI:#105: XStream's ObjectInputStream returns wrong values for readUnsignedByte and readUnsignedShort.</li>
<li>JIRA:XSTR-616 and GHPR:#93: Introduce StringCodec interface to support arbitrary Base64 codec
implementations for EncodedByteArrayConverter. Prefer Base64 codec implementations of the Java runtime over
XStream's own one.</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2004, 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2010, 2011, 2013, 2014, 2015, 2016 XStream Committers.
* Copyright (C) 2006, 2007, 2010, 2011, 2013, 2014, 2015, 2016, 2018 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -156,11 +156,7 @@ public byte readByte() throws IOException {

@Override
public int readUnsignedByte() throws IOException {
int b = ((Byte)peekCallback().readFromStream()).byteValue();
if (b < 0) {
b += Byte.MAX_VALUE;
}
return b;
return ((Byte)peekCallback().readFromStream()).intValue() & 0xff;
}

@Override
Expand Down Expand Up @@ -195,11 +191,7 @@ public short readShort() throws IOException {

@Override
public int readUnsignedShort() throws IOException {
int b = ((Short)peekCallback().readFromStream()).shortValue();
if (b < 0) {
b += Short.MAX_VALUE;
}
return b;
return ((Short)peekCallback().readFromStream()).intValue() & 0xffff;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,38 @@ public void testSupportsOptionToPreserveReferencesAcrossDifferentObjectsInStream

assertSame(alice, jane.secretary);
}

public void testReadUnsignedValuesFromInputStream() throws IOException {
final Writer writer = new StringWriter();
final ObjectOutputStream oos = xstream.createObjectOutputStream(writer);
oos.writeByte(1);
oos.writeByte(-1);
oos.writeByte(Byte.MIN_VALUE);
oos.writeShort(1);
oos.writeShort(-1);
oos.writeShort(Short.MIN_VALUE);
oos.close();

final String expectedXml = ""
+ "<object-stream>\n"
+ " <byte>1</byte>\n"
+ " <byte>-1</byte>\n"
+ " <byte>-128</byte>\n"
+ " <short>1</short>\n"
+ " <short>-1</short>\n"
+ " <short>-32768</short>\n"
+ "</object-stream>";

assertEquals(expectedXml, writer.toString());

final ObjectInputStream ois = xstream.createObjectInputStream(new StringReader(writer.toString()));
assertEquals(1, ois.readUnsignedByte());
assertEquals(255, ois.readUnsignedByte());
assertEquals(128, ois.readUnsignedByte());
assertEquals(1, ois.readUnsignedShort());
assertEquals(65535, ois.readUnsignedShort());
assertEquals(32768, ois.readUnsignedShort());

ois.close();
}
}

0 comments on commit 3305b44

Please sign in to comment.