Example usage for io.netty.buffer ByteBuf writerIndex

List of usage examples for io.netty.buffer ByteBuf writerIndex

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writerIndex.

Prototype

public abstract int writerIndex();

Source Link

Document

Returns the writerIndex of this buffer.

Usage

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

public static MessageMetadata parseMessageMetadata(ByteBuf buffer) {
    try {/* ww  w  .ja  v a  2s . c  om*/
        // initially reader-index may point to start_of_checksum : increment reader-index to start_of_metadata to parse
        // metadata
        readChecksum(buffer);
        int metadataSize = (int) buffer.readUnsignedInt();

        int writerIndex = buffer.writerIndex();
        buffer.writerIndex(buffer.readerIndex() + metadataSize);
        ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer);
        MessageMetadata.Builder messageMetadataBuilder = MessageMetadata.newBuilder();
        MessageMetadata res = messageMetadataBuilder.mergeFrom(stream, null).build();
        buffer.writerIndex(writerIndex);
        messageMetadataBuilder.recycle();
        stream.recycle();
        return res;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

private static ByteBuf serializeCommandSendWithSize(BaseCommand.Builder cmdBuilder, ChecksumType checksumType,
        MessageMetadata msgMetadata, ByteBuf payload) {
    // / Wire format
    // [TOTAL_SIZE] [CMD_SIZE][CMD] [MAGIC_NUMBER][CHECKSUM] [METADATA_SIZE][METADATA] [PAYLOAD]

    BaseCommand cmd = cmdBuilder.build();
    int cmdSize = cmd.getSerializedSize();
    int msgMetadataSize = msgMetadata.getSerializedSize();
    int payloadSize = payload.readableBytes();
    int magicAndChecksumLength = ChecksumType.Crc32c.equals(checksumType) ? (2 + 4 /* magic + checksumLength*/)
            : 0;//from  w w  w.j av a  2  s  . co  m
    boolean includeChecksum = magicAndChecksumLength > 0;
    int headerContentSize = 4 + cmdSize + magicAndChecksumLength + 4 + msgMetadataSize; // cmdLength + cmdSize + magicLength +
    // checksumSize + msgMetadataLength +
    // msgMetadataSize
    int totalSize = headerContentSize + payloadSize;
    int headersSize = 4 + headerContentSize; // totalSize + headerLength
    int checksumReaderIndex = -1;

    ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize, headersSize);
    headers.writeInt(totalSize); // External frame

    try {
        // Write cmd
        headers.writeInt(cmdSize);

        ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
        cmd.writeTo(outStream);
        cmd.recycle();
        cmdBuilder.recycle();

        //Create checksum placeholder
        if (includeChecksum) {
            headers.writeShort(magicCrc32c);
            checksumReaderIndex = headers.writerIndex();
            headers.writerIndex(headers.writerIndex() + checksumSize); //skip 4 bytes of checksum
        }

        // Write metadata
        headers.writeInt(msgMetadataSize);
        msgMetadata.writeTo(outStream);
        outStream.recycle();
    } catch (IOException e) {
        // This is in-memory serialization, should not fail
        throw new RuntimeException(e);
    }

    ByteBuf command = DoubleByteBuf.get(headers, payload);

    // write checksum at created checksum-placeholder
    if (includeChecksum) {
        headers.markReaderIndex();
        headers.readerIndex(checksumReaderIndex + checksumSize);
        int metadataChecksum = computeChecksum(headers);
        int computedChecksum = resumeChecksum(metadataChecksum, payload);
        // set computed checksum
        headers.setInt(checksumReaderIndex, computedChecksum);
        headers.resetReaderIndex();
    }
    return command;
}

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

public static ByteBuf deSerializeSingleMessageInBatch(ByteBuf uncompressedPayload,
        PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, int index, int batchSize)
        throws IOException {
    int singleMetaSize = (int) uncompressedPayload.readUnsignedInt();
    int writerIndex = uncompressedPayload.writerIndex();
    int beginIndex = uncompressedPayload.readerIndex() + singleMetaSize;
    uncompressedPayload.writerIndex(beginIndex);
    ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(uncompressedPayload);
    PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.mergeFrom(stream, null)
            .build();/*from   w w w.  j  a v  a 2  s .com*/
    int singleMessagePayloadSize = singleMessageMetadata.getPayloadSize();

    uncompressedPayload.markReaderIndex();
    ByteBuf singleMessagePayload = uncompressedPayload.slice(uncompressedPayload.readerIndex(),
            singleMessagePayloadSize);
    singleMessagePayload.retain();
    uncompressedPayload.writerIndex(writerIndex);
    uncompressedPayload.resetReaderIndex();
    // reader now points to beginning of payload read; so move it past message payload just read
    if (index < batchSize) {
        uncompressedPayload.readerIndex(uncompressedPayload.readerIndex() + singleMessagePayloadSize);
    }
    return singleMessagePayload;
}

From source file:com.yahoo.pulsar.common.api.PulsarDecoder.java

License:Apache License

@Override
final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // Get a buffer that contains the full frame
    ByteBuf buffer = (ByteBuf) msg;
    BaseCommand cmd = null;//w  ww.j ava  2 s .  c  o  m
    BaseCommand.Builder cmdBuilder = null;

    try {
        // De-serialize the command
        int cmdSize = (int) buffer.readUnsignedInt();
        int writerIndex = buffer.writerIndex();
        buffer.writerIndex(buffer.readerIndex() + cmdSize);
        ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer);
        cmdBuilder = BaseCommand.newBuilder();
        cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build();
        buffer.writerIndex(writerIndex);

        cmdInputStream.recycle();

        if (log.isDebugEnabled()) {
            log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType());
        }

        messageReceived();

        switch (cmd.getType()) {
        case PARTITIONED_METADATA:
            checkArgument(cmd.hasPartitionMetadata());
            handlePartitionMetadataRequest(cmd.getPartitionMetadata());
            cmd.getPartitionMetadata().recycle();
            break;

        case PARTITIONED_METADATA_RESPONSE:
            checkArgument(cmd.hasPartitionMetadataResponse());
            handlePartitionResponse(cmd.getPartitionMetadataResponse());
            cmd.getPartitionMetadataResponse().recycle();
            break;

        case LOOKUP:
            checkArgument(cmd.hasLookupTopic());
            handleLookup(cmd.getLookupTopic());
            cmd.getLookupTopic().recycle();
            break;

        case LOOKUP_RESPONSE:
            checkArgument(cmd.hasLookupTopicResponse());
            handleLookupResponse(cmd.getLookupTopicResponse());
            cmd.getLookupTopicResponse().recycle();
            break;

        case ACK:
            checkArgument(cmd.hasAck());
            handleAck(cmd.getAck());
            cmd.getAck().getMessageId().recycle();
            cmd.getAck().recycle();
            break;

        case CLOSE_CONSUMER:
            checkArgument(cmd.hasCloseConsumer());
            handleCloseConsumer(cmd.getCloseConsumer());
            cmd.getCloseConsumer().recycle();
            break;

        case CLOSE_PRODUCER:
            checkArgument(cmd.hasCloseProducer());
            handleCloseProducer(cmd.getCloseProducer());
            cmd.getCloseProducer().recycle();
            break;

        case CONNECT:
            checkArgument(cmd.hasConnect());
            handleConnect(cmd.getConnect());
            cmd.getConnect().recycle();
            break;
        case CONNECTED:
            checkArgument(cmd.hasConnected());
            handleConnected(cmd.getConnected());
            cmd.getConnected().recycle();
            break;

        case ERROR:
            checkArgument(cmd.hasError());
            handleError(cmd.getError());
            cmd.getError().recycle();
            break;

        case FLOW:
            checkArgument(cmd.hasFlow());
            handleFlow(cmd.getFlow());
            cmd.getFlow().recycle();
            break;

        case MESSAGE: {
            checkArgument(cmd.hasMessage());
            handleMessage(cmd.getMessage(), buffer);
            cmd.getMessage().recycle();
            break;
        }
        case PRODUCER:
            checkArgument(cmd.hasProducer());
            handleProducer(cmd.getProducer());
            cmd.getProducer().recycle();
            break;

        case SEND: {
            checkArgument(cmd.hasSend());

            // Store a buffer marking the content + headers
            ByteBuf headersAndPayload = buffer.markReaderIndex();
            handleSend(cmd.getSend(), headersAndPayload);
            cmd.getSend().recycle();
            break;
        }
        case SEND_ERROR:
            checkArgument(cmd.hasSendError());
            handleSendError(cmd.getSendError());
            cmd.getSendError().recycle();
            break;

        case SEND_RECEIPT:
            checkArgument(cmd.hasSendReceipt());
            handleSendReceipt(cmd.getSendReceipt());
            cmd.getSendReceipt().recycle();
            break;

        case SUBSCRIBE:
            checkArgument(cmd.hasSubscribe());
            handleSubscribe(cmd.getSubscribe());
            cmd.getSubscribe().recycle();
            break;

        case SUCCESS:
            checkArgument(cmd.hasSuccess());
            handleSuccess(cmd.getSuccess());
            cmd.getSuccess().recycle();
            break;

        case PRODUCER_SUCCESS:
            checkArgument(cmd.hasProducerSuccess());
            handleProducerSuccess(cmd.getProducerSuccess());
            cmd.getProducerSuccess().recycle();
            break;

        case UNSUBSCRIBE:
            checkArgument(cmd.hasUnsubscribe());
            handleUnsubscribe(cmd.getUnsubscribe());
            cmd.getUnsubscribe().recycle();
            break;

        case PING:
            checkArgument(cmd.hasPing());
            handlePing(cmd.getPing());
            cmd.getPing().recycle();
            break;

        case PONG:
            checkArgument(cmd.hasPong());
            handlePong(cmd.getPong());
            cmd.getPong().recycle();
            break;

        case REDELIVER_UNACKNOWLEDGED_MESSAGES:
            checkArgument(cmd.hasRedeliverUnacknowledgedMessages());
            handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages());
            cmd.getRedeliverUnacknowledgedMessages().recycle();
            break;
        }

    } finally {
        if (cmdBuilder != null) {
            cmdBuilder.recycle();
        }

        if (cmd != null) {
            cmd.recycle();
        }

        buffer.release();
    }
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

private void deflate(ByteBuf out) {
    int numBytes;
    do {/*  ww  w .j  a va 2  s.com*/
        int writerIndex = out.writerIndex();
        numBytes = deflater.deflate(out.array(), out.arrayOffset() + writerIndex, out.writableBytes(),
                Deflater.SYNC_FLUSH);
        out.writerIndex(writerIndex + numBytes);
    } while (numBytes > 0);
}

From source file:com.yahoo.pulsar.utils.NumberFormat.java

License:Apache License

static void format(ByteBuf out, long num) {
    if (num == 0) {
        out.writeByte('0');
        return;/*from  w  w w .  j  av  a 2s. c  om*/
    }

    // Long.MIN_VALUE needs special handling since abs(Long.MIN_VALUE) = abs(Long.MAX_VALUE) + 1
    boolean encounteredMinValue = (num == Long.MIN_VALUE);
    if (num < 0) {
        out.writeByte('-');
        num += encounteredMinValue ? 1 : 0;
        num *= -1;
    }

    // Putting the number in bytebuf in reverse order
    int start = out.writerIndex();
    formatHelper(out, num);
    int end = out.writerIndex();

    if (encounteredMinValue) {
        out.setByte(start, out.getByte(start) + 1);
    }

    // Reversing the digits
    end--;
    for (int i = 0; i <= (end - start) / 2; i++) {
        byte tmp = out.getByte(end - i);
        out.setByte(end - i, out.getByte(start + i));
        out.setByte(start + i, tmp);
    }
}

From source file:com.yea.remote.netty.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null) {
        throw new Exception("The encode message is null");
    }//from  ww w.  j a v a2  s .  c  o m
    ISerializer serializer = serializePool.borrow();
    try {
        long basedate = new Date().getTime();
        sendBuf.writeInt((msg.getHeader().getCrcCode()));// 4
        sendBuf.writeInt((msg.getHeader().getLength()));// 4Header+BodyHeader18
        sendBuf.writeBytes((msg.getHeader().getSessionID()));// 16
        sendBuf.writeByte((msg.getHeader().getType()));// 1
        sendBuf.writeByte((msg.getHeader().getPriority()));// 1
        sendBuf.writeByte((msg.getHeader().getResult()));// 1
        if (compressionAlgorithm != null && compressionAlgorithm.code() > 0) {
            // 
            serializer.setCompress(new Compress().setCompressionAlgorithm(compressionAlgorithm.algorithm()));
            sendBuf.writeByte(compressionAlgorithm.ordinal());
        } else {
            sendBuf.writeByte(RemoteConstants.CompressionAlgorithm.NONE.ordinal());
        }
        sendBuf.writeLong(basedate);// 8??
        if (msg.getHeader().getAttachment() != null && !msg.getHeader().getAttachment().isEmpty()) {
            sendBuf.writeByte(msg.getHeader().getAttachment().size());
            Map<String, Number> mapDateType = new HashMap<String, Number>();// Date
            Map<String, String> mapStringType = new HashMap<String, String>();// String

            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(new byte[1]);// 1?Date?
            byte[] keyArray = null;
            byte[] valueArray = null;
            for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
                if (param.getValue() instanceof Date) {
                    //Date???byte
                    long time = basedate - ((Date) param.getValue()).getTime();
                    if (time > Integer.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Long(time));
                    } else if (time > Short.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Integer((int) time));
                    } else if (time > Byte.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Short((short) time));
                    } else {
                        mapDateType.put(param.getKey(), new Byte((byte) time));
                    }
                } else if (param.getValue() instanceof String) {
                    //??getBytes()?
                    mapStringType.put(param.getKey(), (String) param.getValue());
                } else {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = serializer.serialize(param.getValue());
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
            sendBuf.setByte(lengthPos,
                    msg.getHeader().getAttachment().size() - mapDateType.size() - mapStringType.size());

            if (mapDateType.isEmpty()) {
                sendBuf.writeByte(0);// 1Date?0
            } else {
                sendBuf.writeByte(mapDateType.size());// 1Date?
                for (Map.Entry<String, Number> param : mapDateType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    if (param.getValue() instanceof Long) {
                        sendBuf.writeByte(8);
                        sendBuf.writeLong((Long) param.getValue());
                    } else if (param.getValue() instanceof Integer) {
                        sendBuf.writeByte(4);
                        sendBuf.writeInt((Integer) param.getValue());
                    } else if (param.getValue() instanceof Short) {
                        sendBuf.writeByte(2);
                        sendBuf.writeShort((Short) param.getValue());
                    } else {
                        sendBuf.writeByte(1);
                        sendBuf.writeByte((Byte) param.getValue());
                    }
                }
            }

            if (mapStringType.isEmpty()) {
                sendBuf.writeByte(0);// 1String?0
            } else {
                sendBuf.writeByte(mapStringType.size());// 1String?
                for (Map.Entry<String, String> param : mapStringType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = param.getValue().getBytes("ISO-8859-1");
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
        } else {
            sendBuf.writeByte(0);// 20
        }

        if (msg.getBody() != null) {
            byte[] objArray = serializer.serialize(msg.getBody());
            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(LENGTH_PLACEHOLDER);// 4Body
            sendBuf.writeBytes(objArray);
            sendBuf.setInt(lengthPos, sendBuf.writerIndex() - lengthPos - 4);// Body
        } else {
            sendBuf.writeInt(0);
        }
        sendBuf.setInt(4, sendBuf.readableBytes() - 8);// Header+Body
    } finally {
        serializePool.restore(serializer);
    }
}

From source file:com.zaradai.distributor.messaging.netty.handler.MessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
    int index = out.writerIndex();
    // reserve space for length
    out.writeInt(0);/*from  w ww  . j  ava  2 s.  c om*/
    // get the slot start for the message bytes
    int start = out.writerIndex();
    // write the message
    writeHeader(out);
    writeSource(msg, out);
    writeEvent(msg, out);
    // get slot end index;
    int end = out.writerIndex();
    // write out the bytes length
    out.setInt(index, (end - start));
}

From source file:com.zaradai.distributor.messaging.netty.handler.MessagingHandshake.java

License:Apache License

@Override
protected void clientWrite(ByteBuf out) {
    int idx = out.writerIndex();
    // reserve space for length
    out.writeInt(0);/* w w w.  ja v  a 2 s. c o m*/
    // get slot for the bytes
    int start = out.writerIndex();
    // write protocol header
    writeProtocolHeader(out);
    writeAddress(out);
    // note end of the object slot
    int end = out.writerIndex();
    // write length
    out.setInt(idx, (end - start));
}

From source file:com.zz.learning.netty5.chap12.codec.MarshallingEncoder.java

License:Apache License

/**
 *  ?//  www  . j  av a  2  s.  c o  m
 * @param msg
 * @param out
 * @throws Exception
 */
protected void encode(Object msg, ByteBuf out) throws Exception {
    try {
        int lengthPos = out.writerIndex(); //??
        out.writeBytes(LENGTH_PLACEHOLDER); // msg??
        ChannelBufferByteOutput output = new ChannelBufferByteOutput(out);
        marshaller.start(output);
        marshaller.writeObject(msg);
        marshaller.finish();
        out.setInt(lengthPos, out.writerIndex() - lengthPos - 4); //msg
    } finally {
        marshaller.close();
    }
}