Class StreamBufferScanner

  • Direct Known Subclasses:
    HttpRequestParser

    public class StreamBufferScanner
    extends java.lang.Object
    Provides scanning of StreamBuffer instances.
    Author:
    Daniel Sagenschneider
    • Constructor Detail

      • StreamBufferScanner

        public StreamBufferScanner()
    • Method Detail

      • buildLong

        public <T extends java.lang.Throwable> long buildLong​(java.util.function.Supplier<T> illegalValueExceptionFactory)
                                                       throws T extends java.lang.Throwable
        Builds a long (8 bytes) from the StreamBuffer at current position.
        Type Parameters:
        T - Illegal value Exception
        Parameters:
        illegalValueExceptionFactory - Supplier to create Throwable for illegal long value.
        Returns:
        Long value, otherwise -1 if not enough bytes in StreamBuffer to build a long.
        Throws:
        T - If invalid value.
        T extends java.lang.Throwable
      • buildShort

        public <T extends java.lang.Throwable> short buildShort​(java.util.function.Supplier<T> illegalValueExceptionFactory)
                                                         throws T extends java.lang.Throwable
        Builds a short (2 bytes) from the StreamBuffer at current position.
        Type Parameters:
        T - Illegal value Exception
        Parameters:
        illegalValueExceptionFactory - Supplier to create Throwable for illegal short value.
        Returns:
        Short value, otherwise -1 if not enough bytes in StreamBuffer to build a short.
        Throws:
        T - If invalid value.
        T extends java.lang.Throwable
      • buildByte

        public <T extends java.lang.Throwable> byte buildByte​(java.util.function.Supplier<T> illegalValueExceptionFactory)
                                                       throws T extends java.lang.Throwable
        Builds a byte from the StreamBuffer at current position.
        Type Parameters:
        T - Illegal value Exception
        Parameters:
        illegalValueExceptionFactory - Supplier to create Throwable for illegal short value.
        Returns:
        Byte value, otherwise -1 if not enough bytes in StreamBuffer to build a byte.
        Throws:
        T - If invalid value.
        T extends java.lang.Throwable
      • scanToTarget

        public <T extends java.lang.Throwable> StreamBufferByteSequence scanToTarget​(StreamBufferScanner.ScanTarget target,
                                                                                     int maxBytesLength,
                                                                                     java.util.function.Supplier<T> tooLongExceptionFactory)
                                                                              throws T extends java.lang.Throwable

        Scans the StreamBuffer for the byte value from the current position.

        The returned StreamBufferByteSequence is inclusive of the current position but exclusive of the target byte. This is because target bytes for HTTP are typically delimiters (eg. space, CR, etc) that are not included in the bytes of interest.

        Type Parameters:
        T - Too long Exception type.
        Parameters:
        target - StreamBufferScanner.ScanTarget.
        maxBytesLength - Max bytes.
        tooLongExceptionFactory - Supplier to provide too long Exception.
        Returns:
        StreamBufferByteSequence if found the byte. Otherwise, null indicating further StreamBuffer instances may contain the byte.
        Throws:
        T - If too long Exception.
        T extends java.lang.Throwable
      • skipBytes

        public void skipBytes​(int numberOfBytes)
        Skips forward the particular number of bytes in the current StreamBuffer.
        Parameters:
        numberOfBytes - Number of bytes to skip.
      • indexOf

        public static final byte indexOf​(long bytes,
                                         StreamBufferScanner.ScanTarget target)

        Scans the ByteBuffer to find the next byte of particular value.

        Note: the algorithm used does not handle negative byte values. However, the values (ASCII characters) searched for in HTTP parsing are all positive (lower 7 bits of the byte).

        Parameters:
        bytes - Long of 8 bytes to check for the value.
        target - StreamBufferScanner.ScanTarget for the byte to find the index.
        Returns:
        Index within the long of the first byte value. Otherwise, -1 if does not find the byte.