public abstract class AbstractBuffer extends java.lang.Object implements Buffer
| Modifier and Type | Field and Description |
|---|---|
protected int |
lowerBoundary
We will pretend that any bytes below this boundary doesn't exist.
|
protected int |
markedReaderIndex
The position of the reader index that has been marked.
|
protected int |
readerIndex
From where we will continue reading
|
protected int |
upperBoundary
Any bytes above this boundary is not accessible to us
|
protected int |
writerIndex
This is where we will write the next byte.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractBuffer(int readerIndex,
int lowerBoundary,
int upperBoundary,
int writerIndex) |
| Modifier and Type | Method and Description |
|---|---|
int |
capacity()
The capacity of this buffer.
|
protected void |
checkIndex(int index)
Convenience method for checking if we can read at the index
|
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.
|
protected void |
checkWriterIndex(int index)
Convenience method for checking whether we can write at the specified
index.
|
abstract Buffer |
clone()
Performs a deep clone of this object.
|
abstract boolean |
equals(java.lang.Object other)
Check whether to buffers are considered to be equal.
|
int |
getReadableBytes()
Returns the number of available bytes for reading without blocking.
|
int |
getReaderIndex()
The reader index
|
int |
getWritableBytes()
Get the number of writable bytes.
|
int |
getWriterIndex()
The writer index.
|
abstract int |
hashCode() |
boolean |
hasWritableBytes()
Checks whether this
Buffer has any space left for writing. |
boolean |
hasWriteSupport()
The underlying subclass should override this if it has write support.
|
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. |
void |
markReaderIndex()
Mark the current position of the reader index.
|
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.
|
short |
readUnsignedByte() |
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.
|
void |
resetReaderIndex()
Reset the reader index to the marked position or to the beginning of the
buffer if mark hasn't explicitly been called.
|
void |
setReaderIndex(int index) |
Buffer |
slice()
Slice off the rest of the buffer.
|
Buffer |
slice(int stop)
Same as
#slice(Buffer.getReaderIndex(), int) |
void |
write(byte b)
Write a byte to where the current writer index is pointing.
|
void |
write(java.lang.String s)
Same as
Buffer.write(String, String) where the charset is set to
"UTF-8" |
void |
write(java.lang.String s,
java.lang.String charset)
Write a string to this buffer using the specified charset to convert the String into bytes.
|
finalize, getClass, notify, notifyAll, toString, wait, wait, waitdumpAsHex, equalsIgnoreCase, getArray, getByte, getBytes, getBytes, getInt, getShort, getUnsignedByte, getUnsignedShort, hasReadableBytes, isEmpty, peekByte, readByte, readBytes, readInt, readShort, readUnsignedInt, readUnsignedShort, setByte, setInt, setUnsignedByte, setUnsignedInt, setUnsignedShort, slice, toString, write, write, writeAsString, writeAsStringprotected int readerIndex
protected int writerIndex
protected int markedReaderIndex
resetReaderIndex()protected int lowerBoundary
protected int upperBoundary
protected AbstractBuffer(int readerIndex,
int lowerBoundary,
int upperBoundary,
int writerIndex)
public abstract Buffer clone()
Bufferpublic int getReaderIndex()
getReaderIndex in interface Bufferpublic int getWriterIndex()
BuffergetWriterIndex in interface Bufferpublic void setReaderIndex(int index)
setReaderIndex in interface Bufferpublic int capacity()
public int getWritableBytes()
BuffergetWritableBytes in interface Bufferpublic boolean hasWritableBytes()
BufferBuffer has any space left for writing. Same
as Buffer.getWritableBytes() > 0hasWritableBytes in interface Bufferpublic void markReaderIndex()
markReaderIndex in interface Buffer#reset()public Buffer slice(int stop)
#slice(Buffer.getReaderIndex(), int)public Buffer slice()
Buffer#slice(Buffer.getReaderIndex(), buffer.getCapacity())
Note, if you slice an empty buffer you will get back another empty
buffer. Same goes for when you slice a buffer whose bytes already have
been consumed.public int getReadableBytes()
InputStream may be able to read more off the stream,
however, it may not be able to do so without blocking.getReadableBytes in interface Bufferpublic void resetReaderIndex()
resetReaderIndex in interface Bufferpublic Buffer readUntil(byte b) throws java.io.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.java.io.IOExceptionpublic Buffer readUntil(int maxBytes, byte... bytes) throws java.io.IOException, ByteNotFoundException, java.lang.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)java.io.IOExceptionByteNotFoundException - in case none of the bytes we were looking for are found
within the specified maximum number of bytes.java.lang.IllegalArgumentException - in no bytes to look for is specified.public int indexOf(byte b)
throws java.io.IOException,
ByteNotFoundException,
java.lang.IllegalArgumentException
indexOf in interface Bufferjava.io.IOExceptionByteNotFoundExceptionjava.lang.IllegalArgumentExceptionpublic int indexOf(int maxBytes,
byte... bytes)
throws java.io.IOException,
ByteNotFoundException,
java.lang.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)java.io.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.java.lang.IllegalArgumentExceptionpublic Buffer readLine() throws java.io.IOException
public Buffer readUntilDoubleCRLF() throws java.io.IOException
BufferreadUntilDoubleCRLF in interface Bufferjava.io.IOExceptionprotected void checkReadableBytes(int length)
throws java.lang.IndexOutOfBoundsException
length - the length the user wishes to readjava.lang.IndexOutOfBoundsException - in case we don't have the bytes availableprotected boolean checkReadableBytesSafe(int length)
length - the length the user wishes to readprotected void checkIndex(int index)
throws java.lang.IndexOutOfBoundsException
index - java.lang.IndexOutOfBoundsExceptionprotected boolean checkWritableBytesSafe(int length)
length - protected void checkWriterIndex(int index)
throws java.lang.IndexOutOfBoundsException
index - java.lang.IndexOutOfBoundsExceptionpublic final short readUnsignedByte()
throws java.lang.IndexOutOfBoundsException,
java.io.IOException
readUnsignedByte in interface Bufferjava.lang.IndexOutOfBoundsExceptionjava.io.IOExceptionpublic boolean hasWriteSupport()
Buffer has write support or not. There are
buffers that do not allow you to write to them (such as the
EmptyBuffer) so if you try and write to such a buffer it will
throw a WriteNotSupportedException.hasWriteSupport in interface Bufferpublic void write(byte b)
throws java.lang.IndexOutOfBoundsException
BufferWriteNotSupportedExceptionpublic void write(java.lang.String s)
throws java.lang.IndexOutOfBoundsException,
WriteNotSupportedException,
java.io.UnsupportedEncodingException
BufferBuffer.write(String, String) where the charset is set to
"UTF-8"write in interface Bufferjava.lang.IndexOutOfBoundsException - in case we cannot write entire String to this Buffer.WriteNotSupportedException - in case the underlying implementation does not support
writes.java.io.UnsupportedEncodingException - in case the charset "UTF-8" is not supported by the platform.public void write(java.lang.String s,
java.lang.String charset)
throws java.lang.IndexOutOfBoundsException,
WriteNotSupportedException,
java.io.UnsupportedEncodingException
BufferwriterInxex of this buffer will be increased with the corresponding number
of bytes.
Note, either the entire string is written to this buffer or if it doesn't fit then nothing is
written to this buffer.write in interface Bufferjava.lang.IndexOutOfBoundsException - in case we cannot write entire String to this
Buffer.WriteNotSupportedExceptionjava.io.UnsupportedEncodingException - in case the specified charset is not supportedpublic abstract boolean equals(java.lang.Object other)
Bufferpublic abstract int hashCode()
public int parseToInt()
throws java.lang.NumberFormatException,
java.io.IOException
BufferparseToInt in interface Bufferjava.lang.NumberFormatException - in case the bytes in the buffer cannot be converted into an
integer value.java.io.IOException - in case anything goes wrong when reading from the underlyingpublic int parseToInt(int radix)
throws java.lang.NumberFormatException,
java.io.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 Buffers - the String containing the integer representation
to be parsedradix - the radix to be used while parsing s.java.lang.NumberFormatException - if the String does not contain a parsable
int.java.io.IOExceptionCopyright © 2014. All Rights Reserved.