Package io.micronaut.core.io.buffer
Class ReadBuffer
java.lang.Object
io.micronaut.core.io.buffer.ReadBuffer
- All Implemented Interfaces:
AutoCloseable
A buffer of bytes. Can be read from exactly once. Must be either consumed fully, or
closed
.
Consuming operations
Certain operations on ReadBuffers are consuming, meaning that they read all the remaining bytes in the buffer. Those operations are called out in the javadoc. After such an operation, the buffer does not need to be closed (though it's still allowed to do so).
- Since:
- 4.10.0
- Author:
- Jonas Konrad
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
close()
Closes this ReadBuffer, releasing any system resources associated with it.abstract @NonNull ReadBuffer
Create a new independent buffer that reads the same data as this one.protected abstract boolean
abstract @NonNull ReadBuffer
move()
Create a new buffer with the same content as this one, consuming this buffer in the process.protected abstract byte[]
peekArray
(int n) abstract int
readable()
Returns the number of bytes that can be read from this buffer.abstract @NonNull ReadBuffer
split
(int splitPosition) Split this buffer in two, returning the bytes up untilsplitPosition
as a new buffer.byte @NonNull []
toArray()
Returns the contents of this ReadBuffer as a byte array.abstract void
Reads the contents of this ReadBuffer into the specified destination array, which must have enough space for this buffer.Converts thisReadBuffer
into aByteBuffer
.Create a newInputStream
that reads the data of this buffer.toString()
Returns the contents of this ReadBuffer as a string.void
transferTo
(@NonNull OutputStream stream) Write this buffer to the givenOutputStream
.
-
Constructor Details
-
ReadBuffer
Internal constructor. Extension is only allowed in micronaut-core.
-
-
Method Details
-
readable
public abstract int readable()Returns the number of bytes that can be read from this buffer.- Returns:
- The number of readable bytes
- Throws:
IllegalStateException
- If this buffer is already closed or consumed
-
duplicate
Create a new independent buffer that reads the same data as this one. This buffer remains unchanged.- Returns:
- A new, independent buffer
- Throws:
IllegalStateException
- If this buffer is already closed or consumed
-
split
@NonNull public abstract @NonNull ReadBuffer split(int splitPosition) throws IndexOutOfBoundsException Split this buffer in two, returning the bytes up untilsplitPosition
as a new buffer. Those bytes will be removed from this buffer. This is not a consuming operation, even ifn == readable()
.- Parameters:
splitPosition
- Position where to split the data- Returns:
- A new, independent buffer that reads the first
splitPosition
bytes - Throws:
IllegalStateException
- If this buffer is already closed or consumedIndexOutOfBoundsException
- Ifn > readable()
-
move
Create a new buffer with the same content as this one, consuming this buffer in the process. If this buffer is later closed, the returned buffer remains usable.This is a consuming operation.
- Returns:
- a new ReadBuffer containing all the bytes that were previously in this ReadBuffer
- Throws:
IllegalStateException
- If this buffer is already closed or consumed
-
toArray
public abstract void toArray(byte @NonNull [] destination, int offset) throws IndexOutOfBoundsException Reads the contents of this ReadBuffer into the specified destination array, which must have enough space for this buffer.This is a consuming operation.
- Parameters:
destination
- the byte array to copy the contents intooffset
- the starting index in the destination array- Throws:
IllegalStateException
- If this buffer is already closed or consumedIndexOutOfBoundsException
- if the destination array is not large enough to hold the contents of this ReadBuffer, or if the offset is negative or exceeds the destination array's length
-
toArray
Returns the contents of this ReadBuffer as a byte array. Some implementations may share the array with other buffers, so the returned array should not be written to.This is a consuming operation.
- Returns:
- a byte array containing the contents of this ReadBuffer
- Throws:
IllegalStateException
- If this buffer is already closed or consumed
-
toString
Returns the contents of this ReadBuffer as a string.This is a consuming operation.
- Parameters:
charset
- the character encoding to use for converting the bytes to a string- Returns:
- a string representation of the contents of this ReadBuffer
- Throws:
IllegalStateException
- If this buffer is already closed or consumed
-
toByteBuffer
Converts thisReadBuffer
into aByteBuffer
. The returnedByteBuffer
will contain all the readable bytes from thisReadBuffer
.This is a consuming operation.
- Returns:
- a
ByteBuffer
containing the contents of thisReadBuffer
- Throws:
IllegalStateException
- If this buffer is already closed or consumed
-
toInputStream
Create a newInputStream
that reads the data of this buffer. The returned stream must be closed.This is a consuming operation.
- Returns:
- A stream reading from this buffer
- Throws:
IllegalStateException
- If this buffer is already closed or consumed
-
transferTo
Write this buffer to the givenOutputStream
.This is a consuming operation.
- Parameters:
stream
- The stream to write to- Throws:
IllegalStateException
- If this buffer is already closed or consumedIOException
- If theOutputStream
throws an exception
-
close
public abstract void close()Closes this ReadBuffer, releasing any system resources associated with it. May be called multiple times without effect. Note that consuming operations implicitly close the buffer as well.- Specified by:
close
in interfaceAutoCloseable
-
isConsumed
protected abstract boolean isConsumed() -
peekArray
protected abstract byte[] peekArray(int n) -
toString
-