Skip to content

Commit

Permalink
Fix Buffer.getShort()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaswolf committed Aug 18, 2023
1 parent 786fd51 commit c411efb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ public byte getByte() {
public short getShort() {
ensureAvailable(Short.BYTES);
getRawBytes(workBuf, 0, Short.BYTES);
short v = (short) ((workBuf[1] << Byte.SIZE) & 0xFF00);
v |= (short) (workBuf[0] & 0xF);
short v = (short) ((workBuf[0] << Byte.SIZE) & 0xFF00);
v |= (short) (workBuf[1] & 0xFF);
return v;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,18 @@ public void testGetPublicKeyCorrupted() {
assertFalse(e instanceof OutOfMemoryError);
assertEquals(8, buffer.array().length);
}

@Test
public void testShortPositive() {
ByteArrayBuffer buffer = new ByteArrayBuffer(2);
buffer.putShort(261);
assertEquals(261, buffer.getShort());
}

@Test
public void testShortNegative() {
ByteArrayBuffer buffer = new ByteArrayBuffer(2);
buffer.putShort(-2);
assertEquals(-2, buffer.getShort());
}
}

0 comments on commit c411efb

Please sign in to comment.