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

The following are Jave code examples for showing how to use ensureWritable() 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: CloudNet   File: ProtocolLengthSerializer.java   Source Code and License Vote up 6 votes
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {

	if (!(msg instanceof ByteBuf)) return;
	ByteBuf in = (ByteBuf) msg;

	int readableBytes = in.readableBytes();
	int lengthByteSpace = getVarIntSize(readableBytes);

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

	out.ensureWritable(lengthByteSpace + readableBytes);
	writeVarInt(readableBytes, out);
	out.writeBytes(in, in.readerIndex(), readableBytes);
}
 
Example 2
Project: xrpc   File: Example.java   Source Code and License Vote up 6 votes
private static FullHttpResponse getDino(XrpcRequest request, List<Dino> dinos) {
  try {
    DinoGetRequest getRequest =
        DinoGetRequest.parseFrom(CodedInputStream.newInstance(request.getData().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.getByteBuf();
      resp.ensureWritable(CodedOutputStream.computeMessageSizeNoTag(getReply), true);
      getReply.writeTo(new ByteBufOutputStream(resp));

      return Recipes.newResponse(
          HttpResponseStatus.OK,
          request.getByteBuf().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 3
Project: neoscada   File: APDUEncoder.java   Source Code and License Vote up 6 votes
private void handleIFormat ( final InformationTransfer msg, ByteBuf out )
{
    final ByteBuf data = msg.getData ();
    try
    {
        out = out.order ( ByteOrder.LITTLE_ENDIAN );

        final int len = data.readableBytes ();

        if ( len > Constants.APCI_MAX_DATA_LENGTH )
        {
            throw new EncoderException ( String.format ( "Packet too big - %s bytes", len ) );
        }

        out.ensureWritable ( 6 + len );
        out.writeByte ( Constants.START_BYTE );
        out.writeByte ( 4 + len );
        out.writeShort ( msg.getSendSequenceNumber () << 1 );
        out.writeShort ( msg.getReceiveSequenceNumber () << 1 );
        out.writeBytes ( data );
    }
    finally
    {
        ReferenceCountUtil.release ( msg.getData () );
    }
}
 
Example 4
Project: candlelight   File: VarInt21FrameEncoder.java   Source Code and License Vote up 6 votes
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception
{
	int i = msg.readableBytes();
	int j = getVarIntSize(i);

	if (j > 3)
	{
		throw new IllegalArgumentException("unable to compress varint of " + i + " bytes into maximum 3 bytes");
	}
	else
	{
		out.ensureWritable(j + i);
		writeVarInt(i, out);
		out.writeBytes(msg, msg.readerIndex(), i);
	}
}
 
Example 5
Project: wecard-server   File: ProtobufVarint32LengthFieldPrepender.java   Source Code and License Vote up 5 votes
@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 6
Project: neoscada   File: APDUEncoder.java   Source Code and License Vote up 5 votes
private void handleSFormat ( final Supervisory msg, ByteBuf out )
{
    out = out.order ( ByteOrder.LITTLE_ENDIAN );

    out.ensureWritable ( 6 );
    out.writeByte ( Constants.START_BYTE );
    out.writeByte ( 4 );
    out.writeBytes ( new byte[] { 0x01, 0x00 } );
    out.writeShort ( msg.getReceiveSequenceNumber () << 1 );
}
 
Example 7
Project: neoscada   File: APDUEncoder.java   Source Code and License Vote up 5 votes
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 8
Project: MooProject   File: Varint32LengthFieldPrepender.java   Source Code and License Vote up 5 votes
protected void encode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf1, ByteBuf paramByteBuf2)
        throws Exception {
    int i = paramByteBuf1.readableBytes();
    int j = CodedOutputStream.computeRawVarint32Size(i);
    paramByteBuf2.ensureWritable(j + i);

    CodedOutputStream localCodedOutputStream = CodedOutputStream.newInstance(new ByteBufOutputStream(paramByteBuf2), j);

    localCodedOutputStream.writeRawVarint32(i);
    localCodedOutputStream.flush();

    paramByteBuf2.writeBytes(paramByteBuf1, paramByteBuf1.readerIndex(), i);
}
 
Example 9
Project: MooProject   File: ProtobufVarint32LengthFieldPrepender.java   Source Code and License Vote up 5 votes
protected void encode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf1, ByteBuf paramByteBuf2)
        throws Exception {
    int i = paramByteBuf1.readableBytes();
    int j = CodedOutputStream.computeRawVarint32Size(i);
    paramByteBuf2.ensureWritable(j + i);

    CodedOutputStream localCodedOutputStream = CodedOutputStream.newInstance(new ByteBufOutputStream(paramByteBuf2), j);

    localCodedOutputStream.writeRawVarint32(i);
    localCodedOutputStream.flush();

    paramByteBuf2.writeBytes(paramByteBuf1, paramByteBuf1.readerIndex(), i);
    paramByteBuf2.release();
}
 
Example 10
Project: FlexMC   File: MessagePrepender.java   Source Code and License Vote up 5 votes
@Override
protected void encode( ChannelHandlerContext channelHandlerContext, ByteBuf a, ByteBuf byteBuf ) throws Exception {
    int i = a.readableBytes();
    int j = Message.getVarIntSize( i );
    if ( j > 3 ) {
        throw new IllegalArgumentException( "unable to fit " + i + " into " + 3 );
    }

    byteBuf.ensureWritable( j + i );
    Message.writeVarInt( i, byteBuf );
    byteBuf.writeBytes( a, a.readerIndex(), i );
}