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 ByteOrder order();

Source Link

Document

Returns the <a href="http://en.wikipedia.org/wiki/Endianness">endianness</a> of this buffer.

Usage

From source file:at.yawk.dbus.protocol.object.ArrayObject.java

/**
 * Allocate a write buffer that will be written to the given target. Inherits endianness.
 *///  w w w  . j a v  a2 s .  co m
static ByteBuf allocateBufferForWrite(ByteBuf writeTarget) {
    return writeTarget.alloc().buffer().order(writeTarget.order());
}

From source file:com.l2jmobius.commons.network.codecs.LengthFieldBasedFrameEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) {
    final ByteBuf buf = ctx.alloc().buffer(2);
    final short length = (short) (msg.readableBytes() + 2);
    buf.writeShort(buf.order() != ByteOrder.LITTLE_ENDIAN ? Short.reverseBytes(length) : length);
    out.add(buf);/*  w w  w. j  av  a  2 s . c o  m*/
    out.add(msg.retain());
}

From source file:com.l2jmobius.commons.network.codecs.PacketDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    if ((in == null) || !in.isReadable()) {
        return;/*from   w w  w  .  j  av a2  s . co m*/
    }

    if (in.order() != _byteOrder) {
        in = in.order(_byteOrder);
    }

    try {
        final short packetId = in.readUnsignedByte();
        if (packetId >= _incomingPackets.length) {
            LOGGER.finer("Unknown packet: " + Integer.toHexString(packetId));
            return;
        }

        final IIncomingPackets<T> incomingPacket = _incomingPackets[packetId];
        if (incomingPacket == null) {
            LOGGER.finer("Unknown packet: " + Integer.toHexString(packetId));
            return;
        }

        final IConnectionState connectionState = ctx.channel().attr(IConnectionState.ATTRIBUTE_KEY).get();
        if ((connectionState == null) || !incomingPacket.getConnectionStates().contains(connectionState)) {
            // LOGGER.warning(incomingPacket + ": Connection at invalid state: " + connectionState + " Required States: " + incomingPacket.getConnectionStates());
            return;
        }

        final IIncomingPacket<T> packet = incomingPacket.newIncomingPacket();
        if ((packet != null) && packet.read(_client, new PacketReader(in))) {
            out.add(packet);
        }
    } finally {
        // We always consider that we read whole packet.
        in.readerIndex(in.writerIndex());
    }
}

From source file:com.l2jmobius.commons.network.codecs.PacketEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, IOutgoingPacket packet, ByteBuf out) {
    if (out.order() != _byteOrder) {
        out = out.order(_byteOrder);/* w w w  .  j  a va2s.  com*/
    }

    try {
        if (packet.write(new PacketWriter(out))) {
            if (out.writerIndex() > _maxPacketSize) {
                throw new IllegalStateException("Packet (" + packet + ") size (" + out.writerIndex()
                        + ") is bigger than the limit (" + _maxPacketSize + ")");
            }
        } else {
            // Avoid sending the packet
            out.clear();
        }
    } catch (Throwable e) {
        LOGGER.log(Level.WARNING, "Failed sending Packet(" + packet + ")", e);
        // Avoid sending the packet if some exception happened
        out.clear();
    }
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPMessageParser.java

License:Apache License

private boolean parseZMTP2Header(ByteBuf buffer) throws ZMTPMessageParsingException {
    if (buffer.readableBytes() < 2) {
        return false;
    }// w  w w. j a  v  a2 s . co  m
    int flags = buffer.readByte();
    hasMore = (flags & MORE_FLAG) == MORE_FLAG;
    if ((flags & LONG_FLAG) != LONG_FLAG) {
        frameSize = buffer.readByte() & 0xff;
        return true;
    }
    if (buffer.readableBytes() < 8) {
        return false;
    }
    long len;
    if (buffer.order() == BIG_ENDIAN) {
        len = buffer.readLong();
    } else {
        len = ByteBufUtil.swapLong(buffer.readLong());
    }
    if (len > Integer.MAX_VALUE) {
        throw new ZMTPMessageParsingException("Received too large frame: " + len);
    }
    frameSize = (int) len;
    return true;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

/**
 * Helper to decode a ZMTP/1.0 length field
 *
 * @return length/*from   ww  w. j  ava2  s. c o  m*/
 * @throws IndexOutOfBoundsException if there is not enough octets to be read.
 */
static public long decodeLength(final ByteBuf in) {
    long size = in.readByte() & 0xFF;
    if (size == 0xFF) {
        if (in.readableBytes() < 8) {
            return -1;
        }
        if (in.order() == BIG_ENDIAN) {
            size = in.readLong();
        } else {
            size = ByteBufUtil.swapLong(in.readLong());
        }
    }

    return size;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

static void writeLong(final ByteBuf buffer, final long value) {
    if (buffer.order() == BIG_ENDIAN) {
        buffer.writeLong(value);//from ww  w.j a  v  a  2  s.  co  m
    } else {
        buffer.writeLong(ByteBufUtil.swapLong(value));
    }
}

From source file:com.talent.mysql.packet.MysqlResponsePacket.java

License:Open Source License

public PacketWithMeta<Packet> decode(ByteBuf byteBuf) throws DecodeException {
    int capacity = byteBuf.capacity();
    PacketWithMeta<Packet> packetWithMeta = null;
    while (true) {
        if ((capacity - byteBuf.readerIndex()) < MysqlHeader.HEADER_LEN) //?
        {/* w w w. ja va2 s .  c om*/
            if (packetWithMeta != null) {
                packetWithMeta.setPacketLenght(byteBuf.readerIndex());
            }

            return packetWithMeta;
        }

        if (byteBuf.order() == ByteOrder.BIG_ENDIAN) {
            byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);
        }

        MysqlHeader header = MysqlHeaderFactory.borrow();
        header.decode(byteBuf);

        ParseUtils.processReadIndex(byteBuf);

        //?
        if (capacity - byteBuf.readerIndex() < header.getBodyLength()) //?
        {
            if (packetWithMeta != null) {
                packetWithMeta.setPacketLenght(byteBuf.readerIndex());
            }
            return packetWithMeta;
        } else {
            T t = decodeBody(byteBuf, header);
            if (packetWithMeta == null) {
                packetWithMeta = new PacketWithMeta<Packet>();

                List<Packet> packets = new ArrayList<Packet>();
                packets.add(t);

                packetWithMeta.setPackets(packets);
                packetWithMeta.setPacketLenght(byteBuf.readerIndex());
                return packetWithMeta;

                //               com.talent.mysql.packet.factory.MysqlHeaderFactory.returnObj(t.getMysqlHeader());
            } else {
                packetWithMeta.getPackets().add(t);
            }
        }
    }

}

From source file:eu.xworlds.util.raknet.protocol.BaseMessage.java

License:Open Source License

/**
 * writes an unsigned short to byte buf/*w w w  . j ava  2 s  . com*/
 * 
 * @param target target byte buffer.
 * @param value the unsigned short value.
 */
protected void writeUnsignedShort(ByteBuf target, int value) {
    if (value < 0 || value > 0xffff) {
        throw new IllegalArgumentException(value + " exceeds allowed size"); //$NON-NLS-1$
    }
    if (target.order() == ByteOrder.BIG_ENDIAN) {
        target.writeByte(value >> 8);
        target.writeByte(value);
    } else {
        target.writeByte(value);
        target.writeByte(value >> 8);
    }
}

From source file:eu.xworlds.util.raknet.protocol.BaseMessage.java

License:Open Source License

/**
 * writes an unsigned int to byte buf//  w  ww . j  a v a 2s  .c  o  m
 * 
 * @param target target byte buffer.
 * @param value the unsigned short value.
 */
protected void writeUnsignedInt(ByteBuf target, long value) {
    if (value < 0 || value > 0xffffffl) {
        throw new IllegalArgumentException(value + " exceeds allowed size"); //$NON-NLS-1$
    }
    if (target.order() == ByteOrder.BIG_ENDIAN) {
        target.writeByte((int) value >> 32);
        target.writeByte((int) value >> 16);
        target.writeByte((int) value >> 8);
        target.writeByte((int) value);
    } else {
        target.writeByte((int) value);
        target.writeByte((int) value >> 8);
        target.writeByte((int) value >> 16);
        target.writeByte((int) value >> 32);
    }
}