Example usage for io.netty.buffer ByteBuf order

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

Introduction

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

Prototype

@Deprecated
public abstract ByteBuf order(ByteOrder endianness);

Source Link

Document

Returns a buffer with the specified endianness which shares the whole region, indexes, and marks of this buffer.

Usage

From source file:tk.jomp16.rcon.internal.netty.RconNettyDecoder.java

License:Open Source License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {
    if (in.readableBytes() < 8) {
        return;/*  w  w w .  j av  a2s. c  om*/
    }

    final ByteBuf byteBuf = in.order(ByteOrder.LITTLE_ENDIAN);

    final int readerIndex = in.readerIndex();
    final int packetLength = byteBuf.readInt();

    if (packetLength < 4 || packetLength > 4096) {
        return;
    }

    if (byteBuf.readableBytes() < packetLength) {
        byteBuf.readerIndex(readerIndex);

        return;
    }

    final int id = byteBuf.readInt();
    final int type = byteBuf.readInt();

    final String body = byteBuf.readBytes(byteBuf.readableBytes() - 4).toString(Charset.forName("UTF-8"));

    final String ip = ctx.channel().remoteAddress().toString().replaceFirst("/", "");

    log.trace("(" + ip + ") - GOT --> [" + id + "][" + type + "] -- "
            + body.replaceAll("[\\r\\n]+", "(newline)"));

    //       log.debug("packetLength=" + packetLength + " id=" + id + " type=" + type + " body=" + body);

    out.add(new RconRequest(id, type, body));
}

From source file:tk.jomp16.rcon.internal.netty.RconNettyEncoder.java

License:Open Source License

@Override
protected void encode(final ChannelHandlerContext ctx, final RconResponse msg, final ByteBuf out)
        throws Exception {
    final String ip = ctx.channel().remoteAddress().toString().replaceFirst("/", "");

    log.trace("(" + ip + ") - SENT --> [" + msg.getId() + "][" + msg.getType() + "] -- "
            + msg.getResponseBody().toString().replaceAll("[\\r\\n]+", "(newline)"));

    out.order(ByteOrder.LITTLE_ENDIAN).writeInt(msg.getResponseBody().toString().length() + 12)
            .writeInt(msg.getId()).writeInt(msg.getType())
            .writeBytes(msg.getResponseBody().toString().getBytes()).writeInt(0);
}