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(int index, int length, ByteProcessor processor);

Source Link

Document

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

Usage

From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelper.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.
 *
 * This implementation starts for the current {@link ByteBuf#readerIndex() readerIndex} + startOffset.
 *
 * @param buf the {@link ByteBuf} where to search for the end of a section enclosed in openingChar and closingChar.
 * @param startOffset the offset at which to start reading (from buffer's readerIndex).
 * @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 the section closing position or -1 if not found.
 *///from   ww  w.j  av a  2  s.c om
public static int findSectionClosingPosition(ByteBuf buf, int startOffset, char openingChar, char closingChar) {
    int from = buf.readerIndex() + startOffset;
    int length = buf.writerIndex() - from;
    if (length < 0) {
        throw new IllegalArgumentException(
                "startOffset must not go beyond the readable byte length of the buffer");
    }

    return buf.forEachByte(from, length, new ClosingPositionBufProcessor(openingChar, closingChar, true));
}

From source file:io.grpc.netty.GrpcHpackHuffmanDecoder.java

License:Apache License

/**
 * Decompresses the given Huffman coded string literal.
 *
 * @param buf the string literal to be decoded
 * @return the output stream for the compressed data
 * @throws Http2Exception EOS Decoded/*w  w w. j  a v  a  2  s.  c  o  m*/
 */
public AsciiString decode(ByteBuf buf, int length) throws Http2Exception {
    processor.reset();
    buf.forEachByte(buf.readerIndex(), length, processor);
    buf.skipBytes(length);
    return processor.end();
}

From source file:io.netlibs.bgp.handlers.BGPv4Reframer.java

License:Apache License

/**
 * reframe the received packet to completely contain the next BGPv4 packet. It peeks into the first four bytes of the TCP stream which
 * contain a 16-bit marker and a 16-bit length field. The marker must be all one's and the length value must be between 19 and 4096
 * according to RFC 4271. The marker and length constraints are verified and if either is violated the connection is closed early.
 * //  w  w  w  .  j  av  a  2 s .co  m
 * Any packets that are added start on the type byte. The buffer will contain the full message payload.
 * 
 */

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buffer, final List<Object> out)
        throws Exception {

    if (buffer.readableBytes() < (BGPv4Constants.BGP_PACKET_MIN_LENGTH - 1)) {
        // need more bytes for a full read.
        return;
    }

    buffer.markReaderIndex();

    // confirm that the next BGP_PACKET_MARKER_LENGTH bytes are all 0xff.

    if (buffer.forEachByte(buffer.readerIndex(), BGPv4Constants.BGP_PACKET_MARKER_LENGTH,
            value -> value == (byte) 0xff) != -1) {
        log.error("received invalid marker, closing connection");
        NotificationHelper.sendEncodedNotification(ctx, new ConnectionNotSynchronizedNotificationPacket(),
                new BgpEventFireChannelFutureListener(ctx));
        return;
    }

    // skip the marker.
    buffer.skipBytes(BGPv4Constants.BGP_PACKET_MARKER_LENGTH);

    // read the packet length.
    final int length = buffer.readUnsignedShort();

    if ((length < BGPv4Constants.BGP_PACKET_MIN_LENGTH) || (length > BGPv4Constants.BGP_PACKET_MAX_LENGTH)) {

        log.error("received illegal packet size {}, must be between {} and {}. closing connection",
                new Object[] { length, BGPv4Constants.BGP_PACKET_MIN_LENGTH,
                        BGPv4Constants.BGP_PACKET_MAX_LENGTH });

        NotificationHelper.sendEncodedNotification(ctx, new BadMessageLengthNotificationPacket(length),
                new BgpEventFireChannelFutureListener(ctx));

        return;

    }

    final int mustRead = (length - (BGPv4Constants.BGP_PACKET_MARKER_LENGTH + 2)); // we have consumed marker and length at this point

    // must if we don't have the right amount, abort.
    if (buffer.readableBytes() < mustRead) {
        buffer.resetReaderIndex();
        return;
    }

    out.add(buffer.readBytes(mustRead));

}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static <T> T[] textDecodeArray(IntFunction<T[]> supplier, DataType type, int index, int len,
        ByteBuf buff) {
    List<T> list = new ArrayList<>();
    int from = index + 1; // Set index after '{'
    int to = index + len - 1; // Set index before '}'
    while (from < to) {
        // Escaped content ?
        boolean escaped = buff.getByte(from) == '"';
        int idx;/*from  w w w .j a v a2 s. co  m*/
        if (escaped) {
            idx = buff.forEachByte(from, to - from, new UTF8StringEndDetector());
            idx = buff.indexOf(idx, to, (byte) ','); // SEE iF WE CAN GET RID oF IT
        } else {
            idx = buff.indexOf(from, to, (byte) ',');
        }
        if (idx == -1) {
            idx = to;
        }
        T elt = textDecodeArrayElement(type, from, idx - from, buff);
        list.add(elt);
        from = idx + 1;
    }
    return list.toArray(supplier.apply(list.size()));
}

From source file:org.apache.tajo.plan.function.stream.CSVLineDeserializer.java

License:Apache License

public void deserialize(final ByteBuf lineBuf, Tuple output) throws IOException, TextLineParsingError {
    int[] projection = targetColumnIndexes;
    if (lineBuf == null || targetColumnIndexes == null || targetColumnIndexes.length == 0) {
        return;//from  ww  w .  ja va2s.c o  m
    }

    final int rowLength = lineBuf.readableBytes();
    int start = 0, fieldLength = 0, end = 0;

    //Projection
    int currentTarget = 0;
    int currentIndex = 0;

    while (end != -1) {
        end = lineBuf.forEachByte(start, rowLength - start, processor);

        if (end < 0) {
            fieldLength = rowLength - start;
        } else {
            fieldLength = end - start - delimiterCompensation;
        }

        if (projection.length > currentTarget && currentIndex == projection[currentTarget]) {
            lineBuf.setIndex(start, start + fieldLength);
            Datum datum = fieldSerDer.deserialize(lineBuf, schema.getColumn(currentIndex), currentIndex,
                    nullChars);
            output.put(currentIndex, datum);
            currentTarget++;
        }

        if (projection.length == currentTarget) {
            break;
        }

        start = end + 1;
        currentIndex++;
    }
}

From source file:org.apache.tajo.storage.TestSplitProcessor.java

License:Apache License

@Test
public void testFieldSplitProcessor() throws IOException {
    String data = "abc||de|";
    final ByteBuf buf = releaseLater(Unpooled.copiedBuffer(data, CharsetUtil.ISO_8859_1));

    final int len = buf.readableBytes();
    FieldSplitProcessor processor = new FieldSplitProcessor((byte) '|');

    assertEquals(3, buf.forEachByte(0, len, processor));
    assertEquals(4, buf.forEachByte(4, len - 4, processor));
    assertEquals(7, buf.forEachByte(5, len - 5, processor));
    assertEquals(-1, buf.forEachByte(8, len - 8, processor));
}

From source file:org.apache.tajo.storage.TestSplitProcessor.java

License:Apache License

@Test
public void testMultiCharFieldSplitProcessor1() throws IOException {
    String data = "abc||||de||";
    final ByteBuf buf = releaseLater(Unpooled.copiedBuffer(data, CharsetUtil.ISO_8859_1));

    final int len = buf.readableBytes();
    ByteBufProcessor processor = new MultiBytesFieldSplitProcessor("||".getBytes());

    assertEquals(4, buf.forEachByte(0, len, processor));
    assertEquals(6, buf.forEachByte(5, len - 5, processor));
    assertEquals(10, buf.forEachByte(7, len - 7, processor));
    assertEquals(-1, buf.forEachByte(11, len - 11, processor));
}

From source file:org.apache.tajo.storage.TestSplitProcessor.java

License:Apache License

@Test
public void testMultiCharFieldSplitProcessor2() throws IOException {
    String data = "abcde";
    final ByteBuf buf = releaseLater(Unpooled.copiedBuffer(data, CharsetUtil.UTF_8));

    final int len = buf.readableBytes();
    ByteBufProcessor processor = new MultiBytesFieldSplitProcessor("".getBytes());

    assertEquals(5, buf.forEachByte(0, len, processor));
    assertEquals(8, buf.forEachByte(6, len - 6, processor));
    assertEquals(13, buf.forEachByte(9, len - 9, processor));
    assertEquals(-1, buf.forEachByte(14, len - 14, processor));
}

From source file:org.apache.tajo.storage.TestSplitProcessor.java

License:Apache License

@Test
public void testLineSplitProcessor() throws IOException {
    String data = "abc\r\n\n";
    final ByteBuf buf = releaseLater(Unpooled.copiedBuffer(data, CharsetUtil.ISO_8859_1));

    final int len = buf.readableBytes();
    LineSplitProcessor processor = new LineSplitProcessor();

    //find CR//from w  ww .j  a  v  a  2  s.c o m
    assertEquals(3, buf.forEachByte(0, len, processor));

    // find CRLF
    assertEquals(4, buf.forEachByte(4, len - 4, processor));
    assertEquals(buf.getByte(4), '\n');
    // need to skip LF
    assertTrue(processor.isPrevCharCR());

    // find LF
    assertEquals(5, buf.forEachByte(5, len - 5, processor)); //line length is zero
}

From source file:org.graylog2.inputs.transports.netty.LenientLineBasedFrameDecoder.java

License:Open Source 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.
 *///from w  ww.  j  ava 2s.  c  o  m
private int findEndOfLine(final ByteBuf buffer) {
    int totalLength = buffer.readableBytes();
    int i = buffer.forEachByte(buffer.readerIndex() + offset, totalLength - offset, ByteProcessor.FIND_LF);
    if (i >= 0) {
        offset = 0;
        if (i > 0 && buffer.getByte(i - 1) == '\r') {
            i--;
        }
    } else {
        offset = totalLength;
    }
    return i;
}