Example usage for io.netty.buffer Unpooled buffer

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

Introduction

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

Prototype

public static ByteBuf buffer(int initialCapacity) 

Source Link

Document

Creates a new big-endian Java heap buffer with the specified capacity , which expands its capacity boundlessly on demand.

Usage

From source file:cat.tbq.hospital.nio.echoExample.EchoServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf byteBuf = (ByteBuf) msg;/*w w w.j a v  a  2 s  .  c om*/
    int size = byteBuf.readableBytes();
    for (int i = 0; i < size; i++) {
        System.out.println("===" + (char) byteBuf.getByte(i));
        ;
    }
    ByteBuf firstMessage = Unpooled.buffer(20);
    firstMessage.writeBytes("hello how are you".getBytes());
    ctx.writeAndFlush(firstMessage);
}

From source file:cn.david.main.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler.//w  w  w .jav a 2 s .  c o m
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(EchoClient.SIZE);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:cn.npt.net.tcp.test.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler./*from w ww .  j  a  v a 2  s  .c  om*/
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(128);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:com.ahanda.techops.noty.clientTest.ClientHandler.java

License:Apache License

public void logout(Channel ch) {
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/logout",
            Unpooled.buffer(0));

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);

    l.info("readable bytes {}", request.content().readableBytes());

    // Send the HTTP request.
    ch.writeAndFlush(request);/*from www.  ja v a2 s.  c o  m*/
}

From source file:com.bow.remoting.netty.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler.//from w  w  w . j ava 2 s  .  c  o m
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(256);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:com.cc.nettytest.proxy.decoder.CCLengthFieldBasedFrameDecoder.java

License:Apache License

/**
 * Extract the sub-region of the specified buffer. This method is called by
 * {@link #decode(ChannelInboundHandlerContext, ByteBuf)} for each
 * frame.  The default implementation returns a copy of the sub-region.
 * For example, you could override this method to use an alternative
 * {@link ByteBufFactory}.//from  w  w w.ja  v a 2s. c o m
 * <p>
 * If you are sure that the frame and its content are not accessed after
 * the current {@link #decode(ChannelInboundHandlerContext, ByteBuf)}
 * call returns, you can even avoid memory copy by returning the sliced
 * sub-region (i.e. <tt>return buffer.slice(index, length)</tt>).
 * It's often useful when you convert the extracted frame into an object.
 * Refer to the source code of {@link ObjectDecoder} to see how this method
 * is overridden to avoid memory copy.
 */
protected ByteBuf extractFrame(ByteBuf buffer, int index, int length) {
    ByteBuf frame = Unpooled.buffer(length);
    frame.writeBytes(buffer, index, length);
    return frame;
}

From source file:com.chen.opensourceframework.netty.discard.DiscardClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    this.ctx = ctx;

    content = Unpooled.buffer(DiscardClient.SIZE);

    String src = "Hello World";
    content.writeBytes(src.getBytes());//from   w  w w .  j  av a 2s  . c o  m
    ctx.writeAndFlush(content);
    // Send the initial messages.
    //        generateTraffic();
}

From source file:com.chen.opensourceframework.netty.echo.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler./*from   w  w w. j  a  va  2s.  c om*/
 */
public EchoClientHandler() {
    char[] cha = new char[] { 'H', 'E', 'L', 'L', 'O' };
    firstMessage = Unpooled.buffer(cha.length);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte(cha[i]);
    }
}

From source file:com.chenyang.proxy.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler.//from   w  w  w.j  a  v a2 s.c  om
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(AgentTest.SIZE);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:com.chenyang.proxy.EchoServerHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    byte[] bytes = WebUtil.bulidResponse("OK").toString().getBytes();
    ByteBuf firstMessage = Unpooled.buffer(bytes.length);
    firstMessage.writeBytes(bytes);//from w  w  w .j  ava2s  . c o m
    ctx.writeAndFlush(firstMessage);
}