Search in sources :

Example 6 with ByteBufCodedInputStream

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
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteBufCodedInputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 7 with ByteBufCodedInputStream

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;
}
Also used : UninitializedMessageException(com.google.protobuf.UninitializedMessageException) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) ByteBufCodedInputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream) PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) IOException(java.io.IOException)

Example 8 with ByteBufCodedInputStream

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);
    }
}
Also used : MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) ByteBufCodedInputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBufCodedInputStream (org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream)8 ByteBuf (io.netty.buffer.ByteBuf)5 IOException (java.io.IOException)4 PulsarApi (org.apache.pulsar.common.api.proto.PulsarApi)2 BaseCommand (org.apache.pulsar.common.api.proto.PulsarApi.BaseCommand)2 MessageIdData (org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData)2 Test (org.testng.annotations.Test)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 UninitializedMessageException (com.google.protobuf.UninitializedMessageException)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 InetSocketAddress (java.net.InetSocketAddress)1 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)1 PulsarService (org.apache.pulsar.broker.PulsarService)1 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)1 MockedPulsarServiceBaseTest.createMockZooKeeper (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper)1 ConfigurationCacheService (org.apache.pulsar.broker.cache.ConfigurationCacheService)1 LocalZooKeeperCacheService (org.apache.pulsar.broker.cache.LocalZooKeeperCacheService)1 NamespaceService (org.apache.pulsar.broker.namespace.NamespaceService)1 MessageMetadata (org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata)1 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)1