Example usage for io.netty.buffer ByteBuf writeBytes

List of usage examples for io.netty.buffer ByteBuf writeBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writeBytes.

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes.

Usage

From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java

License:Apache License

public static byte[] compressedFrame(ByteBuf uncompressed) {
    ByteBuf compressed = Unpooled.buffer();
    try (ByteBufInputStream is = new ByteBufInputStream(uncompressed, true);
            GZIPOutputStream os = new GZIPOutputStream(new ByteBufOutputStream(compressed))) {
        ByteStreams.copy(is, os);/* w ww. j  a  v  a 2s . c  o  m*/
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(1);
    buf.writeInt(compressed.readableBytes());
    buf.writeBytes(compressed);
    compressed.release();
    byte[] result = ByteBufUtil.getBytes(buf);
    buf.release();
    return result;
}

From source file:com.linecorp.armeria.server.grpc.ArmeriaGrpcServerStreamTest.java

License:Apache License

private static byte[] compressionFrame(byte[] data) {
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(0);//w  ww .j  ava2 s.co  m
    buf.writeInt(data.length);
    buf.writeBytes(data);
    return ByteBufUtil.getBytes(buf);
}

From source file:com.ltln.modules.openflow.core.util.StringByteSerializer.java

License:Apache License

public static void writeTo(final ByteBuf data, final int length, final String value) {
    try {/*w w w  .jav a 2  s .  c om*/
        byte[] name = value.getBytes("ASCII");
        if (name.length < length) {
            data.writeBytes(name);
            for (int i = name.length; i < length; ++i) {
                data.writeByte((byte) 0);
            }
        } else {
            data.writeBytes(name, 0, length - 1);
            data.writeByte((byte) 0);
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.magnet.yak.load.XMPPHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) { // (1)

    String s = "<stream:stream to=\"54.148.43.16\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">\"\r\n";
    final ByteBuf str = ctx.alloc().buffer(s.getBytes().length); // (2)
    str.writeBytes(s.getBytes());
    LOGGER.trace("channelActive : {}");
    final ChannelFuture f = ctx.writeAndFlush(str);
    f.addListener(new ChannelFutureListener() {
        public void operationComplete(ChannelFuture arg0) throws Exception {
            LOGGER.trace("operationComplete : {}");
        }/*from  w w  w  . j av  a  2  s  .  c  o  m*/

    });
    /*final ChannelFuture f = ctx.writeAndFlush(time); // (3)
    f.addListener(new ChannelFutureListener() {
     public void operationComplete(ChannelFuture arg0) throws Exception {
            
     }
            
    }); */// (4)
}

From source file:com.mapr.franz.netty.FranzClientHandler.java

License:Apache License

@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
    ByteBuf out = ctx.nextOutboundByteBuffer();
    out.discardReadBytes();/*from ww w  . j  av a 2 s . co m*/
    out.writeBytes(in);
    ctx.flush();
}

From source file:com.mastfrog.scamper.password.crypto.EncryptingCodec.java

License:Open Source License

protected void compress(ByteBuf in, ByteBuf out) throws IOException {
    byte[] toRead = new byte[in.readableBytes()];
    in.readBytes(toRead);/*from  w ww  . j a v a  2  s  .c  om*/
    byte[] outBound = encrypt.encrypt(toRead);
    out.writeBytes(outBound);
}

From source file:com.mastfrog.scamper.password.crypto.EncryptingCodec.java

License:Open Source License

protected void uncompress(ByteBuf in, ByteBuf out) throws IOException {
    byte[] toWrite = new byte[in.readableBytes()];
    in.readBytes(toWrite);/*ww w  .  j  ava 2  s  .c o  m*/
    byte[] outBound = encrypt.decrypt(toWrite);
    out.writeBytes(outBound);
}

From source file:com.mastfrog.scamper.password.crypto.EncryptingCodecTest.java

License:Open Source License

private ByteBuf bufFor(String data) throws UnsupportedEncodingException {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(data.getBytes("UTF-8"));
    return buf;//  w  ww.  j ava  2 s . co  m
}

From source file:com.mnt.base.stream.netty.NStreamEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, StreamPacket packet, ByteBuf out) throws Exception {

    Attribute<NStreamLightweightParser> parserAttr = ctx.channel().attr(NStreamDecoder.NSTREAM_PARSER_KEY);
    NStreamLightweightParser parser = parserAttr.get();
    if (parser == null) {
        parser = new NStreamLightweightParser();
        parserAttr.set(parser);/*from w  w w. ja  v a  2s .  co  m*/
    }

    ByteArrays source = packet.getSource();

    out.writeByte(StreamPacketDef.BYTE_STREAM_PACKET_START);
    out.writeBytes(BytesUtil.intToBytes(source.getTotalLength()));

    for (ByteArray ba : source.getByteArrays()) {
        out.writeBytes(ba.getBytes(), ba.getPosition(), ba.getLength());
    }

    out.writeBytes(BytesUtil.genSign(source));
    out.writeByte(StreamPacketDef.BYTE_STREAM_PACKET_END);
}

From source file:com.mobius.software.android.iotbroker.mqtt.net.MQEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, MQMessage message, ByteBuf out) throws Exception {
    ByteBuf buf = MQParser.encode(message);
    out.writeBytes(buf);
}