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.rs3e.utility.ByteBufUtils.java

License:Open Source License

public static void writeString(ByteBuf buffer, String str) {
    byte[] bytes = str.getBytes(Charsets.ASCII_LATIN1_CHARSET);
    buffer.writeBytes(bytes);
    buffer.writeByte(0);//from ww w  .  ja va  2 s  . c om
}

From source file:com.rs3e.utility.ByteBufUtils.java

License:Open Source License

public static void writeWorldListString(ByteBuf buffer, String str) {
    buffer.writeByte(0);//from w ww. j a v a  2  s  .c  om
    buffer.writeBytes(str.getBytes(Charsets.ASCII_LATIN1_CHARSET));
    buffer.writeByte(0);
}

From source file:com.sample.netty.socket.client.ClientHandlerOutbound.java

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    ByteBuf encoded = ctx.alloc().buffer(4);
    encoded.writeBytes(((String) msg).getBytes());
    ctx.writeAndFlush(encoded, promise);
    LOG.trace("[CLIENT SEND MENSSAGE] " + msg);
}

From source file:com.sample.netty.socket.server.ServerHandlerOutbound.java

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    ByteBuf encoded = ctx.alloc().buffer(4);
    encoded.writeBytes(((String) msg).getBytes());
    ctx.writeAndFlush(encoded, promise);
    System.err.println("[SERVER SEND MENSSAGE] " + msg);
}

From source file:com.savageboy74.savagetech.network.message.MessageTileEntityBase.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(x);/*w w  w .  ja  v  a  2s. c om*/
    buf.writeInt(y);
    buf.writeInt(z);
    buf.writeByte(orientation);
    buf.writeByte(state);
    buf.writeInt(customName.length());
    buf.writeBytes(customName.getBytes());
    buf.writeInt(owner.length());
    buf.writeBytes(owner.getBytes());
}

From source file:com.schibsted.triathlon.service.RxTestServer.java

License:Apache License

private Observable<Void> handleTest(HttpServerRequest<ByteBuf> request,
        final HttpServerResponse<ByteBuf> response) {
    response.setStatus(HttpResponseStatus.OK);

    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
    byteBuf.writeBytes("test".getBytes(Charset.defaultCharset()));

    response.write(byteBuf);//from  w  w w  .j  a va 2 s  .  com
    return response.close();
}

From source file:com.seagate.kinetic.client.io.provider.nio.udt.UdtTransportProvider.java

License:Open Source License

@Override
public void write(KineticMessage km) throws IOException {

    // get interface message
    Message.Builder message = (Builder) km.getMessage();

    // create extended message
    ExtendedMessage.Builder extendedMessage = ExtendedMessage.newBuilder();

    // set interface message
    extendedMessage.setInterfaceMessage(message);

    // set optional value
    if (km.getValue() != null) {
        extendedMessage.setValue(ByteString.copyFrom(km.getValue()));
    }//from www . j  a  v  a  2s  . c om

    // get byte[] from extended message
    byte[] data = extendedMessage.build().toByteArray();

    final ByteBuf byteBuf = Unpooled.buffer(data.length);
    byteBuf.writeBytes(data);

    UdtMessage udt = new UdtMessage(byteBuf);

    logger.finest("writing udt message.size= " + byteBuf.readableBytes());

    this.channel.writeAndFlush(udt);
}

From source file:com.seagate.kinetic.common.protocol.codec.KineticEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, KineticMessage km, ByteBuf out) throws Exception {

    try {/*w  ww.  j av  a  2 s .  com*/

        // get value to write separately
        // byte[] value = builder.getValue().toByteArray();
        byte[] value = km.getValue();

        // 1. write magic number
        out.writeByte((byte) 'F');

        //get message
        Message.Builder messageBuilder = (Builder) km.getMessage();

        // build message
        Message msg = messageBuilder.build();

        // get proto message bytes
        byte[] protoMessageBytes = msg.toByteArray();

        // 2. write protobuf message message size, 4 byte
        out.writeInt(protoMessageBytes.length);

        // 3. write attached value size, 4 byte
        if (value != null) {
            out.writeInt(value.length);
        } else {
            out.writeInt(0);
        }

        // 4. write protobuf message byte[]
        out.writeBytes(protoMessageBytes);

        // 5 (optional) write attached value if any
        if (value != null && value.length > 0) {
            // write value
            out.writeBytes(value);
        }

        // log message out
        if (printMessage) {

            logger.info("outbound protocol message:");

            String printMsg = ProtocolMessageUtil.toString(km);

            logger.info(printMsg);
        }

    } catch (Exception e) {
        logger.log(Level.WARNING, e.getMessage(), e);
        throw e;
    }
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.udt.UdtRequestProcessRunner.java

License:Open Source License

@Override
public void run() {
    try {//w ww  .j av  a2 s .co m

        // extended request builder
        ExtendedMessage.Builder extendedBuilder = ExtendedMessage.newBuilder().mergeFrom(request);

        // build extended message
        ExtendedMessage extendedMessage = extendedBuilder.build();

        // create kinetic message
        KineticMessage km = new KineticMessage();

        // set interface message
        km.setMessage(extendedMessage.getInterfaceMessage());

        // get command bytes
        ByteString commandBytes = extendedMessage.getInterfaceMessage().getCommandBytes();

        // build command
        Command.Builder commandBuilder = Command.newBuilder();

        try {
            commandBuilder.mergeFrom(commandBytes);
            km.setCommand(commandBuilder.build());
        } catch (InvalidProtocolBufferException e) {
            logger.log(Level.WARNING, e.getMessage(), e);
        }

        // set optiona value
        if (extendedMessage.hasValue()) {
            km.setValue(extendedMessage.getValue().toByteArray());
        }

        // process request
        KineticMessage kmresp = service.processRequest(km);

        // create response builder
        ExtendedMessage.Builder extendedResponse = ExtendedMessage.newBuilder();

        // set response interface message
        extendedResponse.setInterfaceMessage((Message.Builder) kmresp.getMessage());

        // set response value
        if (kmresp.getValue() != null) {
            extendedResponse.setValue(ByteString.copyFrom(kmresp.getValue()));
        }

        // get serialized bytes
        byte[] data = extendedResponse.build().toByteArray();

        ByteBuf byteBuf = Unpooled.buffer(data.length);
        byteBuf.writeBytes(data);

        UdtMessage udt = new UdtMessage(byteBuf);

        ctx.writeAndFlush(udt);
    } catch (Exception e) {
        logger.log(Level.WARNING, e.getMessage(), e);
    }
}

From source file:com.seventh_root.ld33.common.network.packet.clientbound.PublicKeyClientBoundPacket.java

License:Apache License

@Override
public void write(ByteBuf buf) throws UnsupportedEncodingException {
    super.write(buf);
    buf.writeInt(getEncodedPublicKey().length);
    buf.writeBytes(getEncodedPublicKey());
}