Example usage for io.netty.buffer ByteBufUtil swapInt

List of usage examples for io.netty.buffer ByteBufUtil swapInt

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil swapInt.

Prototype

public static int swapInt(int value) 

Source Link

Document

Toggles the endianness of the specified 32-bit integer.

Usage

From source file:com.cc.nettytest.proxy.decoder.CCLengthFieldBasedFrameDecoder.java

License:Apache License

@Override
public Object decode(ChannelHandlerContext ctx, ByteBuf inBuffer) throws Exception {
    if (discardingTooLongFrame) {
        long bytesToDiscard = this.bytesToDiscard;
        int localBytesToDiscard = (int) Math.min(bytesToDiscard, inBuffer.readableBytes());
        inBuffer.skipBytes(localBytesToDiscard);
        bytesToDiscard -= localBytesToDiscard;
        this.bytesToDiscard = bytesToDiscard;
        failIfNecessary(ctx, false);/*from w ww .  jav  a 2  s.co m*/
        return null;
    }

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

    int actualLengthFieldOffset = inBuffer.readerIndex() + lengthFieldOffset;
    long frameLength;
    switch (lengthFieldLength) {
    case 1:
        frameLength = inBuffer.getUnsignedByte(actualLengthFieldOffset);
        break;
    case 2:
        frameLength = inBuffer.getUnsignedShort(actualLengthFieldOffset);
        break;
    case 3:
        frameLength = inBuffer.getUnsignedMedium(actualLengthFieldOffset);
        break;
    case 4:
        frameLength = ByteBufUtil.swapInt((int) inBuffer.getUnsignedInt(actualLengthFieldOffset)); //SWAP FOR UIMANAGER
        break;
    case 8:
        frameLength = inBuffer.getLong(actualLengthFieldOffset);
        break;
    default:
        throw new Error("should not reach here");
    }

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

    frameLength += lengthAdjustment + lengthFieldEndOffset;

    if (frameLength < lengthFieldEndOffset) {
        inBuffer.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 - inBuffer.readableBytes();
        inBuffer.skipBytes(inBuffer.readableBytes());
        failIfNecessary(ctx, true);
        return null;
    }

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

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

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

From source file:com.cc.nettytest.proxy.encoder.CCLengthFieldPrepender.java

License:Apache License

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

    int length = lengthIncludesLengthFieldLength ? msg.readableBytes() + lengthFieldLength
            : msg.readableBytes();/*www  . j  a  v a2 s  .  c  om*/
    switch (lengthFieldLength) {
    case 1:
        if (length >= 256) {
            throw new IllegalArgumentException("length does not fit into a byte: " + length);
        }
        out.writeByte((byte) length);
        break;
    case 2:
        if (length >= 65536) {
            throw new IllegalArgumentException("length does not fit into a short integer: " + length);
        }
        out.writeShort((short) length);
        break;
    case 3:
        if (length >= 16777216) {
            throw new IllegalArgumentException("length does not fit into a medium integer: " + length);
        }
        out.writeMedium(length);
        break;
    case 4:
        out.writeInt(ByteBufUtil.swapInt((int) length)); //SWAP FOR UIMANAGER
        break;
    case 8:
        out.writeLong(length);
        break;
    default:
        throw new Error("should not reach here");
    }

    out.writeBytes(msg, msg.readerIndex(), msg.readableBytes());
}