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

Fix smalllong decoding #13

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public Long readLong(ProtonBuffer buffer, DecoderState state) throws DecodeExcep

switch (encodingCode) {
case EncodingCodes.SMALLLONG:
return (long) buffer.readByte() & 0xff;
return (long) buffer.readByte();
case EncodingCodes.LONG:
return buffer.readLong();
case EncodingCodes.NULL:
Expand All @@ -637,7 +637,7 @@ public long readLong(ProtonBuffer buffer, DecoderState state, long defaultValue)

switch (encodingCode) {
case EncodingCodes.SMALLLONG:
return (long) buffer.readByte() & 0xff;
return buffer.readByte();
case EncodingCodes.LONG:
return buffer.readLong();
case EncodingCodes.NULL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ public Long readLong(InputStream stream, StreamDecoderState state) throws Decode

switch (encodingCode) {
case EncodingCodes.SMALLLONG:
return Long.valueOf(ProtonStreamUtils.readByte(stream) & 0xffl);
return (long) ProtonStreamUtils.readByte(stream);
case EncodingCodes.LONG:
return ProtonStreamUtils.readLong(stream);
case EncodingCodes.NULL:
Expand All @@ -661,7 +661,7 @@ public long readLong(InputStream stream, StreamDecoderState state, long defaultV

switch (encodingCode) {
case EncodingCodes.SMALLLONG:
return ProtonStreamUtils.readByte(stream) & 0xffl;
return ProtonStreamUtils.readByte(stream);
case EncodingCodes.LONG:
return ProtonStreamUtils.readLong(stream);
case EncodingCodes.NULL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class Long8TypeDecoder extends LongTypeDecoder {

@Override
public Long readValue(ProtonBuffer buffer, DecoderState state) throws DecodeException {
return (long) buffer.readByte() & 0xff;
return (long) buffer.readByte();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public void testTypeFromEncodingCode(boolean fromStream) throws IOException {
buffer.writeLong(44);
buffer.writeByte(EncodingCodes.SMALLLONG);
buffer.writeByte((byte) 43);
buffer.writeByte(EncodingCodes.SMALLLONG);
buffer.writeByte((byte) -1);
buffer.writeByte(EncodingCodes.NULL);
buffer.writeByte(EncodingCodes.NULL);

Expand All @@ -122,12 +124,14 @@ public void testTypeFromEncodingCode(boolean fromStream) throws IOException {
assertEquals(42, streamDecoder.readLong(stream, streamDecoderState).intValue());
assertEquals(44, streamDecoder.readLong(stream, streamDecoderState, 42));
assertEquals(43, streamDecoder.readLong(stream, streamDecoderState, 42));
assertEquals(-1, streamDecoder.readLong(stream, streamDecoderState, 42));
assertNull(streamDecoder.readLong(stream, streamDecoderState));
assertEquals(42, streamDecoder.readLong(stream, streamDecoderState, 42l));
} else {
assertEquals(42, decoder.readLong(buffer, decoderState).intValue());
assertEquals(44, decoder.readLong(buffer, decoderState, 42));
assertEquals(43, decoder.readLong(buffer, decoderState, 42));
assertEquals(-1, decoder.readLong(buffer, decoderState, 42));
assertNull(decoder.readLong(buffer, decoderState));
assertEquals(42, decoder.readLong(buffer, decoderState, 42l));
}
Expand Down Expand Up @@ -183,13 +187,13 @@ private void testReadLongFromEncodingCodeSmallLong(boolean fromStream) throws IO
ProtonBuffer buffer = ProtonBufferAllocator.defaultAllocator().allocate();

buffer.writeByte(EncodingCodes.SMALLLONG);
buffer.writeByte((byte) 42);
buffer.writeByte((byte) -42);

if (fromStream) {
InputStream stream = new ProtonBufferInputStream(buffer);
assertEquals(42l, streamDecoder.readLong(stream, streamDecoderState).longValue());
assertEquals(-42l, streamDecoder.readLong(stream, streamDecoderState).longValue());
} else {
assertEquals(42l, decoder.readLong(buffer, decoderState).longValue());
assertEquals(-42l, decoder.readLong(buffer, decoderState).longValue());
}
}

Expand Down Expand Up @@ -360,14 +364,14 @@ public void doTestReadLongArray(byte encoding, boolean fromStream) throws IOExce
buffer.writeInt(2); // Count
buffer.writeByte(EncodingCodes.LONG);
buffer.writeLong(1l); // [0]
buffer.writeLong(2l); // [1]
buffer.writeLong(-2l); // [1]
} else if (encoding == EncodingCodes.SMALLLONG) {
buffer.writeByte(EncodingCodes.ARRAY32);
buffer.writeInt(11); // Size
buffer.writeInt(2); // Count
buffer.writeByte(EncodingCodes.SMALLLONG);
buffer.writeByte((byte) 1); // [0]
buffer.writeByte((byte) 2); // [1]
buffer.writeByte((byte) -2); // [1]
}

final Object result;
Expand All @@ -386,7 +390,7 @@ public void doTestReadLongArray(byte encoding, boolean fromStream) throws IOExce

assertEquals(2, array.length);
assertEquals(1, array[0]);
assertEquals(2, array[1]);
assertEquals(-2, array[1]);
}

@Test
Expand Down