Example usage for io.netty.buffer ByteBuf bytesBefore

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

Introduction

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

Prototype

public abstract int bytesBefore(byte value);

Source Link

Document

Locates the first occurrence of the specified value in this buffer.

Usage

From source file:org.helios.octo.server.streams.StreamOutputAdapter.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, MessageList<Object> msgs, ChannelPromise promise) {
    MessageList<ByteBuf> buffers = msgs.cast();
    ByteBuf acc = ctx.attr(ACC).get();
    if (acc == null) {
        acc = Unpooled.buffer();/*w  ww  .j ava2s.c  o  m*/
        ctx.attr(ACC).set(acc);
    }
    for (ByteBuf b : buffers) {
        acc.writeBytes(b);
    }
    msgs.releaseAllAndRecycle();
    while (true) {
        int loc = acc.bytesBefore(ByteBufIndexFinder.LF);
        if (loc == -1)
            break;
        ByteBuf buf = Unpooled.buffer(10);
        buf.writeLong(System.nanoTime()).writeByte(0).writeByte(streamType);
        //.writeBytes(acc.readBytes(loc+1));
        ChannelHandler se = ctx.pipeline().remove("stringEncoder");
        ctx.write(buf);
        ctx.pipeline().addAfter("out", "stringEncoder", se);
        byte[] bytes = new byte[loc];
        acc.readBytes(bytes);
        String s = new String(bytes);
        log.info("Writing out [" + s + "]");
        ctx.write(s + "\n");
    }
}

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 {//  w w w.ja v a2  s .  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.protocol.decoder.StringObjectDecoder.java

License:Apache License

@Override
public String decode(ByteBuf buf, State state) {
    String status = buf.readBytes(buf.bytesBefore((byte) '\r')).toString(CharsetUtil.UTF_8);
    buf.skipBytes(2);//from   ww w.  j  a  v  a 2  s.  c o  m
    return status;
}

From source file:org.traccar.protocol.AdmProtocolDecoder.java

License:Apache License

private Position parseCommandResponse(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }/*from   w  w w  . j  av a 2 s.c o  m*/

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());

    getLastLocation(position, null);

    int responseTextLength = buf.bytesBefore((byte) 0);
    if (responseTextLength < 0) {
        responseTextLength = CMD_RESPONSE_SIZE - 3;
    }
    position.set(Position.KEY_RESULT, buf.readSlice(responseTextLength).toString(StandardCharsets.UTF_8));

    return position;
}