Example usage for io.netty.buffer ByteBuf resetWriterIndex

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

Introduction

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

Prototype

public abstract ByteBuf resetWriterIndex();

Source Link

Document

Repositions the current writerIndex to the marked writerIndex in this buffer.

Usage

From source file:org.restcomm.protocols.ss7.m3ua.impl.message.M3UAMessageImpl.java

License:Open Source License

public void encode(ByteBuf byteBuf) {
    byteBuf.writeByte(1);/*from   w  w w  .jav a  2 s .  co  m*/
    byteBuf.writeByte(0);
    byteBuf.writeByte(messageClass);
    byteBuf.writeByte(messageType);

    byteBuf.markWriterIndex();
    byteBuf.writeInt(8);
    int currIndex = byteBuf.writerIndex();

    encodeParams(byteBuf);

    int newIndex = byteBuf.writerIndex();
    byteBuf.resetWriterIndex();
    byteBuf.writeInt(newIndex - currIndex + 8);
    byteBuf.writerIndex(newIndex);
}

From source file:qunar.tc.qmq.protocol.MessagesPayloadHolder.java

License:Apache License

private void serializeMessage(BaseMessage message, ByteBuf out) {
    int crcIndex = out.writerIndex();
    // sizeof(bodyCrc<long>)
    out.ensureWritable(8);//from  ww w  . java  2 s . com
    out.writerIndex(crcIndex + 8);

    final int messageStart = out.writerIndex();

    // flag
    byte flag = 0;
    //?(1)?(0)?(1)?(0)?Tag
    flag = Flags.setDelay(flag, DelayUtil.isDelayMessage(message));

    //in avoid add tag after sendMessage
    Set<String> tags = new HashSet<>(message.getTags());
    flag = Flags.setTags(flag, hasTags(tags));

    out.writeByte(flag);

    // created time
    out.writeLong(message.getCreatedTime().getTime());
    if (Flags.isDelay(flag)) {
        out.writeLong(message.getScheduleReceiveTime().getTime());
    } else {
        // expired time
        out.writeLong(System.currentTimeMillis());
    }
    // subject
    PayloadHolderUtils.writeString(message.getSubject(), out);
    // message id
    PayloadHolderUtils.writeString(message.getMessageId(), out);

    writeTags(tags, out);

    out.markWriterIndex();
    // writerIndex + sizeof(bodyLength<int>)
    final int bodyStart = out.writerIndex() + 4;
    out.ensureWritable(4);
    out.writerIndex(bodyStart);

    serializeMap(message.getAttrs(), out);
    final int bodyEnd = out.writerIndex();

    final int messageEnd = out.writerIndex();

    final int bodyLen = bodyEnd - bodyStart;
    final int messageLength = bodyEnd - messageStart;

    // write body length
    out.resetWriterIndex();
    out.writeInt(bodyLen);

    // write message crc
    out.writerIndex(crcIndex);
    out.writeLong(messageCrc(out, messageStart, messageLength));

    out.writerIndex(messageEnd);
}

From source file:qunar.tc.qmq.store.IndexLog.java

License:Apache License

private void partialCopy(ByteBuffer src, ByteBuf to) {
    while (to.isWritable(Long.BYTES)) {
        src.mark();//  w  ww. ja v a2  s.  c  o  m
        to.markWriterIndex();
        if (!to.isWritable(Long.BYTES))
            break;
        to.writeLong(src.getLong());

        if (!to.isWritable(Long.BYTES)) {
            src.reset();
            to.resetWriterIndex();
            break;
        }
        to.writeLong(src.getLong());

        // subject
        if (!writeString(src, to))
            break;

        // msgId
        if (!writeString(src, to))
            break;
    }
}

From source file:qunar.tc.qmq.store.IndexLog.java

License:Apache License

private boolean writeString(ByteBuffer src, ByteBuf to) {
    short len = src.getShort();
    if (!to.isWritable(Short.BYTES + len)) {
        src.reset();/*from www. j  a  va  2  s .c  om*/
        to.resetWriterIndex();
        return false;
    }
    byte[] subject = new byte[len];
    src.get(subject);
    to.writeShort(len);
    to.writeBytes(subject);
    return true;
}

From source file:qunar.tc.qmq.sync.master.MessageIndexSyncWorker.java

License:Apache License

@Override
protected SegmentBuffer getSyncLog(SyncRequest syncRequest) {
    final long originalOffset = syncRequest.getMessageLogOffset();
    long startSyncOffset = originalOffset;

    long minMessageOffset = storage.getMinMessageOffset();
    if (startSyncOffset < minMessageOffset) {
        startSyncOffset = minMessageOffset;
        LOG.info("reset message log sync offset from {} to {}", originalOffset, startSyncOffset);
    }//from w  ww .ja  va2s.  c o  m

    try (MessageLogRecordVisitor visitor = storage.newMessageLogVisitor(startSyncOffset)) {
        LogSegment logSegment = null;
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(batchSize);
        long nextSyncOffset = startSyncOffset;
        try {
            for (int i = 0; i < MAX_SYNC_NUM; ++i) {
                LogVisitorRecord<MessageLogRecord> record = visitor.nextRecord();
                if (record.isNoMore()) {
                    nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
                    break;
                }

                if (!record.hasData()) {
                    nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
                    continue;
                }

                MessageLogRecord data = record.getData();
                logSegment = data.getLogSegment();

                if (!byteBuf.isWritable(Long.BYTES))
                    break;
                byteBuf.markWriterIndex();
                byteBuf.writeLong(data.getSequence());

                if (!byteBuf.isWritable(Long.BYTES)) {
                    byteBuf.resetWriterIndex();
                    break;
                }

                ByteBuffer body = data.getPayload();
                //skip flag
                body.get();
                long createTime = body.getLong();
                //skip expireTime
                body.getLong();

                //subject
                short len = body.getShort();
                byte[] subject = new byte[len];
                body.get(subject);

                //message id
                len = body.getShort();
                byte[] messageId = new byte[len];
                body.get(messageId);

                byteBuf.writeLong(createTime);

                if (!byteBuf.isWritable(Short.BYTES + subject.length)) {
                    byteBuf.resetWriterIndex();
                    break;
                }
                PayloadHolderUtils.writeString(subject, byteBuf);

                if (!byteBuf.isWritable(Short.BYTES + messageId.length)) {
                    byteBuf.resetWriterIndex();
                    break;
                }
                PayloadHolderUtils.writeString(messageId, byteBuf);

                nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
            }
        } finally {
            if (!byteBuf.isReadable()) {
                byteBuf.release();
            }
        }

        if (originalOffset == nextSyncOffset) {
            return null;
        }

        //FIXME: ????
        if (byteBuf.isReadable()) {
            return new ByteBufSegmentBuffer(nextSyncOffset, logSegment, byteBuf, byteBuf.readableBytes());
        } else {
            return new ByteBufSegmentBuffer(nextSyncOffset);
        }
    }
}