Example usage for io.netty.buffer ByteBuf toString

List of usage examples for io.netty.buffer ByteBuf toString

Introduction

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

Prototype

public abstract String toString(Charset charset);

Source Link

Document

Decodes this buffer's readable bytes into a string with the specified character set name.

Usage

From source file:io.lettuce.core.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addByte() {

    CommandArgs<String, String> args = new CommandArgs<>(codec).add("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();
    args.encode(buffer);/*from  w  w  w . ja v a 2 s  . c  om*/

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:io.lettuce.core.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addByteUsingByteCodec() {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).add("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();
    args.encode(buffer);/* ww  w . j a  v a 2 s  .c  o m*/

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:io.lettuce.core.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addValueUsingByteCodec() {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();
    args.encode(buffer);/*  www  . j a  v  a 2 s .com*/

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:io.lettuce.core.protocol.CommandArgsTest.java

License:Apache License

@Test
public void addKeyUsingByteCodec() {

    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes());

    ByteBuf buffer = Unpooled.buffer();
    args.encode(buffer);/*from   w w w  . jav  a 2 s . c o m*/

    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

From source file:io.lettuce.core.protocol.CommandHandler.java

License:Apache License

/**
 * @see io.netty.channel.ChannelInboundHandlerAdapter#channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
 *//*from   w  ww . ja  va 2  s. c o  m*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    ByteBuf input = (ByteBuf) msg;

    if (!input.isReadable() || input.refCnt() == 0) {
        logger.warn("{} Input not readable {}, {}", logPrefix(), input.isReadable(), input.refCnt());
        return;
    }

    if (debugEnabled) {
        logger.debug("{} Received: {} bytes, {} commands in the stack", logPrefix(), input.readableBytes(),
                stack.size());
    }

    try {
        if (buffer.refCnt() < 1) {
            logger.warn("{} Ignoring received data for closed or abandoned connection", logPrefix());
            return;
        }

        if (debugEnabled && ctx.channel() != channel) {
            logger.debug("{} Ignoring data for a non-registered channel {}", logPrefix(), ctx.channel());
            return;
        }

        if (traceEnabled) {
            logger.trace("{} Buffer: {}", logPrefix(), input.toString(Charset.defaultCharset()).trim());
        }

        buffer.writeBytes(input);

        decode(ctx, buffer);
    } finally {
        input.release();
    }
}

From source file:io.liveoak.common.codec.form.FormURLDecoder.java

License:Open Source License

private ResourceState parse(ByteBuf resource) throws IOException {
    DefaultResourceState state = new DefaultResourceState();

    String content = resource.toString(ENCODING);
    String[] pairs = content.split("&");
    if (pairs.length > 0) {
        for (String pair : pairs) {
            String[] values = pair.split("=");
            if (values.length >= 1) {
                String name = URLDecoder.decode(values[0], ENCODING.name());
                if (values.length == 1) {
                    if (name.isEmpty()) {
                        continue;
                    }/*  w ww .  j a  v a  2s  .com*/
                    state.putProperty(name, null);
                } else {
                    String value = URLDecoder.decode(values[1], ENCODING.name());
                    Object typed = value;
                    if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
                        typed = Boolean.parseBoolean(value);
                    } else if ("null".equalsIgnoreCase(value)) {
                        typed = null;
                    } else {
                        try {
                            typed = Integer.parseInt(value);
                        } catch (NumberFormatException e) {
                            try {
                                typed = Double.parseDouble(value);
                            } catch (NumberFormatException e2) {
                            }
                        }
                    }
                    state.putProperty(name, typed);
                }
            }
        }
    }
    return state;
}

From source file:io.liveoak.container.BasicServerTest.java

License:Open Source License

protected ResourceState decode(HttpResponse response) throws Exception {
    ByteBuf buffer = Unpooled.buffer();
    ByteBufOutputStream out = new ByteBufOutputStream(buffer);
    response.getEntity().writeTo(out);//  w  w  w.  j  a v a 2s. c o m
    out.flush();
    out.close();
    System.err.println("===================");
    System.err.println(buffer.toString(Charset.defaultCharset()));
    System.err.println("===================");
    return system.codecManager().decode(MediaType.JSON, buffer);
}

From source file:io.liveoak.container.BasicServerTest.java

License:Open Source License

protected ResourceState decode(ByteBuf buffer) throws Exception {
    System.err.println("===================");
    System.err.println(buffer.toString(Charset.defaultCharset()));
    System.err.println("===================");
    return system.codecManager().decode(MediaType.JSON, buffer);
}

From source file:io.liveoak.container.codec.json.JSONEncoderTest.java

License:Open Source License

@Test
public void testEmptyObject() throws Exception {
    DefaultResourceState state = new DefaultResourceState("bob");

    ByteBuf buffer = encode(state);
    String encoded = buffer.toString(Charset.defaultCharset());

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> root = mapper.readValue(encoded, Map.class);

    assertThat(root.get(LiveOak.ID)).isEqualTo("bob");
}

From source file:io.liveoak.container.codec.json.JSONEncoderTest.java

License:Open Source License

@Test
public void testEmptyObjectWithURI() throws Exception {
    DefaultResourceState state = new DefaultResourceState("bob");
    state.uri(new URI("/bob"));

    ByteBuf buffer = encode(state);
    String encoded = buffer.toString(Charset.defaultCharset());

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> root = mapper.readValue(encoded, Map.class);

    assertThat(root.get(LiveOak.ID)).isEqualTo("bob");
    assertThat(root.get(LiveOak.SELF)).isNotNull();
    assertThat(((Map) root.get(LiveOak.SELF)).get(LiveOak.HREF)).isEqualTo("/bob");
}