Example usage for io.netty.buffer ByteBuf slice

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

Introduction

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

Prototype

public abstract ByteBuf slice(int index, int length);

Source Link

Document

Returns a slice of this buffer's sub-region.

Usage

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpHeadersFrameMultipleContinuations() throws Exception {
    int headerBlockLength = 16;
    int length = 5 + headerBlockLength;
    byte flags = 0x01 | 0x20; // END_STREAM | PRIORITY
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    boolean exclusive = RANDOM.nextBoolean();
    int dependency = RANDOM.nextInt() & 0x7FFFFFFF;
    int weight = RANDOM.nextInt() & 0xFF;

    ByteBuf headersFrame = headersFrame(length, flags, streamId);
    writePriorityFields(headersFrame, exclusive, dependency, weight);
    writeRandomData(headersFrame, headerBlockLength);
    ByteBuf continuationFrame1 = continuationFrame(0, (byte) 0x00, streamId);
    ByteBuf continuationFrame2 = continuationFrame(0, (byte) 0x04, streamId); // END_HEADERS
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(headersFrame, continuationFrame1, continuationFrame2)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readHeadersFrame(streamId, true, false, exclusive, dependency, weight + 1);
    inOrder.verify(delegate).readHeaderBlock(headersFrame.slice(HTTP_FRAME_HEADER_SIZE + 5, headerBlockLength));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);//  ww w  . java 2s. co  m
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpHeadersFrameContinuationReservedFlags() throws Exception {
    int length = 16;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf headersFrame = headersFrame(length, flags, streamId);
    writeRandomData(headersFrame, length);
    ByteBuf continuationFrame = continuationFrame(0, (byte) 0xE7, streamId); // END_HEADERS
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(headersFrame, continuationFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readHeadersFrame(streamId, false, false, false, HTTP_DEFAULT_DEPENDENCY,
            HTTP_DEFAULT_WEIGHT);/*w  w w  . j  a  v a 2  s.  co m*/
    inOrder.verify(delegate).readHeaderBlock(headersFrame.slice(HTTP_FRAME_HEADER_SIZE, 16));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpHeadersFrameContinuationIllegalStreamId() throws Exception {
    int length = 16;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf headersFrame = headersFrame(length, flags, streamId);
    writeRandomData(headersFrame, length);
    // different stream identifier
    ByteBuf continuationFrame = continuationFrame(0, (byte) 0x04, streamId + 1); // END_HEADERS
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(headersFrame, continuationFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readHeadersFrame(streamId, false, false, false, HTTP_DEFAULT_DEPENDENCY,
            HTTP_DEFAULT_WEIGHT);/*from   w w  w .j a  va 2 s .  c om*/
    inOrder.verify(delegate).readHeaderBlock(headersFrame.slice(HTTP_FRAME_HEADER_SIZE, 16));
    inOrder.verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpPushPromiseFrame() throws Exception {
    int headerBlockLength = 16;
    int length = 4 + headerBlockLength;
    byte flags = 0x04; // END_HEADERS
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf frame = pushPromiseFrame(length, flags, streamId);
    frame.writeInt(promisedStreamId);/*from  w  w w  .  j  ava  2s.co  m*/
    writeRandomData(frame, headerBlockLength);
    decoder.decode(frame);

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate).readHeaderBlock(frame.slice(HTTP_FRAME_HEADER_SIZE + 4, headerBlockLength));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testContinuedHttpPushPromiseFrame() throws Exception {
    int headerBlockLength = 16;
    int length = 4;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf pushPromiseFrame = pushPromiseFrame(length, flags, streamId);
    pushPromiseFrame.writeInt(promisedStreamId);
    ByteBuf continuationFrame = continuationFrame(headerBlockLength, (byte) 0x04, streamId); // END_HEADERS
    writeRandomData(continuationFrame, headerBlockLength);
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(pushPromiseFrame, continuationFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate)//  w w w.j  ava2 s .  c om
            .readHeaderBlock(continuationFrame.slice(HTTP_FRAME_HEADER_SIZE, headerBlockLength));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpPushPromiseFrameEmptyContinuation() throws Exception {
    int headerBlockLength = 16;
    int length = 4 + headerBlockLength;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf pushPromiseFrame = pushPromiseFrame(length, flags, streamId);
    pushPromiseFrame.writeInt(promisedStreamId);
    writeRandomData(pushPromiseFrame, headerBlockLength);
    ByteBuf continuationFrame = continuationFrame(0, (byte) 0x04, streamId); // END_HEADERS
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(pushPromiseFrame, continuationFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate)/*from ww w .  j  a  v a  2  s.com*/
            .readHeaderBlock(pushPromiseFrame.slice(HTTP_FRAME_HEADER_SIZE + 4, headerBlockLength));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpPushPromiseFrameMultipleContinuations() throws Exception {
    int headerBlockLength = 16;
    int length = 4 + headerBlockLength;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf pushPromiseFrame = pushPromiseFrame(length, flags, streamId);
    pushPromiseFrame.writeInt(promisedStreamId);
    writeRandomData(pushPromiseFrame, headerBlockLength);
    ByteBuf continuationFrame1 = continuationFrame(0, (byte) 0x00, streamId);
    ByteBuf continuationFrame2 = continuationFrame(0, (byte) 0x04, streamId); // END_HEADERS
    decoder.decode(/*from   w ww  . j  a v  a  2s.c  om*/
            releaseLater(Unpooled.wrappedBuffer(pushPromiseFrame, continuationFrame1, continuationFrame2)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate)
            .readHeaderBlock(pushPromiseFrame.slice(HTTP_FRAME_HEADER_SIZE + 4, headerBlockLength));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpPushPromiseFrameContinuationReservedFlags() throws Exception {
    int headerBlockLength = 16;
    int length = 4 + headerBlockLength;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf pushPromiseFrame = pushPromiseFrame(length, flags, streamId);
    pushPromiseFrame.writeInt(promisedStreamId);
    writeRandomData(pushPromiseFrame, headerBlockLength);
    ByteBuf continuationFrame = continuationFrame(0, (byte) 0xE7, streamId); // END_HEADERS
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(pushPromiseFrame, continuationFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate)/*from  w  w w .  j a v  a2 s.  c  o m*/
            .readHeaderBlock(pushPromiseFrame.slice(HTTP_FRAME_HEADER_SIZE + 4, headerBlockLength));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpPushPromiseFrameContinuationIllegalStreamId() throws Exception {
    int headerBlockLength = 16;
    int length = 4 + headerBlockLength;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf pushPromiseFrame = pushPromiseFrame(length, flags, streamId);
    pushPromiseFrame.writeInt(promisedStreamId);
    writeRandomData(pushPromiseFrame, headerBlockLength);
    // different stream identifier
    ByteBuf continuationFrame = continuationFrame(0, (byte) 0x04, streamId + 1); // END_HEADERS
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(pushPromiseFrame, continuationFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate)//from   www  . java 2s  . co  m
            .readHeaderBlock(pushPromiseFrame.slice(HTTP_FRAME_HEADER_SIZE + 4, headerBlockLength));
    inOrder.verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}

From source file:com.whizzosoftware.hobson.dsc.api.codec.DSCFrameDecoder.java

License:Open Source License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    logger.trace("decode: {}", buffer.toString(CharsetUtil.UTF_8));

    ByteBuf frame = (ByteBuf) super.decode(ctx, buffer);

    if (frame != null) {
        try {//from  w ww .j a  v a 2s  .  c om
            if (frame.readableBytes() >= 3) {
                String cmdId = new String(new byte[] { frame.getByte(0), frame.getByte(1), frame.getByte(2) });
                switch (cmdId) {
                case CodeRequired.ID:
                    return new CodeRequired();
                case CommandAcknowledge.ID:
                    return new CommandAcknowledge();
                case CommandError.ID:
                    return new CommandError();
                case LCDUpdate.ID:
                    int lineNumber = frame.getByte(3) - '0';
                    int columnNumber = Integer
                            .parseInt(new String(new byte[] { frame.getByte(4), frame.getByte(5) }));
                    int length = Integer
                            .parseInt(new String(new byte[] { frame.getByte(6), frame.getByte(7) }));
                    return new LCDUpdate(lineNumber, columnNumber,
                            frame.slice(8, length).toString(CharsetUtil.UTF_8));
                case LEDStatus.ID:
                    return new LEDStatus(LEDStatus.LEDType.forOrdinal(frame.getByte(3) - '0'),
                            LEDStatus.Status.forOrdinal(frame.getByte(4) - '0'));
                case SoftwareVersion.ID:
                    return new SoftwareVersion(frame.slice(3, 4).toString(CharsetUtil.UTF_8));
                case PartitionBusy.ID:
                    return new PartitionBusy(frame.getByte(3) - '0');
                case PartitionNotReady.ID:
                    return new PartitionNotReady(frame.getByte(3) - '0');
                case PartitionReady.ID:
                    return new PartitionReady(frame.getByte(3) - '0');
                case TroubleLEDOff.ID:
                    return new TroubleLEDOff(frame.getByte(3) - '0');
                case ZoneOpen.ID:
                    return new ZoneOpen(Integer.parseInt(
                            new String(new byte[] { frame.getByte(3), frame.getByte(4), frame.getByte(5) })));
                case ZoneRestored.ID:
                    return new ZoneRestored(Integer.parseInt(
                            new String(new byte[] { frame.getByte(3), frame.getByte(4), frame.getByte(5) })));
                case TimeDateBroadcast.ID:
                    return new TimeDateBroadcast(frame.slice(3, 10).toString(CharsetUtil.UTF_8));
                default:
                    logger.debug("Ignoring unknown command frame: {}", buffer.toString(CharsetUtil.UTF_8));
                    return null;
                }
            } else {
                throw new CorruptedFrameException("Frame should not be less than 3 bytes");
            }
        } finally {
            frame.release();
        }
    } else {
        return null;
    }
}