public class BufferAllocator extends FilterAllocator implements Buffer
BufferAllocator
object is used to provide a means
to allocate buffers using a single underlying buffer. This uses a
buffer from a existing allocator to create the region of memory to
use to allocate all other buffers. As a result this allows a single
buffer to acquire the bytes in a number of associated buffers. This
has the advantage of allowing bytes to be read in sequence without
joining data from other buffers or allocating multiple regions.capacity, limit, source
Constructor and Description |
---|
BufferAllocator(Allocator source)
Constructor for the
BufferAllocator object. |
BufferAllocator(Allocator source,
long capacity)
Constructor for the
BufferAllocator object. |
BufferAllocator(Allocator source,
long capacity,
long limit)
Constructor for the
BufferAllocator object. |
Modifier and Type | Method and Description |
---|---|
Buffer |
allocate()
This method is used to allocate a default buffer.
|
Buffer |
allocate(long size)
This method is used to allocate a default buffer.
|
Buffer |
append(byte[] array)
This method is used to append bytes to the end of the buffer.
|
Buffer |
append(byte[] array,
int off,
int size)
This method is used to append bytes to the end of the buffer.
|
void |
clear()
This will clear all data from the buffer.
|
void |
close()
This method is used to ensure the buffer can be closed.
|
String |
encode()
This method is used to acquire the buffered bytes as a string.
|
String |
encode(String charset)
This method is used to acquire the buffered bytes as a string.
|
long |
length()
This is used to provide the number of bytes that have been
written to the buffer.
|
InputStream |
open()
This method is used so that a buffer can be represented as a
stream of bytes.
|
public BufferAllocator(Allocator source)
BufferAllocator
object. This is
used to instantiate the allocator with a default buffer size of
half a kilobyte. This ensures that it can be used for general
purpose byte storage and for minor I/O tasks.source
- this is where the underlying buffer is allocatedpublic BufferAllocator(Allocator source, long capacity)
BufferAllocator
object. This is
used to instantiate the allocator with a specified buffer size.
This is typically used when a very specific buffer capacity is
required, for example a request body with a known length.source
- this is where the underlying buffer is allocatedcapacity
- the initial capacity of the allocated bufferspublic BufferAllocator(Allocator source, long capacity, long limit)
BufferAllocator
object. This is
used to instantiate the allocator with a specified buffer size.
This is typically used when a very specific buffer capacity is
required, for example a request body with a known length.source
- this is where the underlying buffer is allocatedcapacity
- the initial capacity of the allocated bufferslimit
- this is the maximum buffer size created by thispublic InputStream open() throws IOException
open
in interface Buffer
IOException
public String encode() throws IOException
encode
in interface Buffer
IOException
public String encode(String charset) throws IOException
encode
in interface Buffer
charset
- this is the charset to encode the data withIOException
public Buffer append(byte[] array) throws IOException
append
in interface Buffer
array
- this is the byte array to append to this bufferIOException
public Buffer append(byte[] array, int off, int size) throws IOException
append
in interface Buffer
array
- this is the byte array to append to this buffersize
- the number of bytes to be read from the arrayoff
- this is the offset to begin reading the bytes fromIOException
public void clear() throws IOException
clear
in interface Buffer
IOException
public void close() throws IOException
close
in interface Buffer
IOException
public Buffer allocate() throws IOException
allocate
in interface Allocator
allocate
in interface Buffer
allocate
in class FilterAllocator
IOException
public Buffer allocate(long size) throws IOException
allocate
in interface Allocator
allocate
in class FilterAllocator
size
- the initial capacity of the allocated bufferIOException
public long length()
Copyright © 2019. All rights reserved.