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:org.ratpackframework.groovy.templating.internal.TemplateCompiler.java

License:Apache License

public CompiledTemplate compile(ByteBuf templateSource, String name)
        throws CompilationFailedException, IOException {
    ByteBuf scriptSource = byteBufAllocator.buffer(templateSource.capacity());
    parser.parse(templateSource, scriptSource);

    String scriptSourceString = scriptSource.toString(CharsetUtil.UTF_8);

    if (verbose && logger.isLoggable(Level.INFO)) {
        logger.info("\n-- script source --\n" + scriptSourceString + "\n-- script end --\n");
    }//from w w  w . j a v a2  s . com

    try {
        Class<DefaultTemplateScript> scriptClass = scriptEngine.compile(name, scriptSourceString);
        return new CompiledTemplate(name, scriptClass);
    } catch (Exception e) {
        throw new InvalidTemplateException(name, "compilation failure", e);
    }
}

From source file:org.ratpackframework.util.internal.IoUtils.java

License:Apache License

public static String utf8String(ByteBuf byteBuf) {
    return byteBuf.toString(CharsetUtil.UTF_8);
}

From source file:org.redisson.client.handler.CommandDecoder.java

License:Apache License

private void decode(ByteBuf in, CommandData<Object, Object> data, List<Object> parts, Channel channel)
        throws IOException {
    int code = in.readByte();
    if (code == '+') {
        ByteBuf rb = in.readBytes(in.bytesBefore((byte) '\r'));
        try {//from  w w w  .ja va2s.c  o  m
            String result = rb.toString(CharsetUtil.UTF_8);
            in.skipBytes(2);

            handleResult(data, parts, result, false, channel);
        } finally {
            rb.release();
        }
    } else if (code == '-') {
        ByteBuf rb = in.readBytes(in.bytesBefore((byte) '\r'));
        try {
            String error = rb.toString(CharsetUtil.UTF_8);
            in.skipBytes(2);

            if (error.startsWith("MOVED")) {
                String[] errorParts = error.split(" ");
                int slot = Integer.valueOf(errorParts[1]);
                String addr = errorParts[2];
                data.tryFailure(new RedisMovedException(slot, addr));
            } else if (error.startsWith("ASK")) {
                String[] errorParts = error.split(" ");
                int slot = Integer.valueOf(errorParts[1]);
                String addr = errorParts[2];
                data.tryFailure(new RedisAskException(slot, addr));
            } else if (error.startsWith("TRYAGAIN")) {
                data.tryFailure(new RedisTryAgainException(error + ". channel: " + channel + " data: " + data));
            } else if (error.startsWith("LOADING")) {
                data.tryFailure(new RedisLoadingException(error + ". channel: " + channel + " data: " + data));
            } else if (error.startsWith("OOM")) {
                data.tryFailure(new RedisOutOfMemoryException(
                        error.split("OOM ")[1] + ". channel: " + channel + " data: " + data));
            } else if (error.contains("-OOM ")) {
                data.tryFailure(new RedisOutOfMemoryException(
                        error.split("-OOM ")[1] + ". channel: " + channel + " data: " + data));
            } else {
                if (data != null) {
                    data.tryFailure(new RedisException(error + ". channel: " + channel + " command: " + data));
                } else {
                    log.error("Error: {} channel: {} data: {}", error, channel, data);
                }
            }
        } finally {
            rb.release();
        }
    } else if (code == ':') {
        Long result = readLong(in);
        handleResult(data, parts, result, false, channel);
    } else if (code == '$') {
        ByteBuf buf = readBytes(in);
        Object result = null;
        if (buf != null) {
            Decoder<Object> decoder = selectDecoder(data, parts);
            result = decoder.decode(buf, state());
        }
        handleResult(data, parts, result, false, channel);
    } else if (code == '*') {
        int level = state().incLevel();

        long size = readLong(in);
        List<Object> respParts;
        if (state().getLevels().size() - 1 >= level) {
            StateLevel stateLevel = state().getLevels().get(level);
            respParts = stateLevel.getParts();
            size = stateLevel.getSize();
        } else {
            respParts = new ArrayList<Object>();
            if (state().isMakeCheckpoint()) {
                state().addLevel(new StateLevel(size, respParts));
            }
        }

        decodeList(in, data, parts, channel, size, respParts);
    } else {
        String dataStr = in.toString(0, in.writerIndex(), CharsetUtil.UTF_8);
        throw new IllegalStateException("Can't decode replay: " + dataStr);
    }
}

From source file:org.redisson.client.handler.CommandEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, CommandData<?, ?> msg, ByteBuf out) throws Exception {
    try {//from w ww  .  j a va2 s  .co  m
        out.writeByte(ARGS_PREFIX);
        int len = 1 + msg.getParams().length;
        if (msg.getCommand().getSubName() != null) {
            len++;
        }
        out.writeBytes(convert(len));
        out.writeBytes(CRLF);

        writeArgument(out, msg.getCommand().getName().getBytes("UTF-8"));
        if (msg.getCommand().getSubName() != null) {
            writeArgument(out, msg.getCommand().getSubName().getBytes("UTF-8"));
        }
        int i = 1;
        for (Object param : msg.getParams()) {
            Encoder encoder = paramsEncoder;
            if (msg.getCommand().getInParamType().size() == 1) {
                if (msg.getCommand().getInParamIndex() == i
                        && msg.getCommand().getInParamType().get(0) == ValueType.OBJECT) {
                    encoder = msg.getCodec().getValueEncoder();
                } else if (msg.getCommand().getInParamIndex() <= i
                        && msg.getCommand().getInParamType().get(0) != ValueType.OBJECT) {
                    encoder = selectEncoder(msg, i - msg.getCommand().getInParamIndex());
                }
            } else {
                if (msg.getCommand().getInParamIndex() <= i) {
                    int paramNum = i - msg.getCommand().getInParamIndex();
                    encoder = selectEncoder(msg, paramNum);
                }
            }

            writeArgument(out, encoder.encode(param));

            i++;
        }

        if (log.isTraceEnabled()) {
            log.trace("channel: {} message: {}", ctx.channel(), out.toString(CharsetUtil.UTF_8));
        }
    } catch (Exception e) {
        msg.getPromise().tryFailure(e);
        throw e;
    }
}

From source file:org.redisson.client.protocol.decoder.ClusterNodesDecoder.java

License:Apache License

@Override
public List<ClusterNodeInfo> decode(ByteBuf buf, State state) throws IOException {
    String response = buf.toString(CharsetUtil.UTF_8);

    List<ClusterNodeInfo> nodes = new ArrayList<ClusterNodeInfo>();
    for (String nodeInfo : response.split("\n")) {
        ClusterNodeInfo node = new ClusterNodeInfo(nodeInfo);
        String[] params = nodeInfo.split(" ");

        String nodeId = params[0];
        node.setNodeId(nodeId);/* w w w  .ja  va  2s .c  om*/

        String addr = params[1];
        node.setAddress(addr);

        String flags = params[2];
        for (String flag : flags.split(",")) {
            String flagValue = flag.toUpperCase().replaceAll("\\?", "");
            node.addFlag(ClusterNodeInfo.Flag.valueOf(flagValue));
        }

        String slaveOf = params[3];
        if (!"-".equals(slaveOf)) {
            node.setSlaveOf(slaveOf);
        }

        if (params.length > 8) {
            for (int i = 0; i < params.length - 8; i++) {
                String slots = params[i + 8];
                if (slots.indexOf("-<-") != -1 || slots.indexOf("->-") != -1) {
                    continue;
                }

                String[] parts = slots.split("-");
                if (parts.length == 1) {
                    node.addSlotRange(
                            new ClusterSlotRange(Integer.valueOf(parts[0]), Integer.valueOf(parts[0])));
                } else if (parts.length == 2) {
                    node.addSlotRange(
                            new ClusterSlotRange(Integer.valueOf(parts[0]), Integer.valueOf(parts[1])));
                }
            }
        }
        nodes.add(node);
    }
    return nodes;
}

From source file:org.redisson.client.protocol.decoder.KeyValueObjectDecoder.java

License:Apache License

@Override
public Object decode(ByteBuf buf, State state) {
    String status = buf.toString(CharsetUtil.UTF_8);
    buf.skipBytes(1);//from  w w  w  .  ja  va 2  s. co  m
    return status;
}

From source file:org.redisson.client.protocol.decoder.ListResultReplayDecoder.java

License:Apache License

@Override
public Object decode(ByteBuf buf, State state) {
    return buf.toString(CharsetUtil.UTF_8);
}

From source file:org.redisson.client.protocol.decoder.ListScanResultReplayDecoder.java

License:Apache License

@Override
public Object decode(ByteBuf buf, State state) {
    return Long.valueOf(buf.toString(CharsetUtil.UTF_8));
}

From source file:org.redisson.client.protocol.decoder.ObjectFirstScoreReplayDecoder.java

License:Apache License

@Override
public Object decode(ByteBuf buf, State state) {
    return new BigDecimal(buf.toString(CharsetUtil.UTF_8)).doubleValue();
}

From source file:org.redisson.client.protocol.decoder.ScoredSortedSetReplayDecoder.java

License:Apache License

@Override
public Object decode(ByteBuf buf, State state) {
    return new BigDecimal(buf.toString(CharsetUtil.UTF_8));
}