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.yeetor.server.HttpServer.java

License:Open Source License

/**
 * ??Response//from  ww  w . ja  v a2s  .com
 * @param ctx
 * @param response
 */
public void writeHttpResponseWithString(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response,
        String dataString) {
    HttpContent content = new DefaultHttpContent(Unpooled.wrappedBuffer(dataString.getBytes()));
    response.headers().set(CONTENT_LENGTH, content.content().readableBytes());
    ctx.write(response);
    ctx.write(content);
    ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}

From source file:com.zextras.modules.chat.server.EventSenderImpl.java

License:Open Source License

public void tryDelivery(String stanza) throws Throwable {
    Channel channel = getChannel();
    ChannelFuture channelFuture = channel
            .writeAndFlush(Unpooled.wrappedBuffer(stanza.getBytes(Charset.defaultCharset()))).sync();
    if (!channelFuture.isSuccess()) {
        throw channelFuture.cause();
    }//  ww w . java 2s .c o m
}

From source file:com.zextras.modules.chat.server.xmpp.netty.OversizeStanzaManager.java

License:Open Source License

public void manageOversize(ChannelHandlerContext ctx) {
    ByteBuf errorMessage;/*  ww w . j  a v a 2  s.  co  m*/
    try {
        errorMessage = Unpooled.wrappedBuffer(SIZE_LIMIT_VIOLATION.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    ChannelFuture future = ctx.writeAndFlush(errorMessage);
    future.awaitUninterruptibly(1000L);
    ctx.channel().close();
}

From source file:com.zextras.modules.chat.server.xmpp.netty.TransparentProxy.java

License:Open Source License

public Future<Channel> connect() {
    final Promise<Channel> channelFuture = new DefaultProgressivePromise<Channel>(
            ImmediateEventExecutor.INSTANCE);

    if (mServerChannel == null) {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(mNettyService.getEventLoopGroup()).channel(NioSocketChannel.class)
                .remoteAddress(new InetSocketAddress(mAccount.getMailHost(), mPort)).handler(new Initializer());

        ChannelFuture serverChannelFuture = bootstrap.connect();
        serverChannelFuture.addListener(new ChannelFutureListener() {
            @Override/*from   w w  w.j a v a  2  s . co  m*/
            public void operationComplete(ChannelFuture future) {
                if (future.isSuccess()) {
                    ChatLog.log.info(
                            "Proxy xmpp requests for " + mAccount.getName() + " to " + mAccount.getMailHost());
                    mServerChannel = future.channel();

                    mServerChannel.write(Unpooled.wrappedBuffer(mStreamInit.getBytes()));
                    mServerChannel.writeAndFlush(Unpooled.wrappedBuffer(mInitialPayload.getBytes()));

                    mServerChannel.pipeline().addLast("proxyToClient", new Proxy(mClientChannel));

                    mClientChannel.pipeline().addLast("proxyToServer", new Proxy(mServerChannel));

                    mServerChannel.closeFuture().addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            mClientChannel.close();
                        }
                    });

                    mClientChannel.closeFuture().addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            mServerChannel.close();
                        }
                    });

                    future.channel().closeFuture();

                    channelFuture.setSuccess(mServerChannel);
                } else {
                    ChatLog.log.info("Cannot proxy xmpp requests for " + mAccount.getName() + " to "
                            + mAccount.getMailHost() + ": " + Utils.exceptionToString(future.cause()));
                    sendInternalError(mClientChannel);
                    mClientChannel.flush().close();
                    channelFuture.setFailure(future.cause());
                }
            }
        });

        return channelFuture;
    } else {
        mServerChannel.pipeline().addLast("proxyToClient", new Proxy(mClientChannel));

        mServerChannel.writeAndFlush(mInitialPayload.getBytes());

        channelFuture.setSuccess(mServerChannel);
        return channelFuture;
    }
}

From source file:com.zhucode.longio.example.service.TestClient.java

License:Open Source License

public EchoClientHandler() {
    //       try {
    //         firstMessage = Unpooled.wrappedBuffer("{data : {user_id: 1000}, cmd : 100}".getBytes("utf-8"));
    //      } catch (UnsupportedEncodingException e) {
    //         e.printStackTrace();
    //      }//from   ww w. j  ava  2  s.  co m
    //------------------------------
    //       User.Data d = User.Data.newBuilder().setUserId(1000).build();
    //       Proto.Message pd = Proto.Message.newBuilder()
    //             .setCmd(101).setSerial(123).setBody(d.toByteString()).build();
    //       
    //       com.zhucode.longio.example.message.User.Data d0;
    //       firstMessage = Unpooled.wrappedBuffer(pd.toByteArray());

    UserMsg um = new UserMsg();
    um.user_id = 1000;

    MessagePackData mpd = new MessagePackData();
    mpd.cmd = 100;
    mpd.serial = 13444;

    try {
        MessagePack mp = new MessagePack();
        mpd.data = mp.write(um);

        mp = new MessagePack();
        firstMessage = Unpooled.wrappedBuffer(mp.write(mpd));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.zhucode.longio.transport.netty.HttpClientHandler.java

License:Open Source License

private ChannelFuture sendForHttp(ChannelHandlerContext ctx, byte[] bytes) {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            uri.toASCIIString(), Unpooled.wrappedBuffer(bytes));

    // http/*from  w ww. j av a  2 s .  co m*/
    request.headers().set(HttpHeaders.Names.HOST, uri.getHost());
    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());

    return ctx.writeAndFlush(request);
}

From source file:com.zhucode.longio.transport.netty.HttpHandler.java

License:Open Source License

private ChannelFuture sendForHttp(ChannelHandlerContext ctx, byte[] bytes) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(bytes));
    response.headers().set(CONTENT_TYPE, "text/json; charset=utf-8");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

    boolean ka = ctx.attr(keepAlive).get();

    if (!ka) {/* w w w  .jav a  2  s.  c o  m*/
        return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(CONNECTION, Values.KEEP_ALIVE);
        return ctx.writeAndFlush(response);
    }
}

From source file:com.zhucode.longio.transport.netty.RawSocketClientHandler.java

License:Open Source License

@Override
public ChannelFuture sendMessage(ChannelHandlerContext ctx, MessageBlock mb) {
    byte[] bytes = new byte[0];
    try {//from   w w w . j  a  va2  s  .c o  m
        bytes = pp.encode(mb);
    } catch (ProtocolException e) {
        e.printStackTrace();
    }
    return ctx.channel().writeAndFlush(Unpooled.wrappedBuffer(bytes));
}

From source file:com.zhucode.longio.transport.netty.RawSocketHandler.java

License:Open Source License

@Override
public ChannelFuture sendMessage(ChannelHandlerContext ctx, MessageBlock mb) {

    byte[] bytes = new byte[0];
    try {/*from w w  w  .  ja  v a 2s . c  om*/
        bytes = pp.encode(mb);

    } catch (ProtocolException e) {
        e.printStackTrace();
    }
    return ctx.channel().writeAndFlush(Unpooled.wrappedBuffer(bytes));
}

From source file:cubicchunks.network.WorldEncoder.java

License:MIT License

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