Skip to content
Closed
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 @@ -20,6 +20,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import io.netty.buffer.ByteBuf;
import org.roaringbitmap.RoaringBitmap;
Expand All @@ -41,6 +42,7 @@ public static void encode(ByteBuf buf, String s) {

public static String decode(ByteBuf buf) {
int length = buf.readInt();
Objects.checkFromIndexSize(0, length, buf.readableBytes());
byte[] bytes = new byte[length];
buf.readBytes(bytes);
return new String(bytes, StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ public void testRoaringBitmapEncodeShouldFailWhenBufferIsSmall() {
() -> Encoders.Bitmaps.encode(buf, bitmap));
}

@Test
public void testStringsEncodeDecode() {
String s = "spark";
ByteBuf buf = Unpooled.buffer(Encoders.Strings.encodedLength(s));
Encoders.Strings.encode(buf, s);
assertEquals(s, Encoders.Strings.decode(buf));
}

@Test
public void testStringsDecodeShouldFailWhenLengthIsNegative() {
ByteBuf buf = Unpooled.buffer();
buf.writeInt(-1);
assertThrows(IndexOutOfBoundsException.class, () -> Encoders.Strings.decode(buf));
}

@Test
public void testStringsDecodeShouldFailWhenLengthExceedsReadableBytes() {
ByteBuf buf = Unpooled.buffer();
buf.writeInt(Integer.MAX_VALUE);
assertThrows(IndexOutOfBoundsException.class, () -> Encoders.Strings.decode(buf));
}

@Test
public void testBitmapArraysEncodeDecode() {
RoaringBitmap[] bitmaps = new RoaringBitmap[] {
Expand Down