Package net.officefloor.server.buffer
Class StreamBufferScanner
java.lang.Object
net.officefloor.server.buffer.StreamBufferScanner
- Direct Known Subclasses:
HttpRequestParser
Provides scanning of
StreamBuffer instances.- Author:
- Daniel Sagenschneider
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidappendStreamBuffer(StreamBuffer<ByteBuffer> buffer) AppendsStreamBufferfor scanning.<T extends Throwable>
byteBuilds a byte from theStreamBufferat current position.<T extends Throwable>
longBuilds a long (8 bytes) from theStreamBufferat current position.<T extends Throwable>
shortbuildShort(Supplier<T> illegalValueExceptionFactory) Builds a short (2 bytes) from theStreamBufferat current position.static final byteindexOf(long bytes, StreamBufferScanner.ScanTarget target) Scans theByteBufferto find the next byte of particular value.intPeeks in the currentStreamBufferto attempt to find theStreamBufferScanner.ScanTarget.scanBytes(long numberOfBytes) Scans a specific number of bytes.<T extends Throwable>
StreamBufferByteSequencescanToTarget(StreamBufferScanner.ScanTarget target, int maxBytesLength, Supplier<T> tooLongExceptionFactory) Scans theStreamBufferfor the byte value from the current position.voidskipBytes(int numberOfBytes) Skips forward the particular number of bytes in the currentStreamBuffer.
-
Constructor Details
-
StreamBufferScanner
public StreamBufferScanner()
-
-
Method Details
-
appendStreamBuffer
AppendsStreamBufferfor scanning.- Parameters:
buffer-StreamBuffer.
-
buildLong
Builds a long (8 bytes) from theStreamBufferat current position.- Type Parameters:
T- Illegal valueException- Parameters:
illegalValueExceptionFactory-Supplierto createThrowablefor illegal long value.- Returns:
- Long value, otherwise
-1if not enough bytes inStreamBufferto build a long. - Throws:
T- If invalid value.
-
buildShort
Builds a short (2 bytes) from theStreamBufferat current position.- Type Parameters:
T- Illegal valueException- Parameters:
illegalValueExceptionFactory-Supplierto createThrowablefor illegal short value.- Returns:
- Short value, otherwise
-1if not enough bytes inStreamBufferto build a short. - Throws:
T- If invalid value.
-
buildByte
Builds a byte from theStreamBufferat current position.- Type Parameters:
T- Illegal valueException- Parameters:
illegalValueExceptionFactory-Supplierto createThrowablefor illegal short value.- Returns:
- Byte value, otherwise
-1if not enough bytes inStreamBufferto build a byte. - Throws:
T- If invalid value.
-
peekToTarget
Peeks in the currentStreamBufferto attempt to find theStreamBufferScanner.ScanTarget.- Parameters:
target-StreamBufferScanner.ScanTarget.- Returns:
- Position of the
StreamBufferScanner.ScanTargetwithin the currentStreamBuffer(starting at current position). Otherwise,-1to indicate did not findStreamBufferScanner.ScanTargetin currentStreamBuffer.
-
scanBytes
Scans a specific number of bytes.- Parameters:
numberOfBytes- Number of bytes to scan.- Returns:
StreamBufferByteSequenceto the scanned bytes.
-
scanToTarget
public <T extends Throwable> StreamBufferByteSequence scanToTarget(StreamBufferScanner.ScanTarget target, int maxBytesLength, Supplier<T> tooLongExceptionFactory) throws T Scans the
StreamBufferfor the byte value from the current position.The returned
StreamBufferByteSequenceis 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 longExceptiontype.- Parameters:
target-StreamBufferScanner.ScanTarget.maxBytesLength- Max bytes.tooLongExceptionFactory-Supplierto provide too longException.- Returns:
StreamBufferByteSequenceif found the byte. Otherwise,nullindicating furtherStreamBufferinstances may contain the byte.- Throws:
T- If too longException.
-
skipBytes
public void skipBytes(int numberOfBytes) Skips forward the particular number of bytes in the currentStreamBuffer.- Parameters:
numberOfBytes- Number of bytes to skip.
-
indexOf
Scans the
ByteBufferto 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.ScanTargetfor the byte to find the index.- Returns:
- Index within the long of the first byte value. Otherwise,
-1if does not find the byte.
-