use of org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream in project incubator-pulsar by apache.
the class ByteBufCodedInputStreamTest method testByteBufCondedInputStreamTest.
@Test
public void testByteBufCondedInputStreamTest() throws IOException {
ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer("Test-Message".getBytes()));
assertTrue(inputStream.skipField(WireFormat.WIRETYPE_VARINT));
assertTrue(inputStream.skipField(WireFormat.WIRETYPE_FIXED64));
assertFalse(inputStream.skipField(WireFormat.WIRETYPE_END_GROUP));
inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer("1000".getBytes()));
assertTrue(inputStream.skipField(WireFormat.WIRETYPE_FIXED32));
try {
inputStream.skipField(WireFormat.WIRETYPE_START_GROUP);
fail("Should not happend");
} catch (Exception e) {
// pass
}
try {
assertTrue(inputStream.skipField(-1));
fail("Should not happend");
} catch (Exception e) {
// pass
}
try {
assertTrue(inputStream.skipField(WireFormat.WIRETYPE_LENGTH_DELIMITED));
fail("Should not happend");
} catch (Exception e) {
// pass
}
try {
inputStream.skipRawBytes(-1);
fail("Should not happend");
} catch (InvalidProtocolBufferException e) {
// pass
}
try {
inputStream.skipRawBytes(10);
fail("Should not happend");
} catch (InvalidProtocolBufferException e) {
// pass
}
}
use of org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream in project incubator-pulsar by apache.
the class MessageIdImpl method fromByteArray.
// / Serialization
public static MessageId fromByteArray(byte[] data) throws IOException {
checkNotNull(data);
ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer(data, 0, data.length));
PulsarApi.MessageIdData.Builder builder = PulsarApi.MessageIdData.newBuilder();
PulsarApi.MessageIdData idData;
try {
idData = builder.mergeFrom(inputStream, null).build();
} catch (UninitializedMessageException e) {
throw new IOException(e);
}
MessageIdImpl messageId;
if (idData.hasBatchIndex()) {
messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(), idData.getBatchIndex());
} else {
messageId = new MessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition());
}
inputStream.recycle();
builder.recycle();
idData.recycle();
return messageId;
}
use of org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream in project incubator-pulsar by apache.
the class RawMessageImpl method deserializeFrom.
public static RawMessage deserializeFrom(ByteBuf buffer) {
try {
int idSize = buffer.readInt();
int writerIndex = buffer.writerIndex();
buffer.writerIndex(buffer.readerIndex() + idSize);
ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer);
MessageIdData.Builder builder = MessageIdData.newBuilder();
MessageIdData id = builder.mergeFrom(stream, null).build();
buffer.writerIndex(writerIndex);
builder.recycle();
int payloadAndMetadataSize = buffer.readInt();
ByteBuf metadataAndPayload = buffer.slice(buffer.readerIndex(), payloadAndMetadataSize);
return new RawMessageImpl(id, metadataAndPayload);
} catch (IOException e) {
// This is in-memory deserialization, should not fail
log.error("IO exception deserializing ByteBuf (this shouldn't happen as operation is in-memory)", e);
throw new RuntimeException(e);
}
}
Aggregations