use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.
the class CommandsTest method computeChecksum.
private int computeChecksum(MessageMetadata msgMetadata, ByteBuf compressedPayload) throws IOException {
int metadataSize = msgMetadata.getSerializedSize();
int metadataFrameSize = 4 + metadataSize;
ByteBuf metaPayloadFrame = PooledByteBufAllocator.DEFAULT.buffer(metadataFrameSize, metadataFrameSize);
ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(metaPayloadFrame);
metaPayloadFrame.writeInt(metadataSize);
msgMetadata.writeTo(outStream);
ByteBuf payload = compressedPayload.copy();
ByteBufPair metaPayloadBuf = ByteBufPair.get(metaPayloadFrame, payload);
int computedChecksum = Crc32cIntChecksum.computeChecksum(ByteBufPair.coalesce(metaPayloadBuf));
outStream.recycle();
metaPayloadBuf.release();
return computedChecksum;
}
use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.
the class PersistentMessageFinderTest method createMessageWrittenToLedger.
public static byte[] createMessageWrittenToLedger(String msg) throws Exception {
PulsarApi.MessageMetadata.Builder messageMetadataBuilder = PulsarApi.MessageMetadata.newBuilder();
messageMetadataBuilder.setPublishTime(System.currentTimeMillis());
messageMetadataBuilder.setProducerName("createMessageWrittenToLedger");
messageMetadataBuilder.setSequenceId(1);
PulsarApi.MessageMetadata messageMetadata = messageMetadataBuilder.build();
ByteBuf data = UnpooledByteBufAllocator.DEFAULT.heapBuffer().writeBytes(msg.getBytes());
int msgMetadataSize = messageMetadata.getSerializedSize();
int payloadSize = data.readableBytes();
int totalSize = 4 + msgMetadataSize + payloadSize;
ByteBuf headers = PooledByteBufAllocator.DEFAULT.heapBuffer(totalSize, totalSize);
ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
headers.writeInt(msgMetadataSize);
messageMetadata.writeTo(outStream);
ByteBuf headersAndPayload = ByteBufPair.coalesce(ByteBufPair.get(headers, data));
byte[] byteMessage = headersAndPayload.nioBuffer().array();
headersAndPayload.release();
return byteMessage;
}
use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.
the class ByteBufCodedInputStreamTest method testWritingDouble.
@Test
public void testWritingDouble() throws IOException {
ByteBuf buf = Unpooled.buffer();
buf.clear();
ByteBufCodedOutputStream outputStream = ByteBufCodedOutputStream.get(buf);
outputStream.writeDouble(12, 23d);
outputStream.writeDouble(15, 13.13d);
outputStream.writeDouble(1, -0.003d);
ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(buf);
assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 12);
assertEquals(inputStream.readDouble(), 23d);
assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 15);
assertEquals(inputStream.readDouble(), 13.13d);
assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 1);
assertEquals(inputStream.readDouble(), -0.003d);
}
use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.
the class MessageIdImpl method toByteArray.
// batchIndex is -1 if message is non-batched message and has the batchIndex for a batch message
protected byte[] toByteArray(int batchIndex) {
MessageIdData.Builder builder = MessageIdData.newBuilder();
builder.setLedgerId(ledgerId);
builder.setEntryId(entryId);
if (partitionIndex >= 0) {
builder.setPartition(partitionIndex);
}
if (batchIndex != -1) {
builder.setBatchIndex(batchIndex);
}
MessageIdData msgId = builder.build();
int size = msgId.getSerializedSize();
ByteBuf serialized = Unpooled.buffer(size, size);
ByteBufCodedOutputStream stream = ByteBufCodedOutputStream.get(serialized);
try {
msgId.writeTo(stream);
} catch (IOException e) {
// This is in-memory serialization, should not fail
throw new RuntimeException(e);
}
msgId.recycle();
builder.recycle();
stream.recycle();
return serialized.array();
}
use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.
the class Commands method serializeSingleMessageInBatchWithPayload.
public static ByteBuf serializeSingleMessageInBatchWithPayload(PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, ByteBuf payload, ByteBuf batchBuffer) {
int payLoadSize = payload.readableBytes();
PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.setPayloadSize(payLoadSize).build();
// serialize meta-data size, meta-data and payload for single message in batch
int singleMsgMetadataSize = singleMessageMetadata.getSerializedSize();
try {
batchBuffer.writeInt(singleMsgMetadataSize);
ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(batchBuffer);
singleMessageMetadata.writeTo(outStream);
singleMessageMetadata.recycle();
outStream.recycle();
} catch (IOException e) {
throw new RuntimeException(e);
}
return batchBuffer.writeBytes(payload);
}
Aggregations