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.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeTwoEnquireLinkRespButWithReadRequiredInBetween() throws Exception {
    // f1 missing on end at first
    ByteBuf buffer = BufferHelper/*from   w w  w  .j  a v  a2 s .  c o  m*/
            .createBuffer("0000001080000015000000000a342eed0000001080000015000000000a342e");

    EnquireLinkResp pdu0 = (EnquireLinkResp) transcoder.decode(buffer);

    Assert.assertEquals(16, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_ENQUIRE_LINK_RESP, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(171192045, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isResponse());

    Assert.assertEquals(15, buffer.readableBytes());

    // second enquireLink response (but missing 1 byte)
    pdu0 = (EnquireLinkResp) transcoder.decode(buffer);
    Assert.assertNull(pdu0);

    Assert.assertEquals(15, buffer.readableBytes());

    // add 1 more byte (should finish the byte array off)
    ByteBuf buffer0 = BufferHelper.createBuffer("f1");
    ByteBuf buffer1 = Unpooled.wrappedBuffer(buffer, buffer0); // merge both buffers...
    buffer = buffer1;

    pdu0 = (EnquireLinkResp) transcoder.decode(buffer);

    Assert.assertEquals(16, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_ENQUIRE_LINK_RESP, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(171192049, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isResponse());

    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.facebook.nifty.core.NiftyDispatcher.java

License:Apache License

private ByteBuf addFraming(ChannelHandlerContext ctx, ByteBuf response, ThriftTransportType transportType) {
    if (!response.isReadable()) {
        // Empty response from one-way message, don't frame it
        return response;
    }/*from  w  ww  .  ja v a  2s. c om*/

    if (transportType == ThriftTransportType.UNFRAMED) {
        return response;
    } else if (transportType == ThriftTransportType.FRAMED) {
        ByteBuf frameSizeBuffer = ctx.alloc().buffer(4);
        frameSizeBuffer.writeInt(response.readableBytes());
        return Unpooled.wrappedBuffer(frameSizeBuffer, response);
    } else {
        throw new UnsupportedOperationException("Header protocol is not supported");
    }
}

From source file:com.flowpowered.network.pipeline.MessageEncoder.java

License:MIT License

@Override
protected void encode(ChannelHandlerContext ctx, Message message, List<Object> out) throws Exception {
    final Protocol protocol = messageHandler.getSession().getProtocol();
    final Class<? extends Message> clazz = message.getClass();
    CodecRegistration reg = protocol.getCodecRegistration(message.getClass());
    if (reg == null) {
        throw new Exception("Unknown message type: " + clazz + ".");
    }/*  w  w w.  j  a v a2s .  c  o  m*/
    ByteBuf messageBuf = ctx.alloc().buffer();
    messageBuf = reg.getCodec().encode(messageBuf, message);

    ByteBuf headerBuf = ctx.alloc().buffer();
    headerBuf = protocol.writeHeader(headerBuf, reg, messageBuf);
    out.add(Unpooled.wrappedBuffer(headerBuf, messageBuf));
}

From source file:com.ghrum.common.protocol.MessageLookupService.java

License:Apache License

/**
 * Encodes a {@link Message} into a stream
 *
 * @param message the message to encode to the buffer
 * @return a wrapped buffer that contains the header and the body of the message
 * @throws java.io.IOException//  w w w  . j  a  v  a  2s.c  o  m
 */
@SuppressWarnings("unchecked")
protected <T extends Message> ByteBuf encode(T message) throws IOException {
    final MessageCodec<Message> codec = (MessageCodec<Message>) getCodec(message.getClass());
    if (codec == null) {
        throw new IOException("Unknown operation class: " + message.getClass());
    }
    final ByteBuf body = codec.encode(message);
    final ByteBuf header = Unpooled.buffer(3).writeByte(codec.getOpcode()).writeShort(body.capacity());
    return Unpooled.wrappedBuffer(header, body);
}

From source file:com.github.milenkovicm.kafka.handler.CompositeProducerHandler.java

License:Apache License

ByteBuf creteProduceRequest(ByteBufAllocator allocator, ByteBuf messageSet, String topic) {
    final int messageSetSize = messageSet.readableBytes();

    // a bit hardcoded logic follows
    // total length is length of the all fields
    // i've pre-calculated sizes of arrays and strings in case of
    // topic and client id
    final int totalLength = 22 + messageSetSize + Convert.sizeOfString(topicNameEncoded)
            + Convert.sizeOfString(clientIdEncoded);
    // 18 + clientId.length
    ByteBuf header = allocator/*from  w w w  . ja  v  a 2 s. c om*/
            .buffer(24 + Convert.sizeOfString(topicNameEncoded) + Convert.sizeOfString(clientIdEncoded));
    createMessageHeader(totalLength, header);

    updateCrc(messageSet);
    return Unpooled.wrappedBuffer(header, messageSet);

}

From source file:com.liferay.nativity.control.findersync.FSNativityControlImpl.java

License:Open Source License

@Override
public String sendMessage(NativityMessage message) {
    try {/* w w  w. j  a  va  2s .  co m*/
        byte[] messageBytes = _objectMapper.writeValueAsBytes(message);

        ByteBuf byteBuf = Unpooled.wrappedBuffer(messageBytes, _RETURN_NEW_LINE);

        _channelGroup.writeAndFlush(byteBuf);
    } catch (Exception e) {
        _logger.error(e.getMessage(), e);
    }

    return "";
}

From source file:com.trinity.engine.protocol.MessageLookupService.java

License:Apache License

/**
 * Encodes a message into a stream//from w w  w .  ja v a2 s.c o m
 *
 * @param message the message to encode to the buffer
 * @return a wrapped buffer that contains the header and the body of the message
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public <T extends Message> ByteBuf encode(T message) throws IOException {
    final MessageCodec<Message> codec = (MessageCodec<Message>) getCodec(message.getClass());
    if (codec == null) {
        throw new IOException("Unknown operation class: " + message.getClass());
    }
    final ByteBuf body = codec.encode(message);
    final ByteBuf header = Unpooled.buffer(3).writeByte(codec.getOpcode()).writeShort(body.capacity());
    return Unpooled.wrappedBuffer(header, body);
}

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

License:Apache License

@Test
public void testContinuedHttpHeadersFrame() throws Exception {
    int headerBlockLength = 16;
    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 continuationFrame = continuationFrame(headerBlockLength, (byte) 0x04, streamId); // END_HEADERS
    writeRandomData(continuationFrame, headerBlockLength);
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(headersFrame, continuationFrame)));

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readHeadersFrame(streamId, false, false, exclusive, dependency, weight + 1);
    inOrder.verify(delegate)//from   w w w. ja v a  2 s.  c  o 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 testHttpHeadersFrameEmptyContinuation() 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) 0x04, 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);/*from  ww  w  . j a v a2s. c o m*/
    inOrder.verify(delegate).readHeaderBlock(headersFrame.slice(HTTP_FRAME_HEADER_SIZE, length));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

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);//from   ww w . j av a2  s. c om
    inOrder.verify(delegate).readHeaderBlock(headersFrame.slice(HTTP_FRAME_HEADER_SIZE, 16));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}