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(); }
@Override public ByteBuf serialize() { ByteBuf headersAndPayload = this.headersAndPayload.slice(); // Format: [IdSize][Id][PayloadAndMetadataSize][PayloadAndMetadata] int idSize = id.getSerializedSize(); int headerSize = 4 /* IdSize */ + idSize + 4 /* PayloadAndMetadataSize */; int totalSize = headerSize + headersAndPayload.readableBytes(); ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(totalSize); buf.writeInt(idSize); try { ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(buf); id.writeTo(outStream); outStream.recycle(); } catch (IOException e) { // This is in-memory serialization, should not fail log.error("IO exception serializing to ByteBuf (this shouldn't happen as operation is in-memory)", e); throw new RuntimeException(e); } buf.writeInt(headersAndPayload.readableBytes()); buf.writeBytes(headersAndPayload); return buf; }
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(); }