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

The following examples show how to use io.netty.buffer.ByteBuf#writeLong() . 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: BSTIndex.java    From tajo with Apache License 2.0 6 votes vote down vote up
private void writeIndex(ByteBuf byteBuf, Tuple tuple, Long... offsets) throws IOException {

      byte[] buf = rowStoreEncoder.toBytes(tuple);
      int size = buf.length + 8 + (offsets.length * 8);
      if (!byteBuf.isWritable(size)) {
        byteBuf.ensureWritable(size);
      }

      // key writing
      byteBuf.writeInt(buf.length);
      byteBuf.writeBytes(buf);

      //offset num writing
      byteBuf.writeInt(offsets.length);

      /* offset writing */
      for (long offset : offsets) {
        byteBuf.writeLong(offset);
      }

      entrySize++;
      // flush to file and reset buffer
      if (byteBuf.writerIndex() >= BUFFER_SIZE) {
        filePos += flushBuffer(byteBuf, outChannel, out);
      }
    }
 
Example 2
Source File: KafkaMessageV0Serializer.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
public static void writeMessage(ByteBuf buffer, KafkaBrokerMessage message) throws Exception {
    buffer.writeLong(message.getOffset());

    int startIndex = buffer.writerIndex();
    buffer.writeInt(0); // length
    buffer.writeInt(0); // crc
    buffer.writeByte(CURRENT_MAGIC);
    buffer.writeByte(message.getAttribute());

    KafkaBufferUtils.writeBytes(message.getKey(), buffer);
    KafkaBufferUtils.writeBytes(message.getValue(), buffer);

    // 计算整个message长度,包括长度本身的字节数
    int length = buffer.writerIndex() - startIndex;
    byte[] bytes = new byte[length];
    buffer.getBytes(startIndex, bytes);

    // 计算crc,不包括长度字节和crc字节,从magic开始
    long crc = KafkaBufferUtils.crc32(bytes, 4 + 4, bytes.length - 4 - 4);

    // 写入长度和crc
    buffer.setInt(startIndex, length - 4);
    buffer.setInt(startIndex + 4, (int) (crc & 0xffffffffL));
}
 
Example 3
Source File: CommandDecoderTest.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnknownFields() throws IOException {
    ByteBuf buffer = Unpooled.buffer(100);
    Hello command = new WireCommands.Hello(10, 1);     
    CommandEncoder.writeMessage(command, buffer);
    buffer.writeLong(1); //Bonus data
    buffer.writeLong(2);
    buffer.writeLong(3);
    buffer.setInt(4, 8 + 24);
    assertEquals(16 + 24, buffer.readableBytes());

    command = (Hello) CommandDecoder.parseCommand(buffer);
    assertEquals(0, buffer.readableBytes());
    assertEquals(WireCommandType.HELLO, command.getType());    
    assertEquals(10, command.highVersion); 
    assertEquals(1, command.lowVersion);          
}
 
Example 4
Source File: ReadModifyWriteTagService.java    From ethernet-ip with Apache License 2.0 6 votes vote down vote up
private void encode(ByteBuf buffer) {
    switch (maskSize) {
        case ONE_BYTE:
            buffer.writeShort(1);
            buffer.writeByte((int) orMask);
            buffer.writeByte((int) andMask);
            break;
        case TWO_BYTE:
            buffer.writeShort(2);
            buffer.writeShort((int) orMask);
            buffer.writeShort((int) andMask);
            break;
        case FOUR_BYTE:
            buffer.writeShort(4);
            buffer.writeInt((int) orMask);
            buffer.writeInt((int) andMask);
            break;
        case EIGHT_BYTE:
            buffer.writeShort(8);
            buffer.writeLong(orMask);
            buffer.writeLong(andMask);
            break;
    }
}
 
Example 5
Source File: LoginDecoder.java    From luna with MIT License 6 votes vote down vote up
/**
 * Decodes the handshake.
 *
 * @param ctx The channel handler context.
 * @param in The buffer to read data from.
 */
private void decodeHandshake(ChannelHandlerContext ctx, ByteBuf in) {
    if (in.readableBytes() >= 2) {
        int opcode = in.readUnsignedByte(); // TODO Ondemand?

        @SuppressWarnings("unused") int nameHash = in.readUnsignedByte();

        // TODO WHEN AN EXCEPTION IS THROWN HERE THE PLAYER GETS STUCK LOGGED IN?
        checkState(opcode == 14, "opcode != 14");

        ByteBuf msg = ByteMessage.pooledBuffer(17);
        try {
            msg.writeLong(0);
            msg.writeByte(0);
            msg.writeLong(RANDOM.nextLong());
        } finally {
            ctx.writeAndFlush(msg);
        }

        checkpoint(DecodeState.LOGIN_TYPE);
    }
}
 
Example 6
Source File: FetchIndexResponseCodec.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(FetchIndexResponse payload, ByteBuf buffer) throws Exception {
    buffer.writeShort(payload.getData().rowMap().size());
    for (Map.Entry<String, Map<Short, FetchIndexData>> topicEntry : payload.getData().rowMap().entrySet()) {
        Serializer.write(topicEntry.getKey(), buffer, Serializer.SHORT_SIZE);
        buffer.writeShort(topicEntry.getValue().size());
        for (Map.Entry<Short, FetchIndexData> partitionEntry : topicEntry.getValue().entrySet()) {
            FetchIndexData fetchIndexData = partitionEntry.getValue();
            buffer.writeShort(partitionEntry.getKey());
            buffer.writeLong(fetchIndexData.getIndex());
            buffer.writeInt(fetchIndexData.getCode().getCode());

            if (payload.getHeader().getVersion() >= JoyQueueHeader.VERSION_V3) {
                buffer.writeLong(fetchIndexData.getLeftIndex());
                buffer.writeLong(fetchIndexData.getRightIndex());
            }
        }
    }
}
 
Example 7
Source File: RpcRequestProtocolV1.java    From hasor with Apache License 2.0 6 votes vote down vote up
/**encode Message to byte & write to network framework*/
public void encode(RequestBlock reqMsg, ByteBuf buf) throws IOException {
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF版本
    buf.writeByte(reqMsg.getHead());
    //* byte[8]  requestID                            请求ID
    buf.writeLong(reqMsg.getRequestID());
    //* byte[1]  keepData                             保留区
    buf.writeByte(0);
    //* byte[3]  contentLength                        内容大小(max = 16MB)
    //
    ByteBuf requestBody = this.encodeRequest(reqMsg);
    int bodyLength = requestBody.readableBytes();
    bodyLength = (bodyLength << 8) >>> 8;//左移8未,在无符号右移8位。形成最大16777215字节的限制。
    buf.writeMedium(bodyLength);
    //
    buf.writeBytes(requestBody);
}
 
Example 8
Source File: SortedPullLogTable.java    From qmq with Apache License 2.0 5 votes vote down vote up
public boolean appendIndex(Map<String, PullLogIndexEntry> indexMap) {
    positionOfIndex = tablet.getWrotePosition();

    for (Map.Entry<String, PullLogIndexEntry> entry : indexMap.entrySet()) {
        final byte[] consumerBytes = entry.getKey().getBytes(StandardCharsets.UTF_8);
        int size = Integer.BYTES + Short.BYTES + consumerBytes.length + Long.BYTES + Long.BYTES + Integer.BYTES + Integer.BYTES;
        PullLogIndexEntry indexEntry = entry.getValue();

        ByteBuf buffer = ByteBufAllocator.DEFAULT.ioBuffer(size);
        try {
            buffer.writeInt(MagicCode.PULL_LOG_MAGIC_V1);
            buffer.writeShort((short) consumerBytes.length);
            buffer.writeBytes(consumerBytes);
            buffer.writeLong(indexEntry.startOfPullLogSequence);
            buffer.writeLong(indexEntry.baseOfMessageSequence);
            buffer.writeInt(indexEntry.position);
            buffer.writeInt(indexEntry.num);
            ByteBuffer nioBuffer = buffer.nioBuffer();
            Checksums.update(crc, nioBuffer, nioBuffer.limit());
            boolean result = tablet.appendData(nioBuffer);
            if (!result) return false;
        } finally {
            ReferenceCountUtil.safeRelease(buffer);
        }

        ConcurrentSkipListMap<PullLogSequence, SegmentLocation> index = sortedPullLogTable.index;
        index.put(new PullLogSequence(entry.getKey(), indexEntry.startOfPullLogSequence), new SegmentLocation(indexEntry.baseOfMessageSequence, indexEntry.position, indexEntry.num, tablet));
        tablet.retain();
    }
    return true;
}
 
Example 9
Source File: HttpFrameDecoderTest.java    From netty-http2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidHttpPingFrame() throws Exception {
    int length = 12; // invalid length
    byte flags = 0;
    int streamId = 0; // connection identifier
    long data = RANDOM.nextLong();

    ByteBuf frame = pingFrame(length, flags, streamId);
    frame.writeLong(data);
    decoder.decode(frame);

    verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}
 
Example 10
Source File: Serializer.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public static void write(final AppToken appToken, final ByteBuf out) throws Exception {
    out.writeLong(appToken.getId());
    write(appToken.getApp(),out);
    write(appToken.getToken(),out);
    out.writeLong(appToken.getEffectiveTime().getTime());
    out.writeLong(appToken.getExpirationTime().getTime());
}
 
Example 11
Source File: DefaultMessageCodecTest.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testCRCFail() throws Exception {
	ByteBuf buf = Unpooled.buffer();
	Magic.writeMagic(buf);
	buf.writeByte(MessageCodecVersion.BINARY_V1.getVersion());
	buf.writeInt(30);
	buf.writeInt(1);
	buf.writeInt(1);
	buf.writeBytes(new byte[] { 1, 2, 1 });
	buf.writeLong(10L);
	MessageCodec codec = new DefaultMessageCodec();
	codec.decode("topic", buf, String.class);
}
 
Example 12
Source File: UploadStorageGetEncoder.java    From fastdfs-client with Apache License 2.0 5 votes vote down vote up
@Override
public List<Object> encode(ByteBufAllocator alloc) {
    int length = isEmpty(group) ? 0 : FDFS_GROUP_LEN;
    byte cmd = isEmpty(group) ? SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE : SERVICE_QUERY_STORE_WITH_GROUP_ONE;

    ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN);
    buf.writeLong(length);
    buf.writeByte(cmd);
    buf.writeByte(ERRNO_OK);
    if (!isEmpty(group)) {
        writeFixLength(buf, group, FDFS_GROUP_LEN);
    }
    return Collections.singletonList(buf);
}
 
Example 13
Source File: PullRequestSerde.java    From qmq with Apache License 2.0 5 votes vote down vote up
public void write(final PullRequest request, final ByteBuf out) {
    PayloadHolderUtils.writeString(request.getSubject(), out);
    PayloadHolderUtils.writeString(request.getGroup(), out);
    PayloadHolderUtils.writeString(request.getConsumerId(), out);

    out.writeInt(request.getRequestNum());
    out.writeLong(request.getOffset());
    out.writeLong(request.getPullOffsetBegin());
    out.writeLong(request.getPullOffsetLast());
    out.writeLong(request.getTimeoutMillis());
    out.writeByte(request.isBroadcast() ? 1 : 0);

    writeFilters(request.getFilters(), out);
}
 
Example 14
Source File: PaletteBlockStateStorage.java    From Cleanstone with MIT License 5 votes vote down vote up
public void write(ByteBuf out) {
    out.writeByte(bitsPerEntry);
    if (isIndirectPalette(bitsPerEntry) || !omitDirectPaletteLength) {
        ByteBufUtils.writeVarInt(out, indirectPalette.size());
    }
    for (BlockState state : indirectPalette) {
        ByteBufUtils.writeVarInt(out, directPalette.getIndex(state));
    }

    long[] data = baseStorage.getData();
    ByteBufUtils.writeVarInt(out, data.length);
    for (long dataItem : data) {
        out.writeLong(dataItem);
    }
}
 
Example 15
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeUuid(ByteBuf buffer, UUID uuid) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(uuid, "uuid");
    buffer.writeLong(uuid.getMostSignificantBits());
    buffer.writeLong(uuid.getLeastSignificantBits());
}
 
Example 16
Source File: PongCodec.java    From Cleanstone with MIT License 4 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf byteBuf, PongPacket packet) {

    byteBuf.writeLong(packet.getPayload());
    return byteBuf;
}
 
Example 17
Source File: MessageProcessorAction.java    From Minecoprocessors with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void toBytes(ByteBuf buf) {
  buf.writeInt(action.ordinal());
  buf.writeLong(pos.toLong());
}
 
Example 18
Source File: Serializer.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
/**
 * 写入存储消息
 *
 * @param message 存储消息
 * @param out     输出缓冲区
 * @throws Exception 序列化异常
 */
@Deprecated
public static void write(final BrokerMessage message, final ByteBuf out) throws Exception {
    int size;
    if (out == null || message == null) {
        return;
    }
    // 记录写入的起始位置
    int begin = out.writerIndex();
    // 4个字节的消息长度需要计算出来
    out.writeInt(0);
    // 2个字节的魔法标识
    out.writeShort(message.getPartition());
    //消息序号
    out.writeLong(message.getMsgIndexNo());
    out.writeInt(message.getTerm());
    out.writeShort(BrokerMessage.MAGIC_CODE);

    //   | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
    //   1个字节的系统字段 1-1:压缩标识 2-2:顺序消息 3-4: 消息来源,包括Jmq,kafka,mqtt 5-5:压缩算法 6-8:其他,预留未用
    byte sysCode = (byte) (message.isCompressed() ? 1 : 0);
    sysCode |= ((message.isOrdered() ? 1 : 0) << 1) & 0x3;

    sysCode |= (message.getSource() << 2) & 12;
    // compressor
    if (message.isCompressed()) {
        sysCode |= (message.getCompressionType().getType() << 4) & 48;
    }
    out.writeByte(sysCode);
    // 1字节优先级
    out.writeByte(message.getPriority());
    // 6字节的客户端地址
    byte[] clientIp = message.getClientIp();
    if (clientIp != null) {
        out.writeBytes(message.getClientIp());
        if (message.getClientIp().length == 6){
            out.writeBytes(new byte[10]);
        }
    } else {
        out.writeBytes(new byte[16]);
    }

    // 8字节发送时间
    out.writeLong(message.getStartTime());
    // 4字节存储时间(相对发送时间的偏移)
    out.writeInt(0);
    // 8字节消息体CRC
    out.writeLong(message.getBodyCRC());

    // 4字节消息体大小
    // 消息体(字节数组)
    if (message.getByteBody() != null) {
        write(message.getBody(), out);
    } else {
        out.writeInt(0);
    }

    // 1字节主题长度
    // 主题(字节数组)
    write(message.getTopic(), out);
    // 1字节应用长度
    // 应用(字节数组)
    write(message.getApp(), out);
    // 1字节业务ID长度
    // 业务ID(字节数组)
    write(message.getBusinessId(), out);
    write(message.getTxId(), out, SHORT_SIZE);
    // 2字节属性长度
    // 属性(字节数组)
    write(toProperties(message.getAttributes()), out, 2);
    // 4字节扩展字段大小
    // 扩展字段(字节数组)
    write(message.getExtension(), out);

    // 重写总长度
    int end = out.writerIndex();
    size = end - begin;
    message.setSize(size);
    out.writerIndex(begin);
    out.writeInt(size);
    out.writerIndex(end);
}
 
Example 19
Source File: Serializer.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
/**
 * 写入存储消息
 *
 * @param message 存储消息
 * @param out     输出缓冲区
 * @throws Exception 序列化异常
 */
@Deprecated
public static void write(final BrokerMessage message, final ByteBuf out) throws Exception {
    int size;
    if (out == null || message == null) {
        return;
    }
    // 记录写入的起始位置
    int begin = out.writerIndex();
    // 4个字节的消息长度需要计算出来
    out.writeInt(0);
    // 2个字节的魔法标识
    out.writeShort(message.getPartition());
    //消息序号
    out.writeLong(message.getMsgIndexNo());
    out.writeInt(message.getTerm());
    out.writeShort(BrokerMessage.MAGIC_CODE);

    //   | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
    //   1个字节的系统字段 1-1:压缩标识 2-2:顺序消息 3-4: 消息来源,包括Jmq,kafka,mqtt 5-5:压缩算法 6-8:其他,预留未用
    byte sysCode = (byte) (message.isCompressed() ? 1 : 0);
    sysCode |= ((message.isOrdered() ? 1 : 0) << 1) & 0x3;

    sysCode |= (message.getSource() << 2) & 12;
    // compressor
    if (message.isCompressed()) {
        sysCode |= (message.getCompressionType().getType() << 4) & 48;
    }
    out.writeByte(sysCode);
    // 1字节优先级
    out.writeByte(message.getPriority());
    // 6字节的客户端地址
    byte[] clientIp = message.getClientIp();
    if (clientIp != null) {
        out.writeBytes(message.getClientIp());
        if (message.getClientIp().length == 6){
            out.writeBytes(new byte[10]);
        }
    } else {
        out.writeBytes(new byte[16]);
    }

    // 8字节发送时间
    out.writeLong(message.getStartTime());
    // 4字节存储时间(相对发送时间的偏移)
    out.writeInt(0);
    // 8字节消息体CRC
    out.writeLong(message.getBodyCRC());

    // 4字节消息体大小
    // 消息体(字节数组)
    if (message.getByteBody() != null) {
        write(message.getBody(), out);
    } else {
        out.writeInt(0);
    }

    // 1字节主题长度
    // 主题(字节数组)
    write(message.getTopic(), out);
    // 1字节应用长度
    // 应用(字节数组)
    write(message.getApp(), out);
    // 1字节业务ID长度
    // 业务ID(字节数组)
    write(message.getBusinessId(), out);
    write(message.getTxId(), out, SHORT_SIZE);
    // 2字节属性长度
    // 属性(字节数组)
    write(toProperties(message.getAttributes()), out, 2);
    // 4字节扩展字段大小
    // 扩展字段(字节数组)
    write(message.getExtension(), out);

    // 重写总长度
    int end = out.writerIndex();
    size = end - begin;
    message.setSize(size);
    out.writerIndex(begin);
    out.writeInt(size);
    out.writerIndex(end);
}
 
Example 20
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private static void binaryEncodeUUID(UUID uuid, ByteBuf buff) {
  buff.writeLong(uuid.getMostSignificantBits());
  buff.writeLong(uuid.getLeastSignificantBits());
}