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:nettyClient4.clientDecoder.java

License:Apache License

/**
 * return the  date of ByteBuf// w ww.  jav  a 2s  .  c  om
 * @param ctx
 * @param in
 * @return
 * @throws Exception
 */
private ByteBuf decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    if (discardingTooLongFrame) {
        long bytesToDiscard = this.bytesToDiscard;
        int localBytesToDiscard = (int) Math.min(bytesToDiscard, in.readableBytes());
        in.skipBytes(localBytesToDiscard);
        bytesToDiscard -= localBytesToDiscard;
        this.bytesToDiscard = bytesToDiscard;
        failIfNecessary(ctx, false);
        return null;
    }

    if (in.readableBytes() < lengthFieldEndOffset) {
        return null;
    }

    int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset;

    /**??*/
    //        long frameLength = (in.order(byteOrder)).getUnsignedByte(actualLengthFieldOffset);
    long frameLength = (in.order(byteOrder)).getUnsignedShort(actualLengthFieldOffset);

    if (frameLength < 0) {
        in.skipBytes(lengthFieldEndOffset);
        throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength);
    }

    frameLength += lengthAdjustment + lengthFieldEndOffset;

    if (frameLength < lengthFieldEndOffset) {
        in.skipBytes(lengthFieldEndOffset);
        throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less "
                + "than lengthFieldEndOffset: " + lengthFieldEndOffset);
    }

    if (frameLength > maxFrameLength) {
        // Enter the discard mode and discard everything received so far.
        discardingTooLongFrame = true;
        tooLongFrameLength = frameLength;
        bytesToDiscard = frameLength - in.readableBytes();
        in.skipBytes(in.readableBytes());
        failIfNecessary(ctx, true);
        return null;
    }

    // never overflows because it's less than maxFrameLength
    int frameLengthInt = (int) frameLength;
    if (in.readableBytes() < frameLengthInt) {
        return null;
    }

    if (initialBytesToStrip > frameLengthInt) {
        in.skipBytes(frameLengthInt);
        throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less "
                + "than initialBytesToStrip: " + initialBytesToStrip);
    }
    in.skipBytes(initialBytesToStrip);

    // extract frame
    int readerIndex = in.readerIndex();
    int actualFrameLength = frameLengthInt - initialBytesToStrip;
    ByteBuf frame = extractFrame(in, readerIndex, actualFrameLength, ctx);
    in.readerIndex(readerIndex + actualFrameLength);
    return frame;
}

From source file:nettyClient4.clientDecoder.java

License:Apache License

/**
 *  byte  short//w ww .ja va 2  s. c  o  m
 * return  the length of pack
 * @param  in
 * @param  actualLengthFieldOffset
 * @return
 */
@Deprecated
private long getFrameLength(ByteBuf in, int actualLengthFieldOffset) {
    in = in.order(byteOrder);
    return in.getUnsignedShort(actualLengthFieldOffset);//return  the length

}

From source file:org.apache.tajo.tuple.memory.ResizableMemoryBlock.java

License:Apache License

public ResizableMemoryBlock(ByteBuf buffer, ResizableLimitSpec limitSpec) {
    this.buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);
    this.limitSpec = limitSpec;
    this.memoryAddress = this.buffer.hasMemoryAddress() ? this.buffer.memoryAddress() : 0;
}

From source file:org.bgp4j.netty.protocol.open.OpenPacketDecoderTest.java

License:Apache License

@Test
public void testEncodeBasicOpenPacket() {
    OpenPacket open = new OpenPacket();

    open.setProtocolVersion(4);/* www. j  a  va 2  s . com*/
    open.setAutonomousSystem(64512);
    open.setHoldTime(180);
    open.setBgpIdentifier(((192 << 24) | (168 << 16) | (9 << 8) | 1));

    ByteBuf buffer = allocator.buffer();

    buffer.order(ByteOrder.BIG_ENDIAN);

    open.encodePacket(buffer);

    assertBufferContents(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, // marker
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, // marker
            (byte) 0x00, (byte) 0x1d, // length 29
            (byte) 0x01, // type code OPEN
            (byte) 0x04, // BGP version 4 
            (byte) 0xfc, (byte) 0x00, // Autonomous system 64512 
            (byte) 0x00, (byte) 0xb4, // hold time 180 seconds
            (byte) 0xc0, (byte) 0xa8, (byte) 0x09, (byte) 0x01, /// BGP identifier 192.168.9.1 
            (byte) 0x0, // optional parameter length 0 
    }, buffer);
}

From source file:org.bgp4j.netty.protocol.open.OpenPacketDecoderTest.java

License:Apache License

@Test
public void testEncodeFullPacket() {
    OpenPacket open = new OpenPacket();
    MultiProtocolCapability multiCap = new MultiProtocolCapability();
    RouteRefreshCapability routeRefreshCap = new RouteRefreshCapability();
    AutonomousSystem4Capability as4cap = new AutonomousSystem4Capability();

    open.setProtocolVersion(4);//from  ww w. jav a 2s  . com
    open.setAutonomousSystem(64512);
    open.setHoldTime(180);
    open.setBgpIdentifier(((192 << 24) | (168 << 16) | (9 << 8) | 1));

    multiCap.setAfi(AddressFamily.IPv4);
    multiCap.setSafi(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING);
    open.getCapabilities().add(multiCap);

    open.getCapabilities().add(routeRefreshCap);

    as4cap.setAutonomousSystem(64512);
    open.getCapabilities().add(as4cap);

    ByteBuf buffer = allocator.buffer();

    buffer.order(ByteOrder.BIG_ENDIAN);
    open.encodePacket(buffer);

    assertBufferContents(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, // marker
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, // marker
            (byte) 0x00, (byte) 0x2d, // length 53
            (byte) 0x01, // type code OPEN
            (byte) 0x04, // BGP version 4 
            (byte) 0xfc, (byte) 0x00, // Autonomous system 64512 
            (byte) 0x00, (byte) 0xb4, // hold time 180 seconds
            (byte) 0xc0, (byte) 0xa8, (byte) 0x09, (byte) 0x01, /// BGP identifier 192.168.9.1 
            (byte) 0x10, // optional parameter length 16 octets 
            (byte) 0x02, (byte) 0x0e, // parameter type 2 (capability), length 14 octets 
            (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, // Multi-Protocol capability (type 1), IPv4, Unicast  
            (byte) 0x02, (byte) 0x00, // Route-Refresh capability, length 0 octets
            (byte) 0x41, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0xfc, (byte) 0x00 // 4 octet AS capability, AS 64512            
    }, buffer);
}

From source file:org.clitherproject.clither.server.net.packet.Packet.java

License:Open Source License

@SuppressWarnings("deprecation")
public static String readUTF16(ByteBuf in) {
    in = in.order(ByteOrder.BIG_ENDIAN);
    ByteBuf buffer = in.alloc().buffer();
    char chr;//from ww w. j  a  v a  2  s.  co m
    while (in.readableBytes() > 1 && (chr = in.readChar()) != 0) {
        buffer.writeChar(chr);
    }

    return buffer.toString(Charsets.UTF_16LE);
}

From source file:org.clitherproject.clither.server.net.packet.Packet.java

License:Open Source License

@SuppressWarnings("deprecation")
public static void writeUTF16(ByteBuf out, String s) {
    out.order(ByteOrder.BIG_ENDIAN).writeBytes(s.getBytes(Charsets.UTF_16LE));
    out.writeChar(0);// www . j  av  a 2  s  .c om
}

From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUDecoder.java

License:Open Source License

private APCIBase decode(ByteBuf controlFields, final ByteBuf data) {
    logger.trace("Control Fields: {}", ByteBufUtil.hexDump(controlFields));

    controlFields = controlFields.order(ByteOrder.LITTLE_ENDIAN);

    final byte first = controlFields.getByte(0);
    if ((first & 0x01) == 0) {
        // I format
        final int sendSequenceNumber = controlFields.readUnsignedShort() >> 1;
        final int receiveSequenceNumber = controlFields.readUnsignedShort() >> 1;
        logger.debug("S: {}, R: {}", sendSequenceNumber, receiveSequenceNumber);
        return new InformationTransfer(sendSequenceNumber, receiveSequenceNumber, data);
    } else if ((first & 0x03) == 1) {
        // S format
        controlFields.skipBytes(2);/*from  w ww  .j  av  a2 s .  co m*/
        final int receiveSequenceNumber = controlFields.readUnsignedShort() >> 1;
        return new Supervisory(receiveSequenceNumber);
    } else if ((first & 0x03) == 3) {
        // U format
        final Function function = convertFunction(controlFields.readUnsignedByte());
        return new UnnumberedControl(function);
    }

    // this should actually never happen

    throw new DecoderException("Invalid control fields");
}

From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUEncoder.java

License:Open Source License

private void handleIFormat(final InformationTransfer msg, ByteBuf out) {
    final ByteBuf data = msg.getData();
    try {/* w ww .  java  2 s. com*/
        out = out.order(ByteOrder.LITTLE_ENDIAN);

        final int len = data.readableBytes();

        if (len > Constants.APCI_MAX_DATA_LENGTH) {
            throw new EncoderException(String.format("Packet too big - %s bytes", len));
        }

        out.ensureWritable(6 + len);
        out.writeByte(Constants.START_BYTE);
        out.writeByte(4 + len);
        out.writeShort(msg.getSendSequenceNumber() << 1);
        out.writeShort(msg.getReceiveSequenceNumber() << 1);
        out.writeBytes(data);
    } finally {
        ReferenceCountUtil.release(msg.getData());
    }
}

From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUEncoder.java

License:Open Source License

private void handleSFormat(final Supervisory msg, ByteBuf out) {
    out = out.order(ByteOrder.LITTLE_ENDIAN);

    out.ensureWritable(6);/*from www  . jav a 2s  . c om*/
    out.writeByte(Constants.START_BYTE);
    out.writeByte(4);
    out.writeBytes(new byte[] { 0x01, 0x00 });
    out.writeShort(msg.getReceiveSequenceNumber() << 1);
}