use of com.rabbitmq.stream.impl.Client.CompressedEncodedMessageBatch in project rabbitmq-stream-java-client by rabbitmq.
the class CompressionCodecsTest method compressedEncodedMessageBatch.
@ParameterizedTest
@MethodSource
void compressedEncodedMessageBatch(CompressionCodec compressionCodec, CompressionCodec decompressionCodec) throws IOException {
assertThat(compressionCodec.code()).isEqualTo(decompressionCodec.code());
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
int messageCount = 100;
EncodedMessageBatch encodedMessageBatch = new CompressedEncodedMessageBatch(allocator, compressionCodec, messageCount);
List<EncodedMessage> encodedMessages = new ArrayList<>(messageCount);
IntStream.range(0, messageCount).forEach(i -> {
byte[] body = ("message " + i).getBytes(StandardCharsets.UTF_8);
EncodedMessage encodedMessage = new EncodedMessage(body.length, body);
encodedMessageBatch.add(encodedMessage);
encodedMessages.add(encodedMessage);
});
encodedMessageBatch.close();
int plainSize = encodedMessages.stream().mapToInt(EncodedMessage::getSize).sum() + encodedMessages.size() * 4;
int compressedSize = encodedMessageBatch.sizeInBytes();
ByteBuf destinationBb = allocator.buffer(compressedSize);
encodedMessageBatch.write(destinationBb);
assertThat(compressedSize).isLessThan(plainSize);
assertThat(destinationBb.writerIndex()).isEqualTo(compressedSize);
ByteBuf outBb = allocator.buffer(plainSize);
destinationBb.readerIndex(0);
InputStream inputStream = decompressionCodec.decompress(new ByteBufInputStream(destinationBb));
int n;
while (-1 != (n = inputStream.read())) {
outBb.writeByte(n);
}
List<EncodedMessage> decompressedMessages = new ArrayList<>(messageCount);
while (outBb.isReadable()) {
int size = outBb.readInt();
byte[] msg = new byte[size];
outBb.readBytes(msg);
decompressedMessages.add(new EncodedMessage(size, msg));
}
assertThat(decompressedMessages).hasSameSizeAs(encodedMessages);
IntStream.range(0, messageCount).forEach(i -> {
EncodedMessage originalMessage = encodedMessages.get(i);
EncodedMessage decompressedMessage = decompressedMessages.get(i);
assertThat(decompressedMessage.getSize()).isEqualTo(originalMessage.getSize());
assertThat(decompressedMessage.getData()).isEqualTo(originalMessage.getData());
});
destinationBb.release();
outBb.release();
}
Aggregations