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() 

Source Link

Document

Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

Usage

From source file:com.flowpowered.network.util.ByteBufUtilsTest.java

License:MIT License

@Test
public void testUtf8() throws Exception {
    final ByteBuf test = Unpooled.buffer();
    ByteBufUtils.writeUTF8(test, "Hello");
    final String utf8String = ByteBufUtils.readUTF8(test);
    if (!"Hello".equals(utf8String)) {
        fail("The buffer had hello wrote to it but received " + utf8String + " instead!");
    }//from   www  .jav  a2s . c  o m
    boolean exceptionThrown = false;
    try {
        ByteBufUtils.writeUTF8(test, new String(new byte[Short.MAX_VALUE + 1]));
    } catch (Exception ignore) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        fail("Writing more than Short.MAX_VALUE as a UTF8 String to the ByteBuf should have thrown an exception but it did not!");
    }
}

From source file:com.flowpowered.networking.fake.ChannelHandlerContextFaker.java

License:MIT License

public static FakeChannelHandlerContext setup() {
    if (context == null) {
        alloc();/*from  w  ww  .j av  a2 s.co  m*/
        context = Mockito.mock(FakeChannelHandlerContext.class, Mockito.CALLS_REAL_METHODS);
        channel = Mockito.mock(Channel.class);
        config = Mockito.mock(ChannelConfig.class);
        Mockito.doReturn(channel).when(context).channel();
        Mockito.when(channel.config()).thenReturn(config);
        Mockito.when(config.getAllocator()).thenReturn(alloc);
        Answer<ByteBuf> answer = new Answer<ByteBuf>() {
            @Override
            public ByteBuf answer(InvocationOnMock invocation) throws Throwable {
                ByteBuf buffer = Unpooled.buffer();
                buffer.retain();
                return buffer;
            }
        };
        Mockito.when(alloc.buffer()).thenAnswer(answer);
        Mockito.when(alloc.buffer(Mockito.anyInt())).thenAnswer(answer);
    }
    return context;
}

From source file:com.flysoloing.learning.network.netty.http2.Http2ExampleUtil.java

License:Apache License

/**
 * Reads an InputStream into a byte array.
 * @param input the InputStream./* www.j  a  va  2 s. com*/
 * @return a byte array representation of the InputStream.
 * @throws IOException if an I/O exception of some sort happens while reading the InputStream.
 */
public static ByteBuf toByteBuf(InputStream input) throws IOException {
    ByteBuf buf = Unpooled.buffer();
    int n = 0;
    do {
        n = buf.writeBytes(input, BLOCK_SIZE);
    } while (n > 0);
    return buf;
}

From source file:com.forgetutorials.lib.network.SubPacketTileEntityChild.java

License:LGPL

public ByteBuf populate() {

    ByteBuf buf = Unpooled.buffer();
    try {/*from  w  w  w .j av a  2 s.  c  o m*/
        buf.writeByte(this.packetType.ordinal());
        writeData(buf);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }

    return buf;
}

From source file:com.friz.audio.network.AudioSessionContext.java

License:Open Source License

/**
 * Processes the file requests./*from ww w . j  av  a2 s  .c  o m*/
 * @throws IOException The exception thrown if an i/o error occurs.
 */
public void processFileQueue() {
    FileRequestEvent request;
    synchronized (fileQueue) {
        request = fileQueue.pop();
        if (fileQueue.isEmpty()) {
            idle = true;
        } else {
            service.addAudioContext(this);
            idle = false;
        }
    }
    if (request != null) {
        int type = request.getType();
        int file = request.getFile();
        int crc = request.getCrc();
        int version = request.getVersion();
        HttpVersion http = request.getHttp();

        ByteBuf buf = Unpooled.buffer();
        if (type == 255 && file == 255) {
            buf = Unpooled.wrappedBuffer(server.getCache().getChecksum());
        } else {
            if (server.getCache().getReferenceTable(type).getEntry(file).getCrc() != crc
                    || server.getCache().getReferenceTable(type).getEntry(file).getVersion() != version) {
                writeResponse(http, buf);
                return;
            }

            try {
                buf = Unpooled.wrappedBuffer(server.getCache().getStore().read(type, file));
                if (type != 255)
                    buf = buf.slice(0, buf.readableBytes() - 2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        writeResponse(http, buf);
    }
}

From source file:com.github.nettybook.ch3.EchoClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    String sendMessage = "Hello netty";

    ByteBuf messageBuffer = Unpooled.buffer();
    messageBuffer.writeBytes(sendMessage.getBytes());

    StringBuilder builder = new StringBuilder();
    builder.append(" ? [");
    builder.append(sendMessage);//from w  ww .j a va 2  s  . c om
    builder.append("]");

    System.out.println(builder.toString());
    ctx.writeAndFlush(messageBuffer);
}

From source file:com.github.nettybook.ch4.EchoClientHandler2.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    String sendMessage = "Hello netty";

    ByteBuf messageBuffer = Unpooled.buffer();
    messageBuffer.writeBytes(sendMessage.getBytes());

    StringBuilder builder = new StringBuilder();
    builder.append(" ? [");
    builder.append(sendMessage);/*from   w  w w . j  a v a2 s.c om*/
    builder.append("]");

    System.out.println(builder.toString());
    ctx.write(messageBuffer);

    ctx.flush();
}

From source file:com.github.spapageo.jannel.transcode.TranscoderHelperTest.java

License:Open Source License

@Test
public void testDecodeHeartBeatDecodesCorrectly() throws Exception {
    ByteBuf encodedMessage = Unpooled.buffer();
    encodedMessage.writeInt(124);//from w  ww.  ja  v  a  2s  .co  m

    HeartBeat heartBeat = transcoderHelper.decodeHeartBeat(encodedMessage);
    assertEquals("HeartBeat load is incorrect", 124, heartBeat.getLoad());
    encodedMessage.release();
}

From source file:com.github.spapageo.jannel.transcode.TranscoderHelperTest.java

License:Open Source License

@Test
public void testDecodeAdminDecodesCorrectly() throws Exception {
    ByteBuf encodedMessage = Unpooled.buffer();
    encodedMessage.writeInt(1);/* w ww.j a  v  a  2s .  c  o m*/
    ChannelBufferUtils.writeStringToOctetString("", encodedMessage, Charsets.UTF_8);

    Admin admin = transcoderHelper.decodeAdmin(encodedMessage);
    Assert.assertEquals("Type of admin command is incorrect", AdminCommand.SUSPEND, admin.getAdminCommand());
    assertEquals("Box name is incorrect", "", admin.getBoxId());
    encodedMessage.release();
}

From source file:com.github.spapageo.jannel.transcode.TranscoderHelperTest.java

License:Open Source License

@Test(expected = UnknownAdminCommandException.class)
public void testDecodeAdminThrowsWhenAdminCommandIsUnkwown() throws Exception {
    ByteBuf encodedMessage = Unpooled.buffer();
    encodedMessage.writeInt(5);//w w  w.j  av  a 2  s. c o  m
    ChannelBufferUtils.writeStringToOctetString("", encodedMessage, Charsets.UTF_8);

    try {
        transcoderHelper.decodeAdmin(encodedMessage);
    } finally {
        encodedMessage.release();
    }
}