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(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:cubicchunks.network.WorldEncoder.java

License:MIT License

public static ByteBuf createByteBufForRead(byte[] data) {
    ByteBuf bytebuf = Unpooled.wrappedBuffer(data);
    bytebuf.readerIndex(0);
    return bytebuf;
}

From source file:de.dfki.kiara.http.HttpRequestMessage.java

License:Open Source License

public HttpRequest finalizeRequest() {
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, getPayload().remaining());
    ByteBuf bbuf = Unpooled.wrappedBuffer(getPayload());
    ByteBuf byteContent = content.content().clear();
    byteContent.writeBytes(bbuf);// w  ww .ja v a  2  s.  c o  m
    bbuf.release();
    return request;
}

From source file:de.dfki.kiara.http.HttpResponseMessage.java

License:Open Source License

public HttpResponse finalizeResponse() {
    HttpResponse httpResponse = getResponse();
    getContent().content().clear();//from  www  .j  a v a2s .c  om

    if (getPayload() != null) {
        ByteBuf bbuf = Unpooled.wrappedBuffer(getPayload());
        getContent().content().writeBytes(bbuf);
    }

    httpResponse.headers().set(CONTENT_LENGTH, getContent().content().readableBytes());

    return httpResponse;
}

From source file:de.dfki.kiara.netty.ByteBufferEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuffer msg, List<Object> out) throws Exception {
    out.add(Unpooled.wrappedBuffer(msg));
}

From source file:de.ocarthon.core.network.HttpClientTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    postParameters = new ArrayList<>();
    postParameters.add(new AbstractMap.SimpleEntry<>("t", "123"));
    defaultHttpClient = new HttpClient("https", HOST);
    testFile = Unpooled.wrappedBuffer("Test".getBytes());
}

From source file:de.ocarthon.core.network.tcp.TestTCPClientServerTest.java

License:Apache License

@Test
public void testClientServerCommunication() throws Exception {
    TCPServer server = new TCPServer(true);
    server.addCustomCodec(p -> {/*from   w w  w . j  ava2 s  . c om*/
    });
    server.setListener(new TCPListener() {
        @Override
        public void onClientConnect(ChannelHandlerContext ctx) {
        }

        @Override
        public void onClientDisconnect(ChannelHandlerContext ctx) {
        }

        @Override
        public void onMessageReceived(ChannelHandlerContext ctx, Object message) {
        }

        @Override
        public void onExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            System.out.println(cause.toString());
        }
    });
    server.initBootstrap();
    assertTrue(server.bind(24313));

    assertEquals(0, server.getClients().size());

    TCPClient client = new TCPClient();
    client.addCustomCodec(p -> {
    });
    client.setListener(this);
    client.initBootstrap();

    assertTrue(client.connect("127.0.0.1", 24313));

    Thread.sleep(1000);
    server.getClients().writeAndFlush(Unpooled.wrappedBuffer("Test".getBytes()));

    synchronized (entry) {
        if (entry[0].isEmpty()) {
            entry.wait();
        }
    }
    assertFalse(entry[0].isEmpty());

    client.getChannel().close();
    client.release();

    server.shutdown();
}

From source file:de.sanandrew.mods.turretmod.network.PacketSyncTileEntity.java

License:Creative Commons License

@Override
public void handleClientMessage(PacketSyncTileEntity packet, EntityPlayer player) {
    assert player != null;
    assert player.world != null;
    TileEntity te = player.world.getTileEntity(new BlockPos(packet.pos));
    if (te instanceof TileClientSync) {
        ByteBuf buf = Unpooled.wrappedBuffer(packet.tileBytes);
        ((TileClientSync) te).fromBytes(buf);
    }//from  w  w  w  .j  ava2  s.  c  o m
}

From source file:de.saxsys.synchronizefx.netty.base.CommandToBinaryByteBuf.java

License:Open Source License

@Override
protected void encode(final ChannelHandlerContext ctx, final List<Command> msg, final List<Object> out)
        throws Exception {
    final ByteBuf buffer = Unpooled.wrappedBuffer(serializer.serialize(msg));
    buffer.retain();/*from www.j a  v  a2  s.  c  o  m*/
    out.add(buffer);
}

From source file:de.saxsys.synchronizefx.netty.websockets.ByteBufToWebSocketFrameCodec.java

License:Open Source License

@Override
protected void decode(final ChannelHandlerContext ctx, final WebSocketFrame msg, final List<Object> out)
        throws Exception {
    if (msg instanceof BinaryWebSocketFrame) {
        ByteBuf content = msg.content();
        // the content is passed to other handlers so they need to be retained.
        content.retain();//from   ww w  .j a v a2s .  c o  m
        fragments.add(content);
        if (msg.isFinalFragment()) {
            if (fragments.size() == 1) {
                out.add(fragments.get(0));
            } else {
                ByteBuf[] array = fragments.toArray(BYTE_BUF_TYPE);
                out.add(Unpooled.wrappedBuffer(array));
            }
            fragments.clear();
        }
    } else if (msg instanceof TextWebSocketFrame) {
        LOG.warn("Recieved a Websocket text frame. This was not expected. Ignoring it.");
    }
}

From source file:deathcap.wsmc.web.HTTPHandler.java

License:Apache License

public void httpRequest(ChannelHandlerContext context, FullHttpRequest request) throws IOException {
    if (!request.getDecoderResult().isSuccess()) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;/*from w ww  . ja v a2  s. co  m*/
    }
    if (request.getUri().equals("/server")) {
        context.fireChannelRead(request);
        return;
    }

    if ((request.getMethod() == OPTIONS || request.getMethod() == HEAD) && request.getUri().equals("/chunk")) {
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
        response.headers().add("Access-Control-Allow-Origin", "*");
        response.headers().add("Access-Control-Allow-Methods", "POST");
        if (request.getMethod() == OPTIONS) {
            response.headers().add("Access-Control-Allow-Headers", "origin, content-type, accept");
        }
        sendHttpResponse(context, request, response);
    }

    if (request.getMethod() != GET) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return;
    }

    // TODO: send browserified page
    if (request.getUri().equals("/")) {
        request.setUri("/index.html");
    }

    InputStream stream = null;
    /*
    if (request.getUri().startsWith("/resources/")) {
    File file = new File(
            plugin.getResourceDir(),
            request.getUri().substring("/resources/".length())
    );
    stream = new FileInputStream(file);
    } else {
    */
    stream = this.getClass().getClassLoader().getResourceAsStream("www" + request.getUri());
    if (stream == null) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND));
        return;
    }
    ByteBufOutputStream out = new ByteBufOutputStream(Unpooled.buffer());
    copyStream(stream, out);
    stream.close();
    out.close();

    ByteBuf buffer = out.buffer();
    if (request.getUri().equals("/index.html")) {
        String page = buffer.toString(CharsetUtil.UTF_8);
        page = page.replaceAll("%SERVERPORT%", Integer.toString(this.port));
        buffer.release();
        buffer = Unpooled.wrappedBuffer(page.getBytes(CharsetUtil.UTF_8));
    }

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, buffer);
    if (request.getUri().startsWith("/resources/")) {
        response.headers().add("Access-Control-Allow-Origin", "*");
    }

    String ext = request.getUri().substring(request.getUri().lastIndexOf('.') + 1);
    String type = mimeTypes.containsKey(ext) ? mimeTypes.get(ext) : "text/plain";
    if (type.startsWith("text/")) {
        type += "; charset=UTF-8";
    }
    response.headers().set(CONTENT_TYPE, type);
    setContentLength(response, response.content().readableBytes());
    sendHttpResponse(context, request, response);

}