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

The following examples show how to use io.netty.buffer.ByteBuf#setIntLE() . 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: ZstdEncoder.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
private void writeUnCompressedData(ByteBuf out) {
    int flushableBytes = buffer.readableBytes();
    if (flushableBytes == 0) {
        return;
    }
    checksum.reset();
    checksum.update(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes));
    final int check = (int) checksum.getValue();

    out.ensureWritable(flushableBytes + HEADER_LENGTH);
    final int idx = out.writerIndex();
    out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes);

    out.setInt(idx, MAGIC_NUMBER);
    out.setByte(idx + TOKEN_OFFSET, (byte) BLOCK_TYPE_NON_COMPRESSED);
    out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, flushableBytes);
    out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes);
    out.setIntLE(idx + CHECKSUM_OFFSET, check);
    out.writerIndex(idx + HEADER_LENGTH + flushableBytes);
    buffer.clear();
}
 
Example 2
Source File: Lz4FrameEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void flushBufferedData(ByteBuf out) {
    int flushableBytes = buffer.readableBytes();
    if (flushableBytes == 0) {
        return;
    }
    checksum.reset();
    checksum.update(buffer, buffer.readerIndex(), flushableBytes);
    final int check = (int) checksum.getValue();

    final int bufSize = compressor.maxCompressedLength(flushableBytes) + HEADER_LENGTH;
    out.ensureWritable(bufSize);
    final int idx = out.writerIndex();
    int compressedLength;
    try {
        ByteBuffer outNioBuffer = out.internalNioBuffer(idx + HEADER_LENGTH, out.writableBytes() - HEADER_LENGTH);
        int pos = outNioBuffer.position();
        // We always want to start at position 0 as we take care of reusing the buffer in the encode(...) loop.
        compressor.compress(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes), outNioBuffer);
        compressedLength = outNioBuffer.position() - pos;
    } catch (LZ4Exception e) {
        throw new CompressionException(e);
    }
    final int blockType;
    if (compressedLength >= flushableBytes) {
        blockType = BLOCK_TYPE_NON_COMPRESSED;
        compressedLength = flushableBytes;
        out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes);
    } else {
        blockType = BLOCK_TYPE_COMPRESSED;
    }

    out.setLong(idx, MAGIC_NUMBER);
    out.setByte(idx + TOKEN_OFFSET, (byte) (blockType | compressionLevel));
    out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, compressedLength);
    out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes);
    out.setIntLE(idx + CHECKSUM_OFFSET, check);
    out.writerIndex(idx + HEADER_LENGTH + compressedLength);
    buffer.clear();
}
 
Example 3
Source File: FecEncode.java    From java-Kcp with Apache License 2.0 5 votes vote down vote up
public void markParity(ByteBuf byteBuf, int offset){
    byteBuf.setIntLE(offset, (int) this.next);
    byteBuf.setShortLE(offset+4,Fec.typeParity);
    //if(next==this.paws){
    //    next=0;
    //}else{
    //    next++;
    //}
    this.next = (this.next + 1) % this.paws;
    //if(next+1>=this.paws) {
    //    this.next++;
    //    //this.next = (this.next + 1) % this.paws;
    //}
}
 
Example 4
Source File: SQLBatchCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private void sendBatchClientRequest() {
  ChannelHandlerContext chctx = encoder.chctx;

  ByteBuf packet = chctx.alloc().ioBuffer();

  // packet header
  packet.writeByte(MessageType.SQL_BATCH.value());
  packet.writeByte(MessageStatus.NORMAL.value() | MessageStatus.END_OF_MESSAGE.value());
  int packetLenIdx = packet.writerIndex();
  packet.writeShort(0); // set length later
  packet.writeShort(0x00);
  packet.writeByte(0x00); // FIXME packet ID
  packet.writeByte(0x00);

  int start = packet.writerIndex();
  packet.writeIntLE(0x00); // TotalLength for ALL_HEADERS
  encodeTransactionDescriptor(packet, 0, 1);
  // set TotalLength for ALL_HEADERS
  packet.setIntLE(start, packet.writerIndex() - start);

  // SQLText
  packet.writeCharSequence(cmd.sql(), StandardCharsets.UTF_16LE);

  int packetLen = packet.writerIndex() - packetLenIdx + 2;
  packet.setShort(packetLenIdx, packetLen);

  chctx.writeAndFlush(packet);
}
 
Example 5
Source File: ZstdEncoder.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
private void flushBufferedData(ByteBuf out) {
    final int flushableBytes = buffer.readableBytes();
    if (flushableBytes == 0) {
        return;
    }
    checksum.reset();
    checksum.update(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes));
    final int check = (int) checksum.getValue();

    final int bufSize = (int) Zstd.compressBound(flushableBytes) + HEADER_LENGTH;
    out.ensureWritable(bufSize);
    final int idx = out.writerIndex();
    int compressedLength;
    try {
        ByteBuffer outNioBuffer = out.internalNioBuffer(idx + HEADER_LENGTH, out.writableBytes() - HEADER_LENGTH);
        compressedLength = Zstd.compress(
                outNioBuffer,
                buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes),
                DEFAULT_COMPRESS_LEVEL);
    } catch (Exception e) {
        throw new CompressionException(e);
    }
    final int blockType;
    if (compressedLength >= flushableBytes) {
        blockType = BLOCK_TYPE_NON_COMPRESSED;
        compressedLength = flushableBytes;
        out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes);
    } else {
        blockType = BLOCK_TYPE_COMPRESSED;
    }

    out.setInt(idx, MAGIC_NUMBER);
    out.setByte(idx + TOKEN_OFFSET, (byte) (blockType | compressionLevel));
    out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, compressedLength);
    out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes);
    out.setIntLE(idx + CHECKSUM_OFFSET, check);
    out.writerIndex(idx + HEADER_LENGTH + compressedLength);
    buffer.clear();
}
 
Example 6
Source File: FecEncode.java    From java-Kcp with Apache License 2.0 4 votes vote down vote up
public void markData(ByteBuf byteBuf,int offset){
    byteBuf.setIntLE(offset, (int) this.next);
    byteBuf.setShortLE(offset+4, Fec.typeData);
    this.next++;
}
 
Example 7
Source File: ExtendedQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private void sendPrepexecRequest() {
  ChannelHandlerContext chctx = encoder.chctx;

  ByteBuf packet = chctx.alloc().ioBuffer();

  // packet header
  packet.writeByte(MessageType.RPC.value());
  packet.writeByte(MessageStatus.NORMAL.value() | MessageStatus.END_OF_MESSAGE.value());
  int packetLenIdx = packet.writerIndex();
  packet.writeShort(0); // set length later
  packet.writeShort(0x00);
  packet.writeByte(0x00); // FIXME packet ID
  packet.writeByte(0x00);

  int start = packet.writerIndex();
  packet.writeIntLE(0x00); // TotalLength for ALL_HEADERS
  encodeTransactionDescriptor(packet, 0, 1);
  // set TotalLength for ALL_HEADERS
  packet.setIntLE(start, packet.writerIndex() - start);

  /*
    RPCReqBatch
   */
  packet.writeShortLE(0xFFFF);
  packet.writeShortLE(ProcId.Sp_PrepExec);

  // Option flags
  packet.writeShortLE(0x0000);

  // Parameter

  // OUT Parameter
  packet.writeByte(0x00);
  packet.writeByte(0x01); // By reference
  packet.writeByte(MSSQLDataTypeId.INTNTYPE_ID);
  packet.writeByte(0x04);
  packet.writeByte(0x04);
  packet.writeIntLE(0x00);

  Tuple params = cmd.params();

  // Param definitions
  String paramDefinitions = parseParamDefinitions(params);
  encodeNVarcharParameter(packet, paramDefinitions);

  // SQL text
  encodeNVarcharParameter(packet, cmd.sql());

  // Param values
  for (int i = 0; i < params.size(); i++) {
    encodeParamValue(packet, params.getValue(i));
  }

  int packetLen = packet.writerIndex() - packetLenIdx + 2;
  packet.setShort(packetLenIdx, packetLen);

  chctx.writeAndFlush(packet);
}