Example usage for io.netty.buffer ByteBuf readerIndex

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

Introduction

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

Prototype

public abstract int readerIndex();

Source Link

Document

Returns the readerIndex of this buffer.

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   w  w w. j a  v  a 2  s  .  com*/
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:com.couchbase.client.core.endpoint.util.ByteBufJsonHelperTest.java

License:Apache License

@Test
public void testFindNextChar() throws Exception {
    ByteBuf source = Unpooled.copiedBuffer("ABCDEF", CharsetUtil.UTF_8);

    assertEquals(3, ByteBufJsonHelper.findNextChar(source, 'D'));
    assertEquals(-1, ByteBufJsonHelper.findNextChar(source, 'G'));
    assertEquals(0, source.readerIndex());

}

From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelperTest.java

License:Apache License

@Test
public void testFindNextCharNotPrefixedBy() throws Exception {
    ByteBuf source = Unpooled.copiedBuffer("the \\\"end\\\" of a string\"", CharsetUtil.UTF_8);

    assertEquals(5, ByteBufJsonHelper.findNextCharNotPrefixedBy(source, '\"', 'a'));
    assertEquals(23, ByteBufJsonHelper.findNextCharNotPrefixedBy(source, '\"', '\\'));
    assertEquals(-1, ByteBufJsonHelper.findNextCharNotPrefixedBy(source, 'a', ' '));
    assertEquals(-1, ByteBufJsonHelper.findNextCharNotPrefixedBy(source, 'z', ' '));
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelperTest.java

License:Apache License

@Test
public void shouldFindSectionClosingPositionIfAtStartSection() throws Exception {
    ByteBuf source = Unpooled.copiedBuffer("{123}", CharsetUtil.UTF_8);

    assertEquals(4, ByteBufJsonHelper.findSectionClosingPosition(source, '{', '}'));
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelperTest.java

License:Apache License

@Test
public void shouldNotFindSectionClosingPositionIfAfterStartSection() throws Exception {
    ByteBuf source = Unpooled.copiedBuffer("123}", CharsetUtil.UTF_8);

    assertEquals(-1, ByteBufJsonHelper.findSectionClosingPosition(source, '{', '}'));
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelperTest.java

License:Apache License

@Test
public void shouldFindSectionClosingPositionWithOffset() throws Exception {
    ByteBuf source = Unpooled.copiedBuffer("{{{}{123}", CharsetUtil.UTF_8);

    assertEquals(8, ByteBufJsonHelper.findSectionClosingPosition(source, 4, '{', '}'));
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldFindClosingInSimpleSection() {
    ByteBuf source = Unpooled.copiedBuffer("{ this is simple }", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}'));
    assertEquals(17, closingPos);/* w w  w  .ja  v  a2  s.  c o m*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldNotFindClosingInBrokenSection() {
    ByteBuf source = Unpooled.copiedBuffer("{ this is simple", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}'));
    assertEquals(-1, closingPos);/*from www.ja v a  2 s.c  o m*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldFindClosingInSectionWithSubsection() {
    ByteBuf source = Unpooled.copiedBuffer("{ this is { simple } }", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}'));
    assertEquals(21, closingPos);/* w  w  w  . j  a  va2  s . c  o m*/
    assertEquals(0, source.readerIndex());
}

From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java

License:Apache License

@Test
public void shouldNotFindClosingInBrokenSectionWithCompleteSubSection() {
    ByteBuf source = Unpooled.copiedBuffer("{ this is { complex } oups", CharsetUtil.UTF_8);
    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}'));
    assertEquals(-1, closingPos);//from w ww.j a v a  2 s . c  o m
    assertEquals(0, source.readerIndex());
}