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:com.miko.s4netty.handler.WorkerProviderHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;

        if (is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }/*  w ww .  j  ava  2  s .c om*/

        boolean keepAlive = isKeepAlive(req);

        logger.debug("channelRead keepAlive= " + keepAlive);

        Gson gson = new Gson();
        List<CarDTO> carList = simpleCarService.getAll();

        Map<String, List<CarDTO>> byMake = simpleCarService.getAll().stream()
                .collect(Collectors.groupingBy(CarDTO::getMake));
        System.out.println("byMake = " + byMake);
        System.out.println("byMake Skoda= " + byMake.get("Skoda").iterator().next().getModel());

        JsonElement element = gson.toJsonTree(carList, new TypeToken<List<CarDTO>>() {
        }.getType());
        JsonArray jsonArray = element.getAsJsonArray();

        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
                Unpooled.wrappedBuffer(jsonArray.toString().getBytes()));
        response.headers().set(CONTENT_TYPE, "application/json");
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

        //ignoring KeepAlive
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);

    }
}

From source file:com.mpush.api.protocol.JsonPacket.java

License:Apache License

@Override
public Object toFrame(Channel channel) {
    byte[] json = Json.JSON.toJson(this).getBytes(Constants.UTF_8);
    return new TextWebSocketFrame(Unpooled.wrappedBuffer(json));
}

From source file:com.mpush.api.protocol.Packet.java

License:Apache License

public static ByteBuf getHBPacket() {
    return Unpooled.wrappedBuffer(HB_PACKET_BYTES);
}

From source file:com.mpush.common.message.ByteBufMessage.java

License:Apache License

@Override
public void decode(byte[] body) {
    decode(Unpooled.wrappedBuffer(body));
}

From source file:com.mpush.core.handler.HttpProxyHandler.java

License:Apache License

private ByteBuf getBody(HttpRequestMessage message) {
    return message.body == null ? Unpooled.EMPTY_BUFFER : Unpooled.wrappedBuffer(message.body);
}

From source file:com.mycompany.nettyweb.HttpServerHandler.java

private void defResponse(ChannelHandlerContext ctx, HttpRequest request) {

    String STR = "<center><big><big>-   !!!</big></big></center>";
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
            Unpooled.wrappedBuffer(STR.getBytes()));
    response.headers().set(CONTENT_TYPE, "text/html");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    sendResponse(ctx, request, response);
}

From source file:com.mycompany.nettyweb.HttpServerHandler.java

private void helloResponse(ChannelHandlerContext ctx, HttpRequest request) {
    try {/* w  w  w .j a v  a 2  s  .co  m*/
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer("Hello World!".getBytes()));
    response.headers().set(CONTENT_TYPE, "text/plain");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    sendResponse(ctx, request, response);
    System.out.println();
}

From source file:com.mycompany.nettyweb.HttpServerHandler.java

private void statusResponse(ChannelHandlerContext ctx, HttpRequest request) {
    response = new DefaultFullHttpResponse(HTTP_1_1, OK,
            Unpooled.wrappedBuffer(statistics.toString().getBytes()));
    response.headers().set(CONTENT_TYPE, "text/html");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    sendResponse(ctx, request, response);
}

From source file:com.neoba.DsyncserverHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpContent con) throws Exception {

    ByteBuf in = (ByteBuf) con.copy().content();
    if (in.array().length == 0) {
        String time = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())).toString();
        respondAndFlush(Unpooled.wrappedBuffer(
                ("<h1>Neoba Experimental Server 1</h1>" + time + "<br>Status:OK<br>Please use the client")
                        .getBytes()),//  w ww. j  ava 2  s .c  o  m
                ctx, true);
        logger.info("Browser request - responded");
        return;
    }
    //System.out.println(new String(in.array()));
    msgcount += 1;
    logger.info("Message Received from ip " + ctx.channel().remoteAddress() + ":" + msgcount);
    ConsoleUtils.printhex("request", in.array(), in.array().length);
    MessageInterpreter mi = new MessageInterpreter(ctx, in);
    ByteBuf reply = mi.generateReply();
    ConsoleUtils.printhex("response", reply.array(), reply.array().length);
    respondAndFlush(reply, ctx, false);
    logger.info("Message Responded :" + msgcount);

}

From source file:com.netflix.iep.http.ByteBufsTest.java

License:Apache License

private Observable<ByteBuf> obs(byte[] data) {
    return Observable.just(Unpooled.wrappedBuffer(data));
}