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

Getting strange results on CustomObjectInputStream.readUnsignedByte() #105

Closed
Shnitzelil opened this issue Jan 17, 2018 · 5 comments

Comments

@Shnitzelil
Copy link

commented Jan 17, 2018

I getting strange results on this method
so, I wrote tiny code and please review it..

byte[] bytes = {3, 6, 8,-3, -6, -8};
for (byte currentByte : bytes) {
System.out.println("currentByte + Byte.MAX_VALUE = " + (currentByte < 0 ? currentByte + Byte.MAX_VALUE : currentByte));
System.out.println("Byte.toUnsignedInt(currentByte) = " + (Byte.toUnsignedInt(currentByte)));
}

The results are

currentByte + Byte.MAX_VALUE = 3
Byte.toUnsignedInt(currentByte) = 3
currentByte + Byte.MAX_VALUE = 6
Byte.toUnsignedInt(currentByte) = 6
currentByte + Byte.MAX_VALUE = 8
Byte.toUnsignedInt(currentByte) = 8
currentByte + Byte.MAX_VALUE = 124
Byte.toUnsignedInt(currentByte) = 253
currentByte + Byte.MAX_VALUE = 121
Byte.toUnsignedInt(currentByte) = 250
currentByte + Byte.MAX_VALUE = 119
Byte.toUnsignedInt(currentByte) = 248

Do I miss anything?

@joehni joehni self-assigned this Jan 17, 2018
@joehni joehni added the question label Jan 17, 2018
@joehni

This comment has been minimized.

Copy link
Member

commented Jan 17, 2018

Sorry, but I do not see a relation to XStream.

@Shnitzelil

This comment has been minimized.

Copy link
Author

commented Jan 17, 2018

@joehni the CustomObjectInputStream is xstream class.
And I'm trying to use it in my project and the return value is not as expected.
So, I checked it with Java 8 Byte.toUnsignedIn() implementation and got the expected results.

WDYT?

@joehni

This comment has been minimized.

Copy link
Member

commented Jan 18, 2018

Still, you example is not really related. XStream mimics the serialization of the JDK. So, you may create an example where XStream produces after deserialization a different object compared to one produced by the JDK. If the JDK behaves the same, XStream is definitely correct and you face a general problem in Java handling unsigned values, but not a special one of XStream.

@joehni joehni added bug and removed question labels Feb 20, 2018
@joehni

This comment has been minimized.

Copy link
Member

commented Feb 20, 2018

I wrote a unit test that made the mistake obvious. Sorry, that I did not recognize that your example above used the implementation that was present in XStream. Therefore I missed the relation to XStream itself.

@joehni joehni closed this in 3305b44 Feb 20, 2018
joehni added a commit that referenced this issue Feb 20, 2018
@joehni joehni added this to the 1.4.x milestone Feb 20, 2018
@Shnitzelil

This comment has been minimized.

Copy link
Author

commented Feb 20, 2018

@joehni joehni modified the milestones: 1.4.x, 1.4.11 Oct 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.