Example usage for io.netty.buffer Unpooled wrappedBuffer

List of usage examples for io.netty.buffer Unpooled wrappedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled wrappedBuffer.

Prototype

public static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

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);//  ww  w .j  a  v  a 2 s . co m
    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 testHttpHeadersFrameMissingContinuation() throws Exception {
    int length = 5;
    byte flags = 0x20; // 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);
    ByteBuf dataFrame = dataFrame(0, (byte) 0, streamId);
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(headersFrame, dataFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readHeadersFrame(streamId, false, false, exclusive, dependency, weight + 1);
    inOrder.verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);/*  w  w w  .j a va2 s.com*/
}

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)//from  ww w  .  j av  a2s.  co m
            .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)//  ww  w.j  av a2  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 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   ww  w .  j  a v a2 s .co 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   w w w. j a v a2s. co m
            .readHeaderBlock(pushPromiseFrame.slice(HTTP_FRAME_HEADER_SIZE + 4, headerBlockLength));
    inOrder.verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}

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

License:Apache License

@Test
public void testHttpPushPromiseFrameMissingContinuation() throws Exception {
    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 dataFrame = dataFrame(0, (byte) 0, streamId);
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(pushPromiseFrame, dataFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);/*  www .j ava  2 s.c  o  m*/
}

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

License:Apache License

/**
 * Encode an HTTP/2 DATA Frame//  w w w  . j  av  a2s .  co  m
 */
public ByteBuf encodeDataFrame(int streamId, boolean endStream, ByteBuf data) {
    byte flags = endStream ? HTTP_FLAG_END_STREAM : 0;
    ByteBuf header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
    writeFrameHeader(header, data.readableBytes(), HTTP_DATA_FRAME, flags, streamId);
    return Unpooled.wrappedBuffer(header, data);
}

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

License:Apache License

/**
 * Encode an HTTP/2 HEADERS Frame//ww w.j  ava 2  s. c  o  m
 */
public ByteBuf encodeHeadersFrame(int streamId, boolean endStream, boolean exclusive, int dependency,
        int weight, ByteBuf headerBlock) {
    byte flags = endStream ? HTTP_FLAG_END_STREAM : 0;
    boolean hasPriority = exclusive || dependency != HTTP_DEFAULT_DEPENDENCY || weight != HTTP_DEFAULT_WEIGHT;
    if (hasPriority) {
        flags |= HTTP_FLAG_PRIORITY;
    }
    int maxLength = hasPriority ? HTTP_MAX_LENGTH - 5 : HTTP_MAX_LENGTH;
    boolean needsContinuations = headerBlock.readableBytes() > maxLength;
    if (!needsContinuations) {
        flags |= HTTP_FLAG_END_HEADERS;
    }
    int length = needsContinuations ? maxLength : headerBlock.readableBytes();
    if (hasPriority) {
        length += 5;
    }
    int frameLength = hasPriority ? HTTP_FRAME_HEADER_SIZE + 5 : HTTP_FRAME_HEADER_SIZE;
    ByteBuf header = Unpooled.buffer(frameLength);
    writeFrameHeader(header, length, HTTP_HEADERS_FRAME, flags, streamId);
    if (hasPriority) {
        if (exclusive) {
            header.writeInt(dependency | 0x80000000);
        } else {
            header.writeInt(dependency);
        }
        header.writeByte(weight - 1);
        length -= 5;
    }
    ByteBuf frame = Unpooled.wrappedBuffer(header, headerBlock.readSlice(length));
    if (needsContinuations) {
        while (headerBlock.readableBytes() > HTTP_MAX_LENGTH) {
            header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
            writeFrameHeader(header, HTTP_MAX_LENGTH, HTTP_CONTINUATION_FRAME, (byte) 0, streamId);
            frame = Unpooled.wrappedBuffer(frame, header, headerBlock.readSlice(HTTP_MAX_LENGTH));
        }
        header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
        writeFrameHeader(header, headerBlock.readableBytes(), HTTP_CONTINUATION_FRAME, HTTP_FLAG_END_HEADERS,
                streamId);
        frame = Unpooled.wrappedBuffer(frame, header, headerBlock);
    }
    return frame;
}

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

License:Apache License

/**
 * Encode an HTTP/2 PUSH_PROMISE Frame/*from w  w  w. j a  v  a 2 s . c  o m*/
 */
public ByteBuf encodePushPromiseFrame(int streamId, int promisedStreamId, ByteBuf headerBlock) {
    boolean needsContinuations = headerBlock.readableBytes() > HTTP_MAX_LENGTH - 4;
    int length = needsContinuations ? HTTP_MAX_LENGTH - 4 : headerBlock.readableBytes();
    byte flags = needsContinuations ? 0 : HTTP_FLAG_END_HEADERS;
    ByteBuf header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + 4);
    writeFrameHeader(header, length + 4, HTTP_PUSH_PROMISE_FRAME, flags, streamId);
    header.writeInt(promisedStreamId);
    ByteBuf frame = Unpooled.wrappedBuffer(header, headerBlock.readSlice(length));
    if (needsContinuations) {
        while (headerBlock.readableBytes() > HTTP_MAX_LENGTH) {
            header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
            writeFrameHeader(header, HTTP_MAX_LENGTH, HTTP_CONTINUATION_FRAME, (byte) 0, streamId);
            frame = Unpooled.wrappedBuffer(frame, header, headerBlock.readSlice(HTTP_MAX_LENGTH));
        }
        header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
        writeFrameHeader(header, headerBlock.readableBytes(), HTTP_CONTINUATION_FRAME, HTTP_FLAG_END_HEADERS,
                streamId);
        frame = Unpooled.wrappedBuffer(frame, header, headerBlock);
    }
    return frame;
}