Example usage for io.netty.buffer ByteBuf forEachByte

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

Introduction

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

Prototype

public abstract int forEachByte(ByteProcessor processor);

Source Link

Document

Iterates over the readable bytes of this buffer with the specified processor in ascending order.

Usage

From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java

License:Apache License

/**
 * Finds the position of the correct closing character, taking into account the fact that before the correct one,
 * other sub section with same opening and closing characters can be encountered.
 *
 * @param buf the {@link ByteBuf} where to search for the end of a section enclosed in openingChar and closingChar.
 * @param openingChar the section opening char, used to detect a sub-section.
 * @param closingChar the section closing char, used to detect the end of a sub-section / this section.
 * @return//from w w  w  .  j  a va2s  .com
 */
private static int findSectionClosingPosition(ByteBuf buf, char openingChar, char closingChar) {
    return buf.forEachByte(new ClosingPositionBufProcessor(openingChar, closingChar, true));
}

From source file:com.heliosapm.streams.collector.groovy.ByteBufReaderSource.java

License:Apache License

/**
  * Returns the index in the buffer of the end of line found.
  * Returns -1 if no end of line was found in the buffer.
  *//*  www .jav  a 2  s .  c om*/
private static int findEndOfLine(final ByteBuf buffer) {
    int i = buffer.forEachByte(ByteProcessor.FIND_LF);
    if (i > 0 && buffer.getByte(i - 1) == '\r') {
        i--;
    }
    return i;
}

From source file:com.xx_dev.apn.socks.local.FakeHttpClientDecoder.java

License:Apache License

protected void _decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    switch (this.state()) {
    case READ_FAKE_HTTP: {
        int fakeHttpHeadStartIndex = in.readerIndex();

        int fakeHttpHeadEndIndex = in.forEachByte(new ByteBufProcessor() {
            int c = 0;

            @Override//from   w ww  . java  2s.  c o  m
            public boolean process(byte value) throws Exception {

                if (value == '\r' || value == '\n') {
                    c++;
                } else {
                    c = 0;
                }

                //logger.info("value=" + value + ", c=" + c);

                if (c >= 4) {
                    return false;
                } else {
                    return true;
                }
            }
        });

        logger.debug("s: " + fakeHttpHeadStartIndex);
        logger.debug("e: " + fakeHttpHeadEndIndex);

        if (fakeHttpHeadEndIndex == -1) {
            logger.warn("w: " + fakeHttpHeadStartIndex);
            break;
        }

        byte[] buf = new byte[fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1];
        in.readBytes(buf, 0, fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1);
        String s = TextUtil.fromUTF8Bytes(buf);

        //logger.info(s);

        String[] ss = StringUtils.split(s, "\r\n");

        //System.out.println(s + "" + this + " " + Thread.currentThread().getName());

        for (String line : ss) {
            if (StringUtils.startsWith(line, "X-C:")) {
                String lenStr = StringUtils.trim(StringUtils.split(line, ":")[1]);
                //System.out.println(lenStr + "" + this + " " + Thread.currentThread().getName());
                //System.out.println("*****************************************");
                try {
                    length = Integer.parseInt(lenStr, 16);
                    trafficLogger.info("D," + LocalConfig.ins().getUser() + "," + length);
                } catch (Throwable t) {
                    logger.error("--------------------------------------");
                    logger.error(s + "" + this + " " + Thread.currentThread().getName());
                    logger.error("--------------------------------------");
                }

            }
        }

        this.checkpoint(STATE.READ_CONTENT);
    }
    case READ_CONTENT: {
        if (length > 0) {
            byte[] buf = new byte[length];
            in.readBytes(buf, 0, length);

            byte[] res = new byte[length];

            for (int i = 0; i < length; i++) {
                res[i] = (byte) (buf[i] ^ (LocalConfig.ins().getEncryptKey() & 0xFF));
            }

            ByteBuf outBuf = ctx.alloc().buffer();

            outBuf.writeBytes(res);

            out.add(outBuf);
        }

        this.checkpoint(STATE.READ_FAKE_HTTP);
        break;
    }
    default:
        throw new Error("Shouldn't reach here.");
    }

}

From source file:com.xx_dev.apn.socks.remote.FakeHttpServerDecoder.java

License:Apache License

protected void _decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    switch (this.state()) {
    case READ_FAKE_HTTP: {
        int fakeHttpHeadStartIndex = in.readerIndex();

        int fakeHttpHeadEndIndex = in.forEachByte(new ByteBufProcessor() {
            int c = 0;

            @Override//from   w w w  .j a  v  a 2 s . c o m
            public boolean process(byte value) throws Exception {

                if (value == '\r' || value == '\n') {
                    c++;
                } else {
                    c = 0;
                }

                //logger.info("value=" + value + ", c=" + c);

                if (c >= 4) {
                    return false;
                } else {
                    return true;
                }
            }
        });

        logger.debug("s: " + fakeHttpHeadStartIndex);
        logger.debug("e: " + fakeHttpHeadEndIndex);

        if (fakeHttpHeadEndIndex == -1) {
            logger.warn("w: " + fakeHttpHeadStartIndex);
            break;
        }

        byte[] buf = new byte[fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1];
        in.readBytes(buf, 0, fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1);
        String s = TextUtil.fromUTF8Bytes(buf);

        //logger.info(s);

        String[] ss = StringUtils.split(s, "\r\n");

        //System.out.println(s + "" + this + " " + Thread.currentThread().getName());

        for (String line : ss) {
            if (StringUtils.startsWith(line, "X-C:")) {
                String lenStr = StringUtils.trim(StringUtils.split(line, ":")[1]);
                //System.out.println(lenStr + "" + this + " " + Thread.currentThread().getName());
                //System.out.println("*****************************************");
                try {
                    length = Integer.parseInt(lenStr, 16);
                } catch (Throwable t) {
                    logger.error("--------------------------------------");
                    logger.error(s + "" + this + " " + Thread.currentThread().getName());
                    logger.error("--------------------------------------");
                }

            }

            if (StringUtils.startsWith(line, "X-U:")) {
                String user = StringUtils.trim(StringUtils.split(line, ":")[1]);
                ctx.channel().attr(NettyAttributeKey.LINK_USER).set(user);
                logger.info(user);
            }
        }

        this.checkpoint(STATE.READ_CONTENT);
    }
    case READ_CONTENT: {

        trafficLogger.info("U," + ctx.channel().attr(NettyAttributeKey.LINK_USER).get() + "," + length);

        if (length > 0) {

            byte[] buf = new byte[length];
            in.readBytes(buf, 0, length);

            byte[] res = new byte[length];

            for (int i = 0; i < length; i++) {
                res[i] = (byte) (buf[i] ^ (RemoteConfig.ins().getEncryptKey() & 0xFF));
            }

            ByteBuf outBuf = ctx.alloc().buffer();

            outBuf.writeBytes(res);

            out.add(outBuf);
        }

        this.checkpoint(STATE.READ_FAKE_HTTP);
        break;
    }
    default:
        throw new Error("Shouldn't reach here.");
    }

}

From source file:io.codis.nedis.handler.RedisResponseDecoder.java

License:Apache License

private String decodeString(ByteBuf in) throws ProtocolException {
    final StringBuilder buffer = new StringBuilder();
    final MutableBoolean reachCRLF = new MutableBoolean(false);
    setReaderIndex(in, in.forEachByte(new ByteBufProcessor() {

        @Override/* w w w .  j av a  2s.com*/
        public boolean process(byte value) throws Exception {
            if (value == '\n') {
                if ((byte) buffer.charAt(buffer.length() - 1) != '\r') {
                    throw new ProtocolException("Response is not ended by CRLF");
                } else {
                    buffer.setLength(buffer.length() - 1);
                    reachCRLF.setTrue();
                    return false;
                }
            } else {
                buffer.append((char) value);
                return true;
            }
        }
    }));
    return reachCRLF.booleanValue() ? buffer.toString() : null;
}

From source file:io.codis.nedis.handler.RedisResponseDecoder.java

License:Apache License

private Long decodeLong(ByteBuf in) throws ProtocolException {
    byte sign = in.readByte();
    final MutableLong l;
    boolean negative;
    if (sign == '-') {
        negative = true;//from  w  ww.j a v  a  2s  .  c  o  m
        l = new MutableLong(0);
    } else {
        negative = false;
        l = new MutableLong(toDigit(sign));
    }
    final MutableBoolean reachCR = new MutableBoolean(false);
    setReaderIndex(in, in.forEachByte(new ByteBufProcessor() {

        @Override
        public boolean process(byte value) throws Exception {
            if (value == '\r') {
                reachCR.setTrue();
                return false;
            } else {
                if (value >= '0' && value <= '9') {
                    l.setValue(l.longValue() * 10 + toDigit(value));
                } else {
                    throw new ProtocolException("Response is not ended by CRLF");
                }
                return true;
            }
        }
    }));
    if (!reachCR.booleanValue()) {
        return null;
    }
    if (!in.isReadable()) {
        return null;
    }
    if (in.readByte() != '\n') {
        throw new ProtocolException("Response is not ended by CRLF");
    }
    return negative ? -l.longValue() : l.longValue();
}

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

License:Apache License

private int findLineEnd(ByteBuf buffer) {

    int index = buffer.forEachByte(ByteProcessor.FIND_LF);
    return (index > 0 && buffer.getByte(index - 1) == '\r') ? index : -1;
}

From source file:io.reactiverse.pgclient.UtilTest.java

License:Apache License

private void assertSeparator(String s, int expected) throws Exception {
    ByteBuf buf = Unpooled.buffer();
    buf.writeCharSequence(s, StandardCharsets.UTF_8);
    UTF8StringEndDetector processor = new UTF8StringEndDetector();
    int actual = buf.forEachByte(processor);
    assertEquals(expected, actual);/* w  w  w .  ja v a  2  s . c om*/
}

From source file:io.reactivex.netty.protocol.http.sse.ServerSentEventDecoder.java

License:Apache License

protected static int scanAndFindColon(ByteBuf byteBuf) {
    return byteBuf.forEachByte(SCAN_COLON_PROCESSOR);
}

From source file:io.reactivex.netty.protocol.http.sse.ServerSentEventDecoder.java

License:Apache License

protected static int scanAndFindEndOfLine(ByteBuf byteBuf) {
    return byteBuf.forEachByte(SCAN_EOL_PROCESSOR);
}