Java Code Examples for io.netty.buffer.ByteBuf#markWriterIndex()

The following examples show how to use io.netty.buffer.ByteBuf#markWriterIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnconnectedSendService.java    From ethernet-ip with Apache License 2.0 6 votes vote down vote up
private int encodeEmbeddedService(ByteBuf buffer) {
    // length of embedded message
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeShort(0);

    // embedded message
    int messageStartIndex = buffer.writerIndex();
    service.encodeRequest(buffer);

    // go back and update length
    int bytesWritten = buffer.writerIndex() - messageStartIndex;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeShort(bytesWritten);
    buffer.resetWriterIndex();

    return bytesWritten;
}
 
Example 2
Source File: IndexLog.java    From qmq with Apache License 2.0 6 votes vote down vote up
private void partialCopy(ByteBuffer src, ByteBuf to) {
    while (to.isWritable(Long.BYTES)) {
        src.mark();
        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;
    }
}
 
Example 3
Source File: RntbdContextRequestEncoder.java    From azure-cosmosdb-java with MIT License 6 votes vote down vote up
/**
 * Encode an {@link RntbdContextRequest} message into a {@link ByteBuf}.
 * <p>
 * This method will be called for each written message that can be handled by this encoder.
 *
 * @param context the {@link ChannelHandlerContext} to which this {@link MessageToByteEncoder} belongs.
 * @param message the message to encode.
 * @param out the {@link ByteBuf} into which the encoded message will be written.
 *
 * @throws IllegalStateException is thrown if an error occurs.
 */
@Override
protected void encode(
    final ChannelHandlerContext context,
    final RntbdContextRequest message,
    final ByteBuf out) throws IllegalStateException {

    out.markWriterIndex();

    try {
        message.encode(out);
    } catch (final IllegalStateException error) {
        out.resetWriterIndex();
        throw error;
    }

    Logger.debug("{}: ENCODE COMPLETE: message={}", context.channel(), message);
}
 
Example 4
Source File: XorEncoderTest.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testEncode() throws Exception
{
	ByteBuf buf = Unpooled.buffer(1);
	buf.markWriterIndex();
	buf.writeByte(0xff);

	XorEncoder encoder = new XorEncoder();
	encoder.setKey((byte) 0x1);

	ByteBuf out = Unpooled.buffer(1);
	encoder.encode(null, buf, out);

	byte encoded = out.readByte();
	Assert.assertEquals((Byte) (byte) 0xfe, (Byte) encoded);
}
 
Example 5
Source File: LowCopyProtocolEncoder.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
private ByteBuf doEncodeRequest(JRequestPayload request) {
    byte sign = JProtocolHeader.toSign(request.serializerCode(), JProtocolHeader.REQUEST);
    long invokeId = request.invokeId();
    ByteBuf byteBuf = (ByteBuf) request.outputBuf().backingObject();
    int length = byteBuf.readableBytes();

    byteBuf.markWriterIndex();

    byteBuf.writerIndex(byteBuf.writerIndex() - length);

    byteBuf.writeShort(JProtocolHeader.MAGIC)
            .writeByte(sign)
            .writeByte(0x00)
            .writeLong(invokeId)
            .writeInt(length - JProtocolHeader.HEADER_SIZE);

    byteBuf.resetWriterIndex();

    return byteBuf;
}
 
Example 6
Source File: MultipleServicePacketService.java    From ethernet-ip with Apache License 2.0 6 votes vote down vote up
private void encode(ByteBuf buffer) {
    int serviceCount = currentServices.size();

    buffer.writeShort(serviceCount);

    int[] offsets = new int[serviceCount];
    int offsetsStartIndex = buffer.writerIndex();
    buffer.writeZero(serviceCount * 2);

    for (int i = 0; i < serviceCount; i++) {
        offsets[i] = buffer.writerIndex() - offsetsStartIndex + 2;
        currentServices.get(i).encodeRequest(buffer);
    }

    buffer.markWriterIndex();
    buffer.writerIndex(offsetsStartIndex);
    for (int offset : offsets) {
        buffer.writeShort(offset);
    }
    buffer.resetWriterIndex();
}
 
Example 7
Source File: JT808Encoder.java    From jt808-netty with MIT License 6 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, DataPacket msg, ByteBuf out) throws Exception {
    log.debug(msg.toString());
    ByteBuf bb = msg.toByteBufMsg();
    bb.markWriterIndex();//标记一下,先到前面去写覆盖的,然后回到标记写校验码
    short bodyLen = (short) (bb.readableBytes() - 12);//包体长度=总长度-头部长度
    short bodyProps = createDefaultMsgBodyProperty(bodyLen);
    //覆盖占用的4字节
    bb.writerIndex(0);
    bb.writeShort(msg.getHeader().getMsgId());
    bb.writeShort(bodyProps);
    bb.resetWriterIndex();
    bb.writeByte(JT808Util.XorSumBytes(bb));
    log.debug(">>>>> ip:{},hex:{}\n", ctx.channel().remoteAddress(), ByteBufUtil.hexDump(bb));
    ByteBuf escape = escape(bb);
    out.writeBytes(escape);
    ReferenceCountUtil.safeRelease(escape);
}
 
Example 8
Source File: UnconnectedDataItemResponse.java    From ethernet-ip with Apache License 2.0 6 votes vote down vote up
public static ByteBuf encode(UnconnectedDataItemResponse item, ByteBuf buffer) {
    buffer.writeShort(item.getTypeId());

    // Length placeholder...
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeShort(0);

    // Encode the encapsulated data...
    int dataStartIndex = buffer.writerIndex();
    buffer.writeBytes(item.getData());
    item.getData().release();

    // Go back and update the length.
    int bytesWritten = buffer.writerIndex() - dataStartIndex;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeShort(bytesWritten);
    buffer.resetWriterIndex();

    return buffer;
}
 
Example 9
Source File: UnconnectedDataItemRequest.java    From ethernet-ip with Apache License 2.0 6 votes vote down vote up
public static ByteBuf encode(UnconnectedDataItemRequest item, ByteBuf buffer) {
    buffer.writeShort(item.getTypeId());

    // Length placeholder...
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeShort(0);

    // Encode the encapsulated data...
    int dataStartIndex = buffer.writerIndex();
    item.getEncoder().accept(buffer);

    // Go back and update the length.
    int bytesWritten = buffer.writerIndex() - dataStartIndex;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeShort(bytesWritten);
    buffer.resetWriterIndex();

    return buffer;
}
 
Example 10
Source File: ConnectedDataItemResponse.java    From ethernet-ip with Apache License 2.0 6 votes vote down vote up
public static ByteBuf encode(ConnectedDataItemResponse item, ByteBuf buffer) {
    buffer.writeShort(item.getTypeId());

    // Length placeholder...
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeShort(0);

    // Encode the encapsulated data...
    int dataStartIndex = buffer.writerIndex();
    buffer.writeBytes(item.getData());
    item.getData().release();

    // Go back and update the length.
    int bytesWritten = buffer.writerIndex() - dataStartIndex;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeShort(bytesWritten);
    buffer.resetWriterIndex();

    return buffer;
}
 
Example 11
Source File: CompositeMetadataCodec.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
/**
 * Encode a custom mime type and a metadata value length into a newly allocated {@link ByteBuf}.
 *
 * <p>This larger representation encodes the mime type representation's length on a single byte,
 * then the representation itself, then the unsigned metadata value length on 3 additional bytes.
 *
 * @param allocator the {@link ByteBufAllocator} to use to create the buffer.
 * @param customMime a custom mime type to encode.
 * @param metadataLength the metadata length to append to the buffer as an unsigned 24 bits
 *     integer.
 * @return the encoded mime and metadata length information
 */
static ByteBuf encodeMetadataHeader(
    ByteBufAllocator allocator, String customMime, int metadataLength) {
  ByteBuf metadataHeader = allocator.buffer(4 + customMime.length());
  // reserve 1 byte for the customMime length
  // /!\ careful not to read that first byte, which is random at this point
  int writerIndexInitial = metadataHeader.writerIndex();
  metadataHeader.writerIndex(writerIndexInitial + 1);

  // write the custom mime in UTF8 but validate it is all ASCII-compatible
  // (which produces the right result since ASCII chars are still encoded on 1 byte in UTF8)
  int customMimeLength = ByteBufUtil.writeUtf8(metadataHeader, customMime);
  if (!ByteBufUtil.isText(
      metadataHeader, metadataHeader.readerIndex() + 1, customMimeLength, CharsetUtil.US_ASCII)) {
    metadataHeader.release();
    throw new IllegalArgumentException("custom mime type must be US_ASCII characters only");
  }
  if (customMimeLength < 1 || customMimeLength > 128) {
    metadataHeader.release();
    throw new IllegalArgumentException(
        "custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128");
  }
  metadataHeader.markWriterIndex();

  // go back to beginning and write the length
  // encoded length is one less than actual length, since 0 is never a valid length, which gives
  // wider representation range
  metadataHeader.writerIndex(writerIndexInitial);
  metadataHeader.writeByte(customMimeLength - 1);

  // go back to post-mime type and write the metadata content length
  metadataHeader.resetWriterIndex();
  NumberUtils.encodeUnsignedMedium(metadataHeader, metadataLength);

  return metadataHeader;
}
 
Example 12
Source File: ForwardCloseRequest.java    From ethernet-ip with Apache License 2.0 5 votes vote down vote up
/**
 * Encode the connection path.
 * <p>
 * {@link PaddedEPath#encode(EPath, ByteBuf)} can't be used here because the {@link ForwardCloseRequest} has an
 * extra reserved byte after the connection path size for some reason.
 *
 * @param path   the {@link PaddedEPath} to encode.
 * @param buffer the {@link ByteBuf} to encode into.
 */
private static void encodeConnectionPath(PaddedEPath path, ByteBuf buffer) {
    // length placeholder...
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeByte(0);

    // reserved
    buffer.writeZero(1);

    // encode the path segments...
    int dataStartIndex = buffer.writerIndex();

    for (EPathSegment segment : path.getSegments()) {
        if (segment instanceof LogicalSegment) {
            LogicalSegment.encode((LogicalSegment) segment, path.isPadded(), buffer);
        } else if (segment instanceof PortSegment) {
            PortSegment.encode((PortSegment) segment, path.isPadded(), buffer);
        } else if (segment instanceof DataSegment) {
            DataSegment.encode((DataSegment) segment, path.isPadded(), buffer);
        } else {
            throw new RuntimeException("no encoder for " + segment.getClass().getSimpleName());
        }
    }

    // go back and update the length
    int bytesWritten = buffer.writerIndex() - dataStartIndex;
    int wordsWritten = bytesWritten / 2;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeByte(wordsWritten);
    buffer.resetWriterIndex();
}
 
Example 13
Source File: EPath.java    From ethernet-ip with Apache License 2.0 5 votes vote down vote up
public static ByteBuf encode(EPath path, ByteBuf buffer) {
    // length placeholder...
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeByte(0);

    // encode the path segments...
    int dataStartIndex = buffer.writerIndex();

    for (EPathSegment segment : path.getSegments()) {
        if (segment instanceof LogicalSegment) {
            LogicalSegment.encode((LogicalSegment) segment, path.isPadded(), buffer);
        } else if (segment instanceof PortSegment) {
            PortSegment.encode((PortSegment) segment, path.isPadded(), buffer);
        } else if (segment instanceof DataSegment) {
            DataSegment.encode((DataSegment) segment, path.isPadded(), buffer);
        } else {
            throw new RuntimeException("no encoder for " + segment.getClass().getSimpleName());
        }
    }

    // go back and update the length
    int bytesWritten = buffer.writerIndex() - dataStartIndex;
    int wordsWritten = bytesWritten / 2;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeByte(wordsWritten);
    buffer.resetWriterIndex();

    return buffer;
}
 
Example 14
Source File: EnipPacket.java    From ethernet-ip with Apache License 2.0 5 votes vote down vote up
public static ByteBuf encode(EnipPacket packet, ByteBuf buffer) {
    buffer.writeShort(packet.getCommandCode().getCode());

    // Length placeholder...
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeShort(0);

    buffer.writeInt((int) packet.getSessionHandle());
    buffer.writeInt(packet.getStatus().getStatus());
    buffer.writeLong(packet.getSenderContext());
    buffer.writeInt(0);

    int dataStartIndex = buffer.writerIndex();

    if (packet.getCommand() != null) {
        encodeCommand(packet.getCommand(), buffer);
    }

    // Go back and update the length.
    int bytesWritten = buffer.writerIndex() - dataStartIndex;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeShort(bytesWritten);
    buffer.resetWriterIndex();

    return buffer;
}
 
Example 15
Source File: CipIdentityItem.java    From ethernet-ip with Apache License 2.0 5 votes vote down vote up
public static ByteBuf encode(CipIdentityItem item, ByteBuf buffer) {
    buffer.writeShort(TYPE_ID);

    // Length placeholder...
    int lengthStartIndex = buffer.writerIndex();
    buffer.writeShort(0);

    // Encode the item...
    int itemStartIndex = buffer.writerIndex();
    buffer.writeShort(item.getProtocolVersion());
    SockAddr.encode(item.getSocketAddress(), buffer);
    buffer.writeShort(item.getVendorId());
    buffer.writeShort(item.getDeviceType());
    buffer.writeShort(item.getProductCode());
    buffer.writeByte(item.getRevisionMajor());
    buffer.writeByte(item.getRevisionMinor());
    buffer.writeShort(item.getStatus());
    buffer.writeInt((int) item.getSerialNumber());
    writeString(item.getProductName(), buffer);
    buffer.writeByte(item.getState());

    // Go back and update the length.
    int bytesWritten = buffer.writerIndex() - itemStartIndex;
    buffer.markWriterIndex();
    buffer.writerIndex(lengthStartIndex);
    buffer.writeShort(bytesWritten);
    buffer.resetWriterIndex();

    return buffer;
}
 
Example 16
Source File: UnconnectedSendService.java    From ethernet-ip with Apache License 2.0 5 votes vote down vote up
private void encodeConnectionPath(ByteBuf buffer) {
    // connectionPath length
    int pathLengthStartIndex = buffer.writerIndex();
    buffer.writeByte(0);

    // reserved byte
    buffer.writeByte(0x00);

    // encode the path segments...
    int pathDataStartIndex = buffer.writerIndex();

    for (EPathSegment segment : connectionPath.getSegments()) {
        if (segment instanceof LogicalSegment) {
            LogicalSegment.encode((LogicalSegment) segment, connectionPath.isPadded(), buffer);
        } else if (segment instanceof PortSegment) {
            PortSegment.encode((PortSegment) segment, connectionPath.isPadded(), buffer);
        } else if (segment instanceof DataSegment) {
            DataSegment.encode((DataSegment) segment, connectionPath.isPadded(), buffer);
        } else {
            throw new RuntimeException("no encoder for " + segment.getClass().getSimpleName());
        }
    }

    // go back and update the length.
    int pathBytesWritten = buffer.writerIndex() - pathDataStartIndex;
    int wordsWritten = pathBytesWritten / 2;
    buffer.markWriterIndex();
    buffer.writerIndex(pathLengthStartIndex);
    buffer.writeByte(wordsWritten);
    buffer.resetWriterIndex();
}
 
Example 17
Source File: ByteBufUtils.java    From Distributed-KV with Apache License 2.0 5 votes vote down vote up
/**
 * 批量记录索引位置
 * @param buffers
 */
public static void markIndex(ByteBuf... buffers) {
	for(ByteBuf buffer : buffers) {
		// 记录bytebuf信息
		buffer.markReaderIndex();
		buffer.markWriterIndex();
	}
}
 
Example 18
Source File: TestToolBox.java    From qmq with Apache License 2.0 4 votes vote down vote up
public static Buffer messageToBuffer(BaseMessage message) {
	ByteBuf out = Unpooled.buffer();
	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);
	out.writerIndex(messageEnd);

	return new MemTableBuffer(out, out.writerIndex());
}
 
Example 19
Source File: MessageIndexSyncWorker.java    From qmq with Apache License 2.0 4 votes vote down vote up
@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);
    }

    try (MessageLogRecordVisitor visitor = storage.newMessageLogVisitor(startSyncOffset)) {
        LogSegment currentSegment = null;
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(batchSize);
        long nextSyncOffset = originalOffset;
        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();
                currentSegment = data.getLogSegment();

                byteBuf.markWriterIndex();

                //sequence
                if (!writeLong(data.getSequence(), byteBuf)) break;

                ByteBuffer body = data.getPayload();
                //skip flag
                body.get();

                //create time
                if (!writeLong(body.getLong(), byteBuf)) break;

                //skip expireTime
                body.getLong();

                //subject
                Control control = copyString(body, byteBuf);
                if (control == Control.INVALID) {
                    nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
                    continue;
                }
                if (control == Control.NOSPACE) break;

                //message id
                control = copyString(body, byteBuf);
                if (control == Control.INVALID) {
                    nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
                    continue;
                }
                if (control == Control.NOSPACE) break;

                nextSyncOffset = visitor.getStartOffset() + visitor.visitedBufferSize();
            }

        } finally {
            if (!byteBuf.isReadable()) {
                byteBuf.release();
            }
        }

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

        //FIXME: 这里完全是为了避免父类里做超时处理
        if (byteBuf.isReadable()) {
            return new ByteBufSegmentBuffer(nextSyncOffset, currentSegment, byteBuf, byteBuf.readableBytes());
        } else {
            return new ByteBufSegmentBuffer(nextSyncOffset);
        }
    }
}
 
Example 20
Source File: MessagesPayloadHolder.java    From qmq with Apache License 2.0 4 votes vote down vote up
private void serializeMessage(BaseMessage message, ByteBuf out) {
    int crcIndex = out.writerIndex();
    // sizeof(bodyCrc<long>)
    out.ensureWritable(8);
    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);
}