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:appeng.tile.AEBaseTile.java

License:Open Source License

/**
 * Handles tile entites that are being received by the client as part of a full chunk.
 *//*from w  w w.jav a2  s. c  o  m*/
@Override
public void handleUpdateTag(NBTTagCompound tag) {
    final ByteBuf stream = Unpooled.copiedBuffer(tag.getByteArray("X"));
    if (this.readFromStream(stream)) {
        this.markForUpdate();
    }
}

From source file:appeng.tile.misc.TilePaint.java

License:Open Source License

@TileEvent(TileEventType.WORLD_NBT_READ)
public void readFromNBT_TilePaint(final NBTTagCompound data) {
    if (data.hasKey("dots")) {
        this.readBuffer(Unpooled.copiedBuffer(data.getByteArray("dots")));
    }/*from  www  .j a  v  a  2s  .c om*/
}

From source file:basic.TimeServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;//from w  w  w  .  j  a v a2s.  com
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req, "UTF-8");
    System.out.println("The time server receive order : " + body);
    String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
            ? new java.util.Date(System.currentTimeMillis()).toString()
            : "BAD ORDER";
    ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
    ctx.write(resp);
}

From source file:book.netty.n2defualtC4P2.TimeServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;/* w  w  w.jav  a 2 s.  co m*/
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req, "UTF-8").substring(0,
            req.length - System.getProperty("line.separator").length());
    System.out.println("The time server receive order : " + body + " ; the counter is : " + ++counter);
    String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
            ? new java.util.Date(System.currentTimeMillis()).toString()
            : "BAD ORDER";
    currentTime = currentTime + System.getProperty("line.separator");
    ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
    ctx.writeAndFlush(resp);
}

From source file:book.netty.n3correctC4P3.TimeServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    String body = (String) msg;
    System.out.println("The time server receive order : " + body + " ; the counter is : " + ++counter);
    String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
            ? new java.util.Date(System.currentTimeMillis()).toString()
            : "BAD ORDER";
    currentTime = currentTime + System.getProperty("line.separator");
    ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
    ctx.writeAndFlush(resp);//from   w  w w  .  j  a v a 2s. c  o m
}

From source file:book.netty.n4delimiterC5P1.EchoClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*from w w  w. j a  va2  s . c  o  m*/
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:book.netty.n4delimiterC5P1.EchoClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    // ByteBuf buf = UnpooledByteBufAllocator.DEFAULT.buffer(ECHO_REQ
    // .getBytes().length);
    // buf.writeBytes(ECHO_REQ.getBytes());
    for (int i = 0; i < 10; i++) {
        ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes()));
    }/*from   w ww  .j  a va  2s  .  c o m*/
}

From source file:book.netty.n4delimiterC5P1.EchoServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//from   w ww .  j  a v a2  s .c  o m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:book.netty.n4delimiterC5P1.EchoServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    String body = (String) msg;
    System.out.println("This is " + ++counter + " times receive client : [" + body + "]");
    body += "$_";
    ByteBuf echo = Unpooled.copiedBuffer(body.getBytes());
    ctx.writeAndFlush(echo);/*from  w  w  w  .j  ava  2s .co  m*/
}

From source file:books.netty.frame.delimiter.EchoClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*  w  ww.  java 2 s  .  c  o  m*/
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}