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

The following examples show how to use io.netty.buffer.ByteBuf#ensureWritable() . 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: HttpHeadersEncoder.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
static void encoderHeader(CharSequence name, CharSequence value, ByteBuf buf) {
    final int nameLen = name.length();
    final int valueLen = value.length();
    final int entryLen = nameLen + valueLen + 4;
    buf.ensureWritable(entryLen);
    int offset = buf.writerIndex();
    writeAscii(buf, offset, name);
    offset += nameLen;
    ByteBufUtil.setShortBE(buf, offset, COLON_AND_SPACE_SHORT);
    offset += 2;
    writeAscii(buf, offset, value);
    offset += valueLen;
    ByteBufUtil.setShortBE(buf, offset, CRLF_SHORT);
    offset += 2;
    buf.writerIndex(offset);
}
 
Example 2
Source File: HttpObjectEncoder.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
private static void encodeHeader(CharSequence name, CharSequence value, ByteBuf byteBuf, Buffer buffer) {
    final int nameLen = name.length();
    final int valueLen = value.length();
    final int entryLen = nameLen + valueLen + 4;
    byteBuf.ensureWritable(entryLen);
    int offset = byteBuf.writerIndex();
    writeAscii(name, byteBuf, buffer, offset);
    offset += nameLen;
    ByteBufUtil.setShortBE(byteBuf, offset, COLON_AND_SPACE_SHORT);
    offset += 2;
    writeAscii(value, byteBuf, buffer, offset);
    offset += valueLen;
    ByteBufUtil.setShortBE(byteBuf, offset, CRLF_SHORT);
    offset += 2;
    byteBuf.writerIndex(offset);
}
 
Example 3
Source File: HttpHeadersEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static void encoderHeader(CharSequence name, CharSequence value, ByteBuf buf) {
    final int nameLen = name.length();
    final int valueLen = value.length();
    final int entryLen = nameLen + valueLen + 4;
    buf.ensureWritable(entryLen);
    int offset = buf.writerIndex();
    writeAscii(buf, offset, name);
    offset += nameLen;
    ByteBufUtil.setShortBE(buf, offset, COLON_AND_SPACE_SHORT);
    offset += 2;
    writeAscii(buf, offset, value);
    offset += valueLen;
    ByteBufUtil.setShortBE(buf, offset, CRLF_SHORT);
    offset += 2;
    buf.writerIndex(offset);
}
 
Example 4
Source File: BSTIndex.java    From tajo with Apache License 2.0 6 votes vote down vote up
private void writeRootIndex(ByteBuf byteBuf, Tuple tuple, long offset) throws IOException {
  byte[] buf = rowStoreEncoder.toBytes(tuple);
  int size = buf.length + 12;
  if (!byteBuf.isWritable(size)) {
    byteBuf.ensureWritable(size);
  }

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

  // leaf offset writing
  byteBuf.writeLong(offset);

  rootEntrySize++;
  // flush to file and reset buffer
  if (byteBuf.writerIndex() >= BUFFER_SIZE) {
    flushBuffer(byteBuf, rootOutChannel, rootOut);
  }
}
 
Example 5
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 6
Source File: Application.java    From xrpc with Apache License 2.0 6 votes vote down vote up
private static FullHttpResponse getDino(XrpcRequest request, List<Dino> dinos) {
  try {
    DinoGetRequest getRequest =
        DinoGetRequest.parseFrom(CodedInputStream.newInstance(request.body().nioBuffer()));
    Optional<Dino> dinoOptional =
        dinos.stream().filter(xs -> xs.getName().equals(getRequest.getName())).findFirst();

    if (dinoOptional.isPresent()) {
      DinoGetReply getReply = DinoGetReply.newBuilder().setDino(dinoOptional.get()).build();
      ByteBuf resp = request.byteBuf();
      resp.ensureWritable(CodedOutputStream.computeMessageSizeNoTag(getReply), true);
      getReply.writeTo(new ByteBufOutputStream(resp));

      return Recipes.newResponse(
          HttpResponseStatus.OK,
          request.byteBuf().writeBytes(resp),
          Recipes.ContentType.Application_Octet_Stream);
    }

  } catch (IOException e) {
    return Recipes.newResponseBadRequest("Malformed GetDino Request: " + e.getMessage());
  }

  return Recipes.newResponseOk("Dino not Found");
}
 
Example 7
Source File: NativeVelocityCipher.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void process(ByteBuf source, ByteBuf destination) throws ShortBufferException {
  ensureNotDisposed();
  source.memoryAddress();
  destination.memoryAddress();

  // The exact amount we read in is also the amount we write out.
  int len = source.readableBytes();
  destination.ensureWritable(len);

  impl.process(ctx, source.memoryAddress() + source.readerIndex(), len,
      destination.memoryAddress() + destination.writerIndex(), encrypt);

  source.skipBytes(len);
  destination.writerIndex(destination.writerIndex() + len);
}
 
Example 8
Source File: NativeVelocityCompressor.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void deflate(ByteBuf source, ByteBuf destination) throws DataFormatException {
  ensureNotDisposed();
  source.memoryAddress();
  destination.memoryAddress();

  while (!deflate.finished) {
    if (!destination.isWritable()) {
      destination.ensureWritable(ZLIB_BUFFER_SIZE);
    }
    int produced = deflate.process(deflateCtx, source.memoryAddress() + source.readerIndex(),
        source.readableBytes(),
        destination.memoryAddress() + destination.writerIndex(), destination.writableBytes(),
        true);
    source.readerIndex(source.readerIndex() + deflate.consumed);
    destination.writerIndex(destination.writerIndex() + produced);
  }

  deflate.reset(deflateCtx);
  deflate.consumed = 0;
  deflate.finished = false;
}
 
Example 9
Source File: ProtocolLengthSerializer.java    From CloudNet with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    ProtocolBuffer in = new ProtocolBuffer(msg), outbuffer = new ProtocolBuffer(out);
    int readableBytes = in.readableBytes(), lengthByteSpace = getVarIntSize(readableBytes);

    if (lengthByteSpace > 3) {
        throw new IllegalArgumentException();
    }

    out.ensureWritable(lengthByteSpace + readableBytes);
    outbuffer.writeVarInt(readableBytes);
    out.writeBytes(in, in.readerIndex(), readableBytes);
}
 
Example 10
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 11
Source File: Varint21LengthPrepender.java    From ServerListPlus with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    int len = msg.readableBytes();
    int header = MinecraftProtocol.getVarIntSize(len);
    if (header > 3) {
        throw new EncoderException("Packet too big: " + len);
    }

    out.ensureWritable(header + len);
    MinecraftProtocol.writeVarInt(out, len);
    out.writeBytes(msg);
}
 
Example 12
Source File: ProtobufVarint32LengthFieldPrepender.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(
        ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    int bodyLen = msg.readableBytes();
    int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen);
    out.ensureWritable(headerLen + bodyLen);

    CodedOutputStream headerOut =
            CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLen);
    headerOut.writeRawVarint32(bodyLen);
    headerOut.flush();

    out.writeBytes(msg, msg.readerIndex(), bodyLen);
}
 
Example 13
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 14
Source File: APDUEncoder.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void handleUFormat ( final UnnumberedControl msg, final ByteBuf out )
{
    out.ensureWritable ( 6 );
    out.writeByte ( Constants.START_BYTE );
    out.writeByte ( 4 );
    out.writeByte ( msg.getFunction ().getNumericValue () | 0x03 /* bits 1 and 2*/);
    out.writeZero ( 3 );
}
 
Example 15
Source File: NetworkSplitter.java    From The-5zig-Mod with MIT License 5 votes vote down vote up
protected void encode(ChannelHandlerContext ctx, ByteBuf out, ByteBuf byteBuf) {
	int var4 = out.readableBytes();
	int var5 = PacketBuffer.getVarIntSize(var4);

	if (var5 > 3) {
		throw new IllegalArgumentException("unable to fit " + var4 + " into " + 3);
	} else {
		byteBuf.ensureWritable(var5 + var4);
		PacketBuffer.writeVarIntToBuffer(byteBuf, var4);
		byteBuf.writeBytes(out, out.readerIndex(), var4);
	}
}
 
Example 16
Source File: PacketChunkInfo.java    From LookingGlass with GNU General Public License v3.0 5 votes vote down vote up
public static FMLProxyPacket createPacket(Chunk chunk, boolean includeinit, int subid, int dim) {
	int xPos = chunk.xPosition;
	int zPos = chunk.zPosition;
	Extracted extracted = getMapChunkData(chunk, includeinit, subid);
	int yMSBPos = extracted.field_150281_c;
	int yPos = extracted.field_150280_b;
	byte[] chunkData = extracted.field_150282_a;

	deflateGate.acquireUninterruptibly();
	byte[] compressedChunkData = new byte[chunkData.length];
	int len = deflate(chunkData, compressedChunkData);
	deflateGate.release();

	// This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe.
	ByteBuf data = PacketHandlerBase.createDataBuffer((Class<? extends PacketHandlerBase>) new Object() {}.getClass().getEnclosingClass());

	data.writeInt(dim);
	data.writeInt(xPos);
	data.writeInt(zPos);
	data.writeBoolean(includeinit);
	data.writeShort((short) (yPos & 65535));
	data.writeShort((short) (yMSBPos & 65535));
	data.writeInt(len);
	data.writeInt(chunkData.length);
	data.ensureWritable(len);
	data.writeBytes(compressedChunkData, 0, len);

	return buildPacket(data);
}
 
Example 17
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 18
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);
}
 
Example 19
Source File: Bzip2Encoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
    if (finished) {
        out.writeBytes(in);
        return;
    }

    for (;;) {
        switch (currentState) {
            case INIT:
                out.ensureWritable(4);
                out.writeMedium(MAGIC_NUMBER);
                out.writeByte('0' + streamBlockSize / BASE_BLOCK_SIZE);
                currentState = State.INIT_BLOCK;
                // fall through
            case INIT_BLOCK:
                blockCompressor = new Bzip2BlockCompressor(writer, streamBlockSize);
                currentState = State.WRITE_DATA;
                // fall through
            case WRITE_DATA:
                if (!in.isReadable()) {
                    return;
                }
                Bzip2BlockCompressor blockCompressor = this.blockCompressor;
                final int length = Math.min(in.readableBytes(), blockCompressor.availableSize());
                final int bytesWritten = blockCompressor.write(in, in.readerIndex(), length);
                in.skipBytes(bytesWritten);
                if (!blockCompressor.isFull()) {
                    if (in.isReadable()) {
                        break;
                    } else {
                        return;
                    }
                }
                currentState = State.CLOSE_BLOCK;
                // fall through
            case CLOSE_BLOCK:
                closeBlock(out);
                currentState = State.INIT_BLOCK;
                break;
            default:
                throw new IllegalStateException();
        }
    }
}
 
Example 20
Source File: SslHandler.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out)
        throws SSLException {
    ByteBuf newDirectIn = null;
    try {
        int readerIndex = in.readerIndex();
        int readableBytes = in.readableBytes();

        // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of
        // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is called.
        final ByteBuffer[] in0;
        if (in.isDirect() || !engineType.wantsDirectBuffer) {
            // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed ByteBuf
            // to calculate the count) we will just assume a CompositeByteBuf contains more then 1 ByteBuf.
            // The worst that can happen is that we allocate an extra ByteBuffer[] in CompositeByteBuf.nioBuffers()
            // which is better then walking the composed ByteBuf in most cases.
            if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) {
                in0 = singleBuffer;
                // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object allocation
                // to a minimum.
                in0[0] = in.internalNioBuffer(readerIndex, readableBytes);
            } else {
                in0 = in.nioBuffers();
            }
        } else {
            // We could even go further here and check if its a CompositeByteBuf and if so try to decompose it and
            // only replace the ByteBuffer that are not direct. At the moment we just will replace the whole
            // CompositeByteBuf to keep the complexity to a minimum
            newDirectIn = alloc.directBuffer(readableBytes);
            newDirectIn.writeBytes(in, readerIndex, readableBytes);
            in0 = singleBuffer;
            in0[0] = newDirectIn.internalNioBuffer(newDirectIn.readerIndex(), readableBytes);
        }

        for (;;) {
            ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes());
            SSLEngineResult result = engine.wrap(in0, out0);
            in.skipBytes(result.bytesConsumed());
            out.writerIndex(out.writerIndex() + result.bytesProduced());

            switch (result.getStatus()) {
            case BUFFER_OVERFLOW:
                out.ensureWritable(engine.getSession().getPacketBufferSize());
                break;
            default:
                return result;
            }
        }
    } finally {
        // Null out to allow GC of ByteBuffer
        singleBuffer[0] = null;

        if (newDirectIn != null) {
            newDirectIn.release();
        }
    }
}