Example usage for io.netty.buffer Unpooled copiedBuffer

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

Introduction

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

Prototype

public static ByteBuf copiedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new buffer whose content is a merged copy of the specified buffers ' slices.

Usage

From source file:co.rsk.net.discovery.UDPChannel.java

License:Open Source License

void sendPacket(byte[] wire, InetSocketAddress address) {
    DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(wire), address);
    channel.write(packet);//from w  ww  .  j  a v  a  2  s . co m
    channel.flush();
}

From source file:co.rsk.rpc.netty.RskJsonRpcHandlerTest.java

License:Open Source License

@Test
public void handlerDeserializesAndHandlesRequest() throws Exception {
    Channel channel = mock(Channel.class);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(channel);

    when(serializer.deserializeRequest(any())).thenReturn(SAMPLE_SUBSCRIBE_REQUEST);
    when(emitter.subscribe(channel)).thenReturn(SAMPLE_SUBSCRIPTION_ID);
    when(serializer.serializeMessage(any())).thenReturn("serialized");

    DefaultByteBufHolder msg = new DefaultByteBufHolder(Unpooled.copiedBuffer("raw".getBytes()));
    handler.channelRead(ctx, msg);//from   www  .  j  ava 2s  .c  o  m

    verify(ctx, times(1)).writeAndFlush(new TextWebSocketFrame("serialized"));
    verify(ctx, never()).fireChannelRead(any());
}

From source file:co.rsk.rpc.netty.RskJsonRpcHandlerTest.java

License:Open Source License

@Test
public void handlerPassesRequestToNextHandlerOnException() throws Exception {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);

    when(serializer.deserializeRequest(any())).thenThrow(new IOException());

    DefaultByteBufHolder msg = new DefaultByteBufHolder(Unpooled.copiedBuffer("raw".getBytes()));
    handler.channelRead(ctx, msg);/*from  ww  w  .j  a  v a 2 s .  c o  m*/

    verify(ctx, never()).writeAndFlush(any());
    verify(ctx, times(1)).fireChannelRead(msg);
}

From source file:com.addthis.hydra.store.db.DBKey.java

License:Apache License

public static DBKey deltaDecode(byte[] encoding, @Nonnull IPageDB.Key baseKey) {
    ByteBuf buffer = Unpooled.copiedBuffer(encoding);
    long offset = Varint.readSignedVarLong(buffer);
    long id = offset + baseKey.id();
    Raw key;//from ww  w . j  av a2  s .com
    if (buffer.readableBytes() == 0) {
        key = null;
    } else {
        byte[] data = new byte[buffer.readableBytes()];
        buffer.readBytes(data);
        key = Raw.get(data);
    }
    return new DBKey(id, key);
}

From source file:com.andrewkroh.cicso.rtp.NettyRtpSession.java

License:Apache License

/**
 * {@inheritDoc}//from   w w  w.j a va 2s.co  m
 *
 * @throws IllegalStateException
 *             if {@code shutdown} has already been called
 */
@Override
public void sendData(RtpPacket rtpPacket) {
    Preconditions.checkNotNull(rtpPacket);
    checkNotShutdown();

    for (Destination destination : destinations) {
        ByteBuf buffer = Unpooled.copiedBuffer(rtpPacket.getBytes());
        channel.writeAndFlush(new DatagramPacket(buffer, destination.getSocketAddress()));
    }
}

From source file:com.baidu.jprotobuf.pbrpc.transport.handler.RpcDataPackageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, RpcDataPackage msg, List<Object> out) throws Exception {

    RpcDataPackage dataPackage = (RpcDataPackage) msg;

    byte[] encodeBytes = dataPackage.write();
    if (encodeBytes != null) {
        LOG.log(Level.FINE, "Client send content byte size:" + encodeBytes.length);
    }/*from  w w  w.  j  a  v  a2  s.co m*/

    ByteBuf encodedMessage = Unpooled.copiedBuffer(encodeBytes);

    if (chunkSize < 0) {
        out.add(encodedMessage);
        return;
    }

    List<RpcDataPackage> list = dataPackage.chunk(chunkSize);
    for (RpcDataPackage rpcDataPackage : list) {
        encodeBytes = rpcDataPackage.write();
        encodedMessage = Unpooled.copiedBuffer(encodeBytes);
        out.add(encodedMessage);
    }
}

From source file:com.codebullets.external.party.simulator.connections.websocket.inbound.InboundWebSocketConnection.java

License:Apache License

/**
 * Broadcasts a byte buffer message to all connected channels.
 *//*from  www  .ja v a2  s .c o m*/
@Override
public void send(final byte[] buffer) {
    if (connectedChannels != null && !connectedChannels.isEmpty()) {
        ByteBuf binaryData = Unpooled.copiedBuffer(buffer);
        connectedChannels.writeAndFlush(new BinaryWebSocketFrame(binaryData));
    }
}

From source file:com.codebullets.external.party.simulator.connections.websocket.inbound.InboundWebSocketConnection.java

License:Apache License

/**
 * {@inheritDoc}//from w ww  .  j  a v a  2  s  .  co m
 */
@Override
public void send(final ConnectionContext context, final byte[] buffer) {
    if (context instanceof NettyConnectionContext) {
        NettyConnectionContext nettyContext = (NettyConnectionContext) context;
        ByteBuf binaryData = Unpooled.copiedBuffer(buffer);
        nettyContext.getChannel().writeAndFlush(new BinaryWebSocketFrame(binaryData));
    } else {
        LOG.warn("Expected context of type NettyConnectionContext, but was "
                + context.getClass().getSimpleName());
    }
}

From source file:com.comphenix.packetwrapper.play.client.WrapperPlayClientCustomPayload.java

License:Open Source License

/**
 * Update payload contents with a byte array
 *
 * @param content - new payload content/*from  ww  w.j a va 2  s. c o m*/
 */
public void setContents(byte[] content) {
    setContentsBuffer(Unpooled.copiedBuffer(content));
}

From source file:com.comphenix.protocol.compat.netty.independent.IndependentNetty.java

License:Open Source License

@Override
public WrappedByteBuf copiedBuffer(byte[] array) {
    return new NettyByteBuf(Unpooled.copiedBuffer(array));
}