Java Code Examples for io.netty.buffer.ByteBuf#forEachByte()

The following examples show how to use io.netty.buffer.ByteBuf#forEachByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
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;
    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()));
}
 
Example 2
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public AppendableCharSequence parse(ByteBuf buffer) {
    final int oldSize = size;
    seq.reset();
    int i = buffer.forEachByte(this);
    if (i == -1) {
        size = oldSize;
        return null;
    }
    buffer.readerIndex(i + 1);
    return seq;
}
 
Example 3
Source File: DotStuffing.java    From NioSmtpClient with Apache License 2.0 5 votes vote down vote up
private static int findDotAtBeginningOfLine(ByteBuf buffer, int startAt, byte[] previousBytes) {
  int length = buffer.readableBytes();

  if (previousBytes[0] == CR && previousBytes[1] == LF && buffer.getByte(startAt) == DOT) {
    return startAt;
  }

  if (previousBytes[1] == CR && length >= 2 && buffer.getByte(startAt) == LF && buffer.getByte(startAt + 1) == DOT) {
    return startAt + 1;
  }

  int i = startAt;
  while (++i < length) {
    i = buffer.forEachByte(i, length - i, ByteProcessor.FIND_LF);
    if (i == -1) {
      return -1;
    }

    if (buffer.getByte(i - 1) == CR) {
      if (i + 1 < length && buffer.getByte(i + 1) == DOT) {
        return i + 1;
      } else {
        continue;
      }
    }
  }

  return -1;
}
 
Example 4
Source File: EchoClientTest.java    From netty-learning with MIT License 5 votes vote down vote up
/**
 * 查找换行符
 */
@Test
public void findByteTest() {
    ByteBuf byteBuf = Unpooled.copiedBuffer("abc\r", CharsetUtil.UTF_8);
    int index = byteBuf.forEachByte(ByteProcessor.FIND_CR);
    System.out.println(index);
}
 
Example 5
Source File: CSVLineDeserializer.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(final ByteBuf lineBuf, Tuple output) throws IOException, TextLineParsingError {
  int[] projection = targetColumnIndexes;
  if (lineBuf == null || targetColumnIndexes == null || targetColumnIndexes.length == 0) {
    return;
  }

  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).getDataType(), nullChars);
      output.put(currentIndex, datum);
      currentTarget++;
    }

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

    start = end + 1;
    currentIndex++;
  }
}
 
Example 6
Source File: StompFrameDecoder.java    From hazelcastmq with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the optional EOL (and other control characters) that are permitted
 * between the end of one frame and the start of the next frame. When a
 * non-control character is detected, the decoder state will be advanced.
 *
 * @param in the input buffer to read from
 *
 * @return the next decoder state or null if no checkpoint should be set
 */
private DecoderState readControlChars(ByteBuf in) {

  DecoderState nextState = DecoderState.READ_CONTROL_CHARS;

  int index = in.forEachByte(new ByteBufProcessor() {
    @Override
    public boolean process(byte b) throws Exception {
      switch (b) {
        // This is a little more lax than the spec which allows for only
        // EOL character(s) between frames.
        case ' ':
        case CARRIAGE_RETURN_CHAR:
        case LINE_FEED_CHAR:
        case NULL_CHAR:
          // ignore the character
          return true;

        default:
          return false;
      }
    }
  });

  if (index != -1) {
    // A non-control character was found so we skip up to that index and
    // move to the next state.
    in.readerIndex(index);
    nextState = DecoderState.READ_COMMAND;
  }
  else {
    // Discard all available bytes because we couldn't find a
    // non-control character.
    in.readerIndex(in.writerIndex());
  }

  return nextState;
}
 
Example 7
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public AppendableCharSequence parse(ByteBuf buffer) {
    final int oldSize = size;
    seq.reset();
    int i = buffer.forEachByte(this);
    if (i == -1) {
        size = oldSize;
        return null;
    }
    buffer.readerIndex(i + 1);
    return seq;
}
 
Example 8
Source File: StringClosingPositionBufProcessorTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testClosingPosFoundInSimpleString() {
    ByteBuf source = Unpooled.copiedBuffer("\" \"", CharsetUtil.UTF_8);

    int closingPos = source.forEachByte(new StringClosingPositionBufProcessor());

    assertEquals(2, closingPos);
    assertEquals(0, source.readerIndex());
}
 
Example 9
Source File: ByteBufChecksum.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * @see #update(byte[], int, int)
 */
public void update(ByteBuf b, int off, int len) {
    if (b.hasArray()) {
        update(b.array(), b.arrayOffset() + off, len);
    } else {
        b.forEachByte(off, len, updateProcessor);
    }
}
 
Example 10
Source File: RedisDecoder.java    From resp-server with MIT License 5 votes vote down vote up
private static int findEndOfLine(final ByteBuf buffer) {
  int i = buffer.forEachByte(ByteProcessor.FIND_CRLF);
  if (i > 0 && buffer.getByte(i - 1) == '\r') {
    i--;
  }
  return i;
}
 
Example 11
Source File: StringClosingPositionBufProcessorTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testClosingPosNotFoundInPartialStringLeftPart() {
    ByteBuf source = Unpooled.copiedBuffer(" \"\\\"Partial\\\" str",
            CharsetUtil.UTF_8);

    int closingPos = source.forEachByte(new StringClosingPositionBufProcessor());

    assertEquals(-1, closingPos);
    assertEquals(0, source.readerIndex());
}
 
Example 12
Source File: ClosingPositionBufProcessorTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldIgnoreJsonStringWithClosingSectionCharEvenIfStreamInterrupted() {
    ByteBuf source = Unpooled.copiedBuffer(
            "{ this is \"a string \\\"with }", CharsetUtil.UTF_8);

    int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}', true));

    assertEquals(-1, closingPos);
    assertEquals(0, source.readerIndex());
}
 
Example 13
Source File: RedisDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static ByteBuf readLine(ByteBuf in) {
    if (!in.isReadable(RedisConstants.EOL_LENGTH)) {
        return null;
    }
    final int lfIndex = in.forEachByte(ByteProcessor.FIND_LF);
    if (lfIndex < 0) {
        return null;
    }
    ByteBuf data = in.readSlice(lfIndex - in.readerIndex() - 1); // `-1` is for CR
    readEndOfLine(in); // validate CR LF
    return data;
}
 
Example 14
Source File: HttpRequestDecoder.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Override
protected void handlePartialInitialLine(final ChannelHandlerContext ctx, final ByteBuf buffer) {
    buffer.forEachByte(FIND_WS_AFTER_METHOD_NAME);
}
 
Example 15
Source File: PgDecoder.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
int parse(ByteBuf in) {
  afterSpace = false;
  rows = 0;
  in.forEachByte(in.readerIndex(), in.readableBytes() - 1, this);
  return rows;
}
 
Example 16
Source File: HttpObjectDecoder.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
private void parseHeaderLine(final HttpHeaders headers, final ByteBuf buffer, final int lfIndex) {
    // https://tools.ietf.org/html/rfc7230#section-3.2
    // header-field   = field-name ":" OWS field-value OWS
    //
    //      field-name     = token
    //      field-value    = *( field-content / obs-fold )
    //      field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
    //      field-vchar    = VCHAR / obs-text
    //
    //      obs-fold       = CRLF 1*( SP / HTAB )
    //                     ; obsolete line folding
    //                     ; see Section 3.2.4
    //      OWS            = *( SP / HTAB )
    //                     ; optional whitespace
    // https://tools.ietf.org/html/rfc7230#section-3.2.4
    // No whitespace is allowed between the header field-name and colon.  In
    //    the past, differences in the handling of such whitespace have led to
    //    security vulnerabilities in request routing and response handling.
    final int nonControlIndex = lfIndex - 2;
    final int nameStart = buffer.readerIndex();
    // Other checks will be done by header validator if enabled by users
    final int nameEnd = buffer.forEachByte(nameStart, nonControlIndex - nameStart + 1, FIND_COLON);
    if (nameEnd < 0) {
        throw new IllegalArgumentException("Unable to find end of header name");
    }
    if (nameEnd == nameStart) {
        throw new IllegalArgumentException("Empty header name");
    }

    // We assume the allocator will not leak memory, and so we retain + slice to avoid copying data.
    final CharSequence name = newAsciiString(newBufferFrom(buffer.retainedSlice(nameStart, nameEnd - nameStart)));
    final int valueStart;
    if (nameEnd >= nonControlIndex || (valueStart =
            buffer.forEachByte(nameEnd + 1, nonControlIndex - nameEnd, FIND_FIELD_VALUE)) < 0) {
        headers.add(name, emptyAsciiString());
    } else {
        final int valueEnd = buffer.forEachByteDesc(valueStart, nonControlIndex - valueStart + 1, FIND_FIELD_VALUE);
        // We assume the allocator will not leak memory, and so we retain + slice to avoid copying data.
        headers.add(name, newAsciiString(newBufferFrom(buffer.retainedSlice(valueStart,
                valueEnd - valueStart + 1))));
    }
    // Consume the header line bytes from the buffer.
    consumeCRLF(buffer, lfIndex);
}
 
Example 17
Source File: HpackHuffmanDecoder.java    From netty-4.1.22 with Apache License 2.0 3 votes vote down vote up
/**
 * 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
 */
public AsciiString decode(ByteBuf buf, int length) throws Http2Exception {
    processor.reset();
    buf.forEachByte(buf.readerIndex(), length, processor);
    buf.skipBytes(length);
    return processor.end();
}
 
Example 18
Source File: Bzip2BlockCompressor.java    From netty-4.1.22 with Apache License 2.0 2 votes vote down vote up
/**
 * Writes an array to the block.向块写入数组。
 * @param buffer The buffer to write
 * @param offset The offset within the input data to write from
 * @param length The number of bytes of input data to write
 * @return The actual number of input bytes written. May be less than the number requested, or
 *         zero if the block is already full
 */
int write(final ByteBuf buffer, int offset, int length) {
    int index = buffer.forEachByte(offset, length, writeProcessor);
    return index == -1 ? length : index - offset;
}
 
Example 19
Source File: ByteBufJsonHelper.java    From couchbase-jvm-core with Apache License 2.0 2 votes vote down vote up
/**
 * Finds the position of the split character, taking into account the fact that the character
 * could be found escaped in strings (such occurrences are ignored).
 *
 * This implementation starts for the current {@link ByteBuf#readerIndex() readerIndex}.
 *
 * @param buf the {@link ByteBuf} where to search for the split character.
 * @param splitChar the split character to detect.
 * @return the split character position or -1 if not found.
 */
public static final int findSplitPosition(ByteBuf buf, char splitChar) {
    return buf.forEachByte(new SplitPositionBufProcessor(splitChar, true));
}
 
Example 20
Source File: ViewHandler.java    From couchbase-jvm-core with Apache License 2.0 2 votes vote down vote up
/**
 * 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
 */
private static int findSectionClosingPosition(ByteBuf buf, char openingChar, char closingChar) {
    return buf.forEachByte(new ClosingPositionBufProcessor(openingChar, closingChar, true));
}