lerno/naga

RegularPacketWriter does not work with a header size of 4

GoogleCodeExporter opened this issue · 2 comments

What steps will reproduce the problem?
1. Use a RegularPacketWriter constructing it with a value of 4 to indicate 
4 header bytes to describe the length of the payload. 
2. Write any byte array of any length

What is the expected output? What do you see instead?
Expected it to write the packet asynchronously.  Instead it failed throwing 
an illegal argument exception: Payload size cannot be encoded into 4 
byte(s).

What version of the product are you using? On what operating system?
Version 2.0 on Windows Vista

Please provide any additional information below.

The problem is in the static method NIOUtils.getByteBufferFromPacketSize:

...
if (valueToEncode >> (headerSize * 8) > 0)
   throw new IllegalArgumentException(...);

In the case of specifying a 4 byte length in the header, headerSize will be 
4.  Per the JLS, if the type of the operand is int, then the shift amount 
should be treated as if it were specified mod 32.  Thus valueToEncode >> (4 
* 8) is equivalent to valueToEncode >> 0, which of course is always greater 
than zero for any size byte buffer... thus it always throws the exception.

For the case where the headerSize is 4, you don't need to check the size 
because there can't be a size that would violate this that would still fit 
into the int valueToEncode.

The quick hack fix of course is to cast valueToEncode to a long, and in 
this case the JLS specifies that the shift amount should be treated as if 
it were mode 64.

Original issue reported on code.google.com by stevemash on 25 Apr 2010 at 4:55

Naga 2.1 released, patching this issue.

Original comment by christof...@gmail.com on 25 Apr 2010 at 7:42

  • Changed state: Fixed
  • Added labels: ****
  • Removed labels: ****

Original comment by christof...@gmail.com on 25 Apr 2010 at 7:29

  • Changed state: Started
  • Added labels: ****
  • Removed labels: ****