Java Code Examples for io.netty.buffer.ByteBuf.setBytes()

The following are Jave code examples for showing how to use setBytes() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: proteus-java   File: ProteusMetadata.java   Source Code and License Vote up 6 votes
public static int encode(ByteBuf byteBuf, int namespaceId, int serviceId, int methodId, ByteBuf metadata) {
    int offset = 0;

    byteBuf.setShort(offset, 1);
    offset += VERSION_SIZE;

    byteBuf.setInt(offset, namespaceId);
    offset += NAMESPACE_ID_SIZE;

    byteBuf.setInt(offset, serviceId);
    offset += SERVICE_ID_SIZE;

    byteBuf.setInt(offset, methodId);
    offset += METHOD_ID_SIZE;

    int metadataLength = metadata.readableBytes();
    byteBuf.setInt(offset, metadataLength);
    offset += METADATA_LENGTH_SIZE;

    byteBuf.setBytes(offset, metadata);
    offset += metadataLength;

    byteBuf.writerIndex(offset);

    return offset;
}
 
Example 2
Project: jsf-sdk   File: CodecUtils.java   Source Code and License Vote up 6 votes
/**
 * encode报文头
 *
 * @param header
 *         MessageHeader
 * @param byteBuf
 *         报文
 * @see JSFProtocol
 */
public static short encodeHeader(MessageHeader header, ByteBuf byteBuf) {
    short headLength = 8; // 没有map 长度是8
    if( byteBuf.capacity() < 8 ) byteBuf.capacity(8);
    int writeIndex = byteBuf.writerIndex();
    byteBuf.writeShort(headLength);
    byteBuf.writeByte(header.getProtocolType());
    byteBuf.writeByte(header.getCodecType());
    byteBuf.writeByte(header.getMsgType());
    byteBuf.writeByte(header.getCompressType());
    byteBuf.writeInt(header.getMsgId());
    if (header.getAttrMapSize() > 0) {
        headLength += map2bytes(header.getAttrMap(), byteBuf);
        byteBuf.setBytes(writeIndex, short2bytes(headLength)); // 替换head长度的两位
    }
    return headLength;
}