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.ObjectProvides scanning ofStreamBufferinstances.- Author:
- Daniel Sagenschneider
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStreamBufferScanner.ScanTargetScan target.
-
Constructor Summary
Constructors Constructor Description StreamBufferScanner()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappendStreamBuffer(StreamBuffer<java.nio.ByteBuffer> buffer)AppendsStreamBufferfor scanning.<T extends java.lang.Throwable>
bytebuildByte(java.util.function.Supplier<T> illegalValueExceptionFactory)Builds a byte from theStreamBufferat current position.<T extends java.lang.Throwable>
longbuildLong(java.util.function.Supplier<T> illegalValueExceptionFactory)Builds a long (8 bytes) from theStreamBufferat current position.<T extends java.lang.Throwable>
shortbuildShort(java.util.function.Supplier<T> illegalValueExceptionFactory)Builds a short (2 bytes) from theStreamBufferat current position.static byteindexOf(long bytes, StreamBufferScanner.ScanTarget target)Scans theByteBufferto find the next byte of particular value.intpeekToTarget(StreamBufferScanner.ScanTarget target)Peeks in the currentStreamBufferto attempt to find theStreamBufferScanner.ScanTarget.StreamBufferByteSequencescanBytes(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 theStreamBufferfor the byte value from the current position.voidskipBytes(int numberOfBytes)Skips forward the particular number of bytes in the currentStreamBuffer.
-
-
-
Method Detail
-
appendStreamBuffer
public void appendStreamBuffer(StreamBuffer<java.nio.ByteBuffer> buffer)
AppendsStreamBufferfor scanning.- Parameters:
buffer-StreamBuffer.
-
buildLong
public <T extends java.lang.Throwable> long buildLong(java.util.function.Supplier<T> illegalValueExceptionFactory) throws T extends java.lang.ThrowableBuilds 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.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.ThrowableBuilds 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.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.ThrowableBuilds 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.T extends java.lang.Throwable
-
peekToTarget
public int peekToTarget(StreamBufferScanner.ScanTarget target)
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
public StreamBufferByteSequence scanBytes(long numberOfBytes)
Scans a specific number of bytes.- Parameters:
numberOfBytes- Number of bytes to scan.- Returns:
StreamBufferByteSequenceto 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
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.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
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.
-
-