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.digitalpetri.opcua.stack.core.channel.headers.AsymmetricSecurityHeader.java

License:Apache License

public static void encode(AsymmetricSecurityHeader header, ByteBuf buffer) {
    String securityPolicy = header.getSecurityPolicyUri();
    buffer.writeInt(securityPolicy.length());
    buffer.writeBytes(securityPolicy.getBytes(Charset.forName("UTF-8")));

    ByteString senderCertificate = header.getSenderCertificate();
    if (senderCertificate.isNull()) {
        buffer.writeInt(-1);/*from ww  w  .j  ava2s . c  om*/
    } else {
        buffer.writeInt(senderCertificate.length());
        buffer.writeBytes(senderCertificate.bytes());
    }

    ByteString receiverThumbprint = header.getReceiverThumbprint();
    if (receiverThumbprint.isNull()) {
        buffer.writeInt(-1);
    } else {
        buffer.writeInt(receiverThumbprint.length());
        buffer.writeBytes(receiverThumbprint.bytes());
    }
}

From source file:com.dingwang.netty.encoder.PersonEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, Person msg, ByteBuf out) throws Exception {

    Gson gson = new Gson();
    String str = gson.toJson(msg);
    System.out.println("str===" + str);

    byte[] src = str.getBytes("UTF-8");

    out.writeBytes(src);
}

From source file:com.dingwang.rpc.encode.RpcEncoder.java

License:Open Source License

@Override
public void encode(ChannelHandlerContext ctx, Object in, ByteBuf out) throws Exception {
    if (genericClass.isInstance(in)) {
        byte[] data = SerializationUtil.serialize(in);
        out.writeInt(data.length);/*from   ww  w. j  av  a2 s  . c  o  m*/
        out.writeBytes(data);
    }
}

From source file:com.dinstone.jrpc.transport.netty4.TransportProtocolEncoder.java

License:Apache License

private void writeFrame(ByteBuf out, byte[] rpcBytes) {
    int objectSize = rpcBytes.length;
    if (objectSize > maxObjectSize) {
        throw new IllegalArgumentException(
                "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')');
    }/*  w w w . j  a  va  2  s  . c o  m*/

    // FrameLen = PrefixLen + RpcObjectSize
    out.writeInt(objectSize);
    out.writeBytes(rpcBytes);
}

From source file:com.diwayou.hybrid.remoting.netty.NettyEncoder.java

License:Apache License

@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out) throws Exception {
    try {// w  w w .ja  v a2s  . c  o m
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        // ? pipelineclose???
        RemotingUtil.closeChannel(ctx.channel());
    }
}

From source file:com.doctor.netty5.example.factorial_algorithm.NumberEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) throws Exception {

    if (msg == null) {
        throw new RuntimeException("not null");
    }/* w  ww  .j  a  v  a  2  s  . co  m*/
    BigInteger integer = null;
    if (msg instanceof BigInteger) {
        integer = (BigInteger) msg;
    } else {
        integer = new BigInteger(String.valueOf(msg));
    }

    byte[] byteArray = integer.toByteArray();
    // ?F +  +  -> 1 + 4 + ?
    out.writeByte((byte) 'F');
    out.writeInt(byteArray.length);
    out.writeBytes(byteArray);
}

From source file:com.dwarf.netty.guide.factorial.NumberEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) {
    // Convert to a BigInteger first for easier implementation.
    BigInteger v;/*w  w  w . j a va2s  .c  om*/
    if (msg instanceof BigInteger) {
        v = (BigInteger) msg;
    } else {
        v = new BigInteger(String.valueOf(msg));
    }

    // Convert the number into a byte array.
    byte[] data = v.toByteArray();
    int dataLength = data.length;

    // Write a message.
    out.writeByte((byte) 'F'); // magic number
    out.writeInt(dataLength); // data length
    out.writeBytes(data); // data
}

From source file:com.easy.remoting.netty.handler.EasyEncoder.java

License:Apache License

@Override
public void encode(ChannelHandlerContext ctx, Cmd cmd, ByteBuf out) throws Exception {
    try {/*from w  w  w .  j  a va2s. c om*/
        ByteBuffer cmdBytes = cmd.encode();
        out.writeBytes(cmdBytes);

    } catch (Exception e) {
        log.error("encode exception: {}", e);

        ctx.channel().close().addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.ebay.jetstream.messaging.transport.netty.compression.MessageCompressionHandler.java

License:MIT License

/**
 * Invoked when {@link Channel#write(Object)} is called.
 *///w w w  .j  a  v  a  2  s .  c o  m
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

    try {

        Attribute<Boolean> attr = ctx.channel().attr(EventProducer.m_eckey);

        Boolean enableCompression = attr.get();

        if ((enableCompression != null) && (enableCompression == true)) {

            ByteBuf chbuf = (ByteBuf) msg;

            int msglen = chbuf.readableBytes();
            ExtendedChannelPromise epromise = (ExtendedChannelPromise) promise;
            epromise.setRawBytes(msglen);

            byte[] compressed = Snappy.rawCompress(chbuf.readBytes(msglen).array(), msglen);

            epromise.setCompressedBytes(compressed.length + 4);
            chbuf.release(); // need to release the original buffer - do I need to check if this this a ref counted buffer

            ByteBuf sendbuf = ctx.alloc().buffer();

            sendbuf.writeInt(compressed.length);
            sendbuf.writeBytes(compressed);

            ctx.write(sendbuf, promise);

            m_totalMessagesCompressed.increment();

        } else {

            ctx.write(msg, promise);

        }

    } catch (Throwable t) {
        m_totalMessagesDropped.increment();
        LOGGER.debug("Failed to compress message - " + t.getLocalizedMessage());

    }

}

From source file:com.eightkdata.nettybson.mongodriver.MongoBSONDocument.java

License:Open Source License

@Override
public void writeToByteBuf(@Nonnull ByteBuf buffer) {
    buffer.writeBytes(new BasicBSONEncoder().encode(bson));
}