use of org.apache.pulsar.common.api.ByteBufPair 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.api.ByteBufPair in project incubator-pulsar by apache.
the class ProducerImpl method stripChecksum.
/**
* Strips checksum from {@link OpSendMsg} command if present else ignore it.
*
* @param op
*/
private void stripChecksum(OpSendMsg op) {
int totalMsgBufSize = op.cmd.readableBytes();
ByteBufPair msg = op.cmd;
if (msg != null) {
ByteBuf headerFrame = msg.getFirst();
headerFrame.markReaderIndex();
try {
// skip [total-size]
headerFrame.skipBytes(4);
int cmdSize = (int) headerFrame.readUnsignedInt();
// verify if checksum present
headerFrame.skipBytes(cmdSize);
if (!hasChecksum(headerFrame)) {
return;
}
// [total-size] [cmd-length] [cmd-size]
int headerSize = 4 + 4 + cmdSize;
// [magic-number] [checksum-size]
int checksumSize = 4 + 2;
// [header-size] [checksum-size]
int checksumMark = (headerSize + checksumSize);
// metadataPayload = totalSize - checksumMark
int metaPayloadSize = (totalMsgBufSize - checksumMark);
// new total-size without checksum
int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize;
headerFrame.resetReaderIndex();
int headerFrameSize = headerFrame.readableBytes();
// rewrite new [total-size]
headerFrame.setInt(0, newTotalFrameSizeLength);
// sliced only
ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark);
// metadata
// set headerFrame write-index to overwrite metadata over checksum
headerFrame.writerIndex(headerSize);
metadata.readBytes(headerFrame, metadata.readableBytes());
// reduce capacity by removed checksum bytes
headerFrame.capacity(headerFrameSize - checksumSize);
} finally {
headerFrame.resetReaderIndex();
}
} else {
log.warn("[{}] Failed while casting {} into ByteBufPair", producerName, op.cmd.getClass().getName());
}
}
Aggregations