public abstract class BaseBuffer extends Object implements Buffer
| Modifier and Type | Field and Description |
|---|---|
protected static byte |
CR |
protected static byte |
LF |
| Constructor and Description |
|---|
BaseBuffer() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
checkReadableBytes(int length)
Convenience method for checking if we have enough readable bytes
|
protected boolean |
checkReadableBytesSafe(int length)
Convenience method for checking if we have enough readable bytes
|
protected boolean |
checkWritableBytesSafe(int length)
Check whether we have enough space for writing the desired length.
|
abstract Buffer |
clone()
Performs a deep clone of this object.
|
boolean |
hasWritableBytes()
Checks whether this
Buffer has any space left for writing. |
int |
indexOf(byte b) |
int |
indexOf(int maxBytes,
byte... bytes)
Same as
Buffer.readUntil(int, byte...) but instead of returning the
buffer with everything up until the specified byte it returns the index
instead. |
protected static boolean |
isByteInArray(byte b,
byte[] bytes) |
int |
parseToInt()
Parse all the readable bytes in this buffer as a unsigned integer value.
|
int |
parseToInt(int radix)
(Copied from the Integer class and slightly altered to read from this
buffer instead of a String)
Parses the string argument as a signed integer in the radix specified by
the second argument.
|
Buffer |
readLine()
Reads a line, i.e., it reads until we hit a line feed ('\n') or a
carriage return ('\r'), or a carriage return followed immediately by a
line feed.
|
Buffer |
readUntil(byte b)
Same as
#readUntil(4096, 'b')
Read until the specified byte is encountered and return a buffer
representing that section of the buffer. |
Buffer |
readUntil(int maxBytes,
byte... bytes)
Read until any of the specified bytes have been encountered or until we
have read a maximum amount of bytes.
|
Buffer |
readUntilDoubleCRLF()
Read until we find a double CRLF.
|
Buffer |
readUntilSafe(int maxBytes,
byte... bytes)
Same as
Buffer.readUntil(int, byte...) but will return null instead of
throwing a ByteNotFoundException |
Buffer |
readUntilSingleCRLF()
Read until we find a single CRLF.
|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcapacity, dumpAsHex, equals, equalsIgnoreCase, getArray, getBit, getBit0, getBit1, getBit2, getBit3, getBit4, getBit5, getBit6, getBit7, getByes, getByte, getBytes, getBytes, getBytes, getInt, getLowerBoundary, getRawArray, getReadableBytes, getReaderIndex, getShort, getUnsignedByte, getUnsignedInt, getUnsignedShort, getUpperBoundary, getWritableBytes, getWriterIndex, hashCode, hasReadableBytes, hasWriteSupport, isEmpty, markReaderIndex, peekByte, readByte, readBytes, readInt, readShort, readUnsignedByte, readUnsignedInt, readUnsignedShort, resetReaderIndex, setByte, setInt, setReaderIndex, setUnsignedByte, setUnsignedInt, setUnsignedShort, setWriterIndex, slice, slice, slice, toString, unsignedInt, write, write, write, write, write, write, writeAsString, writeAsStringprotected static final byte LF
protected static final byte CR
public abstract Buffer clone()
Bufferpublic boolean hasWritableBytes()
BufferBuffer has any space left for writing. Same
as Buffer.getWritableBytes() > 0hasWritableBytes in interface Bufferprotected boolean checkWritableBytesSafe(int length)
length - protected void checkReadableBytes(int length)
throws IndexOutOfBoundsException
length - the length the user wishes to readIndexOutOfBoundsException - in case we don't have the bytes availableprotected boolean checkReadableBytesSafe(int length)
length - the length the user wishes to readpublic final Buffer readUntil(byte b) throws IOException, ByteNotFoundException
#readUntil(4096, 'b')
Read until the specified byte is encountered and return a buffer
representing that section of the buffer.
If the byte isn't found, then a ByteNotFoundException is thrown
and the Buffer.getReaderIndex() is left where we bailed out.
Note, the byte we are looking for will have been consumed so whatever
that is left in the Buffer will not contain that byte.
Example:
Buffer buffer = Buffers.wrap("hello world");
Buffer hello = buffer.readUntil((byte)' ');
System.out.println(hello); // will contain "hello"
System.out.println(buffer); // will contain "world"
As the example above illustrates, we are looking for a space, which is
found between "hello" and "world". Since the space will be consumed, the
original buffer will now only contain "world" and not " world".readUntil in interface Bufferb - the byte to look forByteNotFoundException - in case the byte we were looking for is not found.IOExceptionpublic final Buffer readUntil(int maxBytes, byte... bytes) throws IOException, ByteNotFoundException, IllegalArgumentException
Buffer.readUntil(byte) except it allows you to look for multiple bytes
and to specify for how many bytes we should be looking before we give up.
Example, we want to read until we either findreadUntil in interface BuffermaxBytes - the maximum number of bytes we would like to read before
giving up.bytes - the bytes we are looking for (either one of them)IOExceptionByteNotFoundException - in case none of the bytes we were looking for are found
within the specified maximum number of bytes.IllegalArgumentException - in no bytes to look for is specified.public final Buffer readUntilSafe(int maxBytes, byte... bytes) throws IOException, IllegalArgumentException
Buffer.readUntil(int, byte...) but will return null instead of
throwing a ByteNotFoundExceptionreadUntilSafe in interface BufferIOExceptionIllegalArgumentExceptionpublic final int indexOf(int maxBytes,
byte... bytes)
throws IOException,
ByteNotFoundException,
IllegalArgumentException
Buffer.readUntil(int, byte...) but instead of returning the
buffer with everything up until the specified byte it returns the index
instead.
NOTE. The index is representing where in the Buffer you can find
the byte and the index is in relation to the entire Buffer and
its capacity so even if you have already read x bytes, it would not
change the index of what you search for.
Example:indexOf in interface BuffermaxBytes - the maximum number of bytes we would like to read before
giving up.bytes - the bytes we are looking for (either one of them)IOExceptionByteNotFoundException - will ONLY be thrown if we haven't found the byte within the
maxBytes limit. If the buffer we are searching in is less
than maxBytes and we can't find what we are looking for then
negative one will be returned instead.IllegalArgumentExceptionpublic final int indexOf(byte b)
throws IOException,
ByteNotFoundException,
IllegalArgumentException
indexOf in interface BufferIOExceptionByteNotFoundExceptionIllegalArgumentExceptionpublic final Buffer readLine() throws IOException
readLine in interface BufferIOExceptionpublic final Buffer readUntilSingleCRLF() throws IOException
Buffer.readLine() but the
readLine doesn't enforce the CRLF being present, which is typical for
e.g. SIP and is important when reading bytes being streamed over e.g.
a network connectionreadUntilSingleCRLF in interface BufferIOExceptionpublic final Buffer readUntilDoubleCRLF() throws IOException
BufferreadUntilDoubleCRLF in interface BufferIOExceptionpublic final int parseToInt()
throws NumberFormatException,
IOException
BufferparseToInt in interface BufferNumberFormatException - in case the bytes in the buffer cannot be converted into an
integer value.IOException - in case anything goes wrong when reading from the underlyingpublic final int parseToInt(int radix)
throws NumberFormatException,
IOException
Character.digit(char, int) returns a nonnegative
value), except that the first character may be an ASCII minus sign
'-' ('\u002D') to indicate a negative
value. The resulting integer value is returned.
An exception of type NumberFormatException is thrown if any
of the following situations occurs:
null or is a string of length
zero.
Character.MIN_RADIX or larger than
Character.MAX_RADIX.
'-' (
'\u002D') provided that the string is longer than length
1.
int.
Examples:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
parseToInt in interface Bufferradix - the radix to be used while parsing s.NumberFormatException - if the String does not contain a parsable
int.IOExceptionprotected static boolean isByteInArray(byte b,
byte[] bytes)
Copyright © 2021. All Rights Reserved.