Package net.officefloor.server.buffer
Class StreamBufferScanner
- java.lang.Object
-
- net.officefloor.server.buffer.StreamBufferScanner
-
- Direct Known Subclasses:
HttpRequestParser
public class StreamBufferScanner extends java.lang.Object
Provides scanning ofStreamBuffer
instances.- Author:
- Daniel Sagenschneider
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StreamBufferScanner.ScanTarget
Scan target.
-
Constructor Summary
Constructors Constructor Description StreamBufferScanner()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
appendStreamBuffer(StreamBuffer<java.nio.ByteBuffer> buffer)
AppendsStreamBuffer
for scanning.<T extends java.lang.Throwable>
bytebuildByte(java.util.function.Supplier<T> illegalValueExceptionFactory)
Builds a byte from theStreamBuffer
at current position.<T extends java.lang.Throwable>
longbuildLong(java.util.function.Supplier<T> illegalValueExceptionFactory)
Builds a long (8 bytes) from theStreamBuffer
at current position.<T extends java.lang.Throwable>
shortbuildShort(java.util.function.Supplier<T> illegalValueExceptionFactory)
Builds a short (2 bytes) from theStreamBuffer
at current position.static byte
indexOf(long bytes, StreamBufferScanner.ScanTarget target)
Scans theByteBuffer
to find the next byte of particular value.int
peekToTarget(StreamBufferScanner.ScanTarget target)
Peeks in the currentStreamBuffer
to attempt to find theStreamBufferScanner.ScanTarget
.StreamBufferByteSequence
scanBytes(long numberOfBytes)
Scans a specific number of bytes.<T extends java.lang.Throwable>
StreamBufferByteSequencescanToTarget(StreamBufferScanner.ScanTarget target, int maxBytesLength, java.util.function.Supplier<T> tooLongExceptionFactory)
Scans theStreamBuffer
for the byte value from the current position.void
skipBytes(int numberOfBytes)
Skips forward the particular number of bytes in the currentStreamBuffer
.
-
-
-
Method Detail
-
appendStreamBuffer
public void appendStreamBuffer(StreamBuffer<java.nio.ByteBuffer> buffer)
AppendsStreamBuffer
for scanning.- Parameters:
buffer
-StreamBuffer
.
-
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 theStreamBuffer
at current position.- Type Parameters:
T
- Illegal valueException
- Parameters:
illegalValueExceptionFactory
-Supplier
to createThrowable
for illegal long value.- Returns:
- Long value, otherwise
-1
if not enough bytes inStreamBuffer
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 theStreamBuffer
at current position.- Type Parameters:
T
- Illegal valueException
- Parameters:
illegalValueExceptionFactory
-Supplier
to createThrowable
for illegal short value.- Returns:
- Short value, otherwise
-1
if not enough bytes inStreamBuffer
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 theStreamBuffer
at current position.- Type Parameters:
T
- Illegal valueException
- Parameters:
illegalValueExceptionFactory
-Supplier
to createThrowable
for illegal short value.- Returns:
- Byte value, otherwise
-1
if not enough bytes inStreamBuffer
to build a byte. - Throws:
T
- If invalid value.T extends java.lang.Throwable
-
peekToTarget
public int peekToTarget(StreamBufferScanner.ScanTarget target)
Peeks in the currentStreamBuffer
to attempt to find theStreamBufferScanner.ScanTarget
.- Parameters:
target
-StreamBufferScanner.ScanTarget
.- Returns:
- Position of the
StreamBufferScanner.ScanTarget
within the currentStreamBuffer
(starting at current position). Otherwise,-1
to indicate did not findStreamBufferScanner.ScanTarget
in currentStreamBuffer
.
-
scanBytes
public StreamBufferByteSequence scanBytes(long numberOfBytes)
Scans a specific number of bytes.- Parameters:
numberOfBytes
- Number of bytes to scan.- Returns:
StreamBufferByteSequence
to the scanned bytes.
-
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 longException
type.- Parameters:
target
-StreamBufferScanner.ScanTarget
.maxBytesLength
- Max bytes.tooLongExceptionFactory
-Supplier
to provide too longException
.- Returns:
StreamBufferByteSequence
if found the byte. Otherwise,null
indicating furtherStreamBuffer
instances may contain the byte.- Throws:
T
- If too longException
.T extends java.lang.Throwable
-
skipBytes
public void skipBytes(int numberOfBytes)
Skips forward the particular number of bytes in the currentStreamBuffer
.- 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.
-
-