Buffer is central concept in Tango I/O. Each buffer acts
as a queue (line) where items are removed from the front
and new items are added to the back. Buffers are modeled
by this interface and implemented in various ways.
Buffer can be read from and written to directly, though
various data-converters and filters are often leveraged
to apply structure to what might otherwise be simple raw
data.
Buffers may also be tokenized by applying an Iterator.
This can be handy when one is dealing with text input,
and/or the content suits a more fluid format than most
typical converters support. Iterator tokens are mapped
directly onto buffer content (sliced), making them quite
efficient in practice. Like other types of buffer client,
multiple iterators can be mapped onto one common buffer
and access will be serialized.
- IBuffer buffer() [abstract] ¶#
-
implements Buffered interface
- void[] getContent() [abstract] ¶#
-
Return the backing array
- IBuffer setContent(void[] data) [abstract] ¶#
-
Set the backing array with all content readable. Writing
to this will either flush it to an associated conduit, or
raise an Eof condition. Use IBuffer.clear() to reset the
content (make it all writable).
- IBuffer setContent(void[] data, size_t readable) [abstract] ¶#
-
Set the backing array with some content readable. Writing
to this will either flush it to an associated conduit, or
raise an Eof condition. Use IBuffer.clear() to reset the
content (make it all writable).
- IBuffer append(void* content, size_t length) [abstract] ¶#
-
Append an array of data into this buffer, and flush to the
conduit as necessary. Returns a chaining reference if all
data was written; throws an IOException indicating eof or
eob if not.
This is often used in lieu of a Writer.
- IBuffer append(void[] content) [abstract] ¶#
-
Append an array of data into this buffer, and flush to the
conduit as necessary. Returns a chaining reference if all
data was written; throws an IOException indicating eof or
eob if not.
This is often used in lieu of a Writer.
- IBuffer append(IBuffer other) [abstract] ¶#
-
Append another buffer to this one, and flush to the
conduit as necessary. Returns a chaining reference if all
data was written; throws an IOException indicating eof or
eob if not.
This is often used in lieu of a Writer.
- void consume(void[] src) [abstract] ¶#
-
Consume content from a producer
raw | The content to consume. This is consumed verbatim, and in
raw binary format ~ no implicit conversions are performed. |
This is often used in lieu of a Writer, and enables simple
classes, such as FilePath and Uri, to emit content directly
into a buffer (thus avoiding potential heap activity)
1
2
3
| auto path = new FilePath (somepath);
path.produce (&buffer.consume);
|
- void[] slice() [abstract] ¶#
-
Return a void[] slice of the buffer up to the limit of
valid content.
- void[] opSlice(size_t start, size_t end) [abstract] ¶#
-
Return a void[] slice of the buffer from start to end, where
end is exclusive
- void[] slice(size_t size, bool eat = true) [abstract] ¶#
-
Read a chunk of data from the buffer, loading from the
conduit as necessary. The requested number of bytes are
loaded into the buffer, and marked as having been read
when the 'eat' parameter is set true. When 'eat' is set
false, the read position is not adjusted.
Returns the corresponding buffer slice when successful,
or null if there's not enough data available (Eof; Eob).
- void[] readExact(void* dst, size_t bytes) [abstract] ¶#
-
Access buffer content
dst | destination of the content |
bytes | size of dst |
A reference to the populated content
Fill the provided array with content. We try to satisfy
the request from the buffer content, and read directly
from an attached conduit where more is required.
- size_t fill(void[] dst) [abstract] ¶#
-
Fill the provided buffer. Returns the number of bytes
actually read, which will be less than dst.length when
Eof has been reached and IConduit.Eof thereafter.
- size_t writer(size_t delegate (void[]) writer) [abstract] ¶#
-
Exposes the raw data buffer at the current write position,
The delegate is provided with a void[] representing space
available within the buffer at the current write position.
The delegate should return the approriate number of bytes
if it writes valid content, or IConduit.Eof on error.
Returns whatever the delegate returns.
- size_t reader(size_t delegate (void[]) reader) [abstract] ¶#
-
Exposes the raw data buffer at the current read position. The
delegate is provided with a void[] representing the available
data, and should return zero to leave the current read position
intact.
If the delegate consumes data, it should return the number of
bytes consumed; or IConduit.Eof to indicate an error.
Returns whatever the delegate returns.
- IBuffer compress() [abstract] ¶#
-
If we have some data left after an export, move it to
front-of-buffer and set position to be just after the
remains. This is for supporting certain conduits which
choose to write just the initial portion of a request.
Limit is set to the amount of data remaining. Position
is always reset to zero.
- bool skip(int size) [abstract] ¶#
-
Skip ahead by the specified number of bytes, streaming from
the associated conduit as necessary.
Can also reverse the read position by 'size' bytes. This may
be used to support lookahead-type operations.
Returns true if successful, false otherwise.
- bool next(size_t delegate (void[])) [abstract] ¶#
-
Support for tokenizing iterators.
Upon success, the delegate should return the byte-based
index of the consumed pattern (tail end of it). Failure
to match a pattern should be indicated by returning an
IConduit.Eof.
Each pattern is expected to be stripped of the delimiter.
An end-of-file condition causes trailing content to be
placed into the token. Requests made beyond Eof result
in empty matches (length == zero).
Note that additional iterator and/or reader instances
will stay in lockstep when bound to a common buffer.
Returns true if a token was isolated, false otherwise.
- size_t fill(InputStream src) [abstract] ¶#
-
Try to _fill the available buffer with content from the
specified conduit. We try to read as much as possible
by clearing the buffer when all current content has been
eaten. If there is no space available, nothing will be
read.
Returns the number of bytes read, or Conduit.Eof.
- size_t drain(OutputStream dst) [abstract] ¶#
-
Write as much of the buffer that the associated conduit
can consume.
Returns the number of bytes written, or Conduit.Eof.
- bool truncate(size_t extent) [abstract] ¶#
-
Truncate the buffer within its extent. Returns true if
the new 'extent' is valid, false otherwise.
- bool compress(bool yes) [abstract] ¶#
-
Configure the compression strategy for iterators
Iterators will tend to compress the buffered content in
order to maximize space for new data. You can disable this
behaviour by setting this boolean to false
- size_t readable() [abstract] ¶#
-
Return count of readable bytes remaining in buffer. This is
calculated simply as limit() - position().
- size_t writable() [abstract] ¶#
-
Return count of writable bytes available in buffer. This is
calculated simply as capacity() - limit().
- size_t reserve(size_t space) [abstract] ¶#
-
Reserve the specified space within the buffer, compressing
existing content as necessary to make room
Returns the current read point, after compression if that
was required
- size_t limit() [abstract] ¶#
-
Returns the limit of readable content within this buffer.
- size_t capacity() [abstract] ¶#
-
Returns the total capacity of this buffer.
- size_t position() [abstract] ¶#
-
Returns the current position within this buffer.
- IBuffer setConduit(IConduit conduit) [abstract] ¶#
-
Sets the external conduit associated with this buffer.
Buffers do not require an external conduit to operate, but
it can be convenient to associate one. For example, methods
read and write use it to import/export content as necessary.
- IBuffer output(OutputStream sink) [abstract] ¶#
-
Set output stream
sink | the stream to attach to |
Sets the external output stream associated with this buffer.
Buffers do not require an external stream to operate, but
it can be convenient to associate one. For example, methods
fill & drain use them to import/export content as necessary.
- IBuffer input(InputStream source) [abstract] ¶#
-
Set input stream
source | the stream to attach to |
Sets the external input stream associated with this buffer.
Buffers do not require an external stream to operate, but
it can be convenient to associate one. For example, methods
fill & drain use them to import/export content as necessary.
- size_t read(void[] dst) [abstract] ¶#
-
Transfer content into the provided dst.
dst | destination of the content |
Return the number of bytes read, which may be less than
dst.length. Eof is returned when no further content is
available.
Populates the provided array with content. We try to
satisfy the request from the buffer content, and read
directly from an attached conduit when the buffer is
empty.
- size_t write(void[] src) [abstract] ¶#
-
Emulate OutputStream.write()
Return the number of bytes written, which will be Eof when
the content cannot be written.
Appends all of dst to the buffer, flushing to an attached
conduit as necessary.
- OutputStream output() [abstract] ¶#
-
Exposes configured output stream
Returns the OutputStream associated with this buffer. Returns
null if the buffer is not attached to an output; that is, it's
not backed by some external medium.
Buffers do not require an external stream to operate, but
it can be convenient to associate them. For example, methods
fill & drain use them to import/export content as necessary.
- InputStream input() [abstract] ¶#
-
Exposes configured input stream
Returns the InputStream associated with this buffer. Returns
null if the buffer is not attached to an input; that is, it's
not backed by some external medium.
Buffers do not require an external stream to operate, but
it can be convenient to associate them. For example, methods
fill & drain use them to import/export content as necessary.
- void error(char[] msg) [abstract] ¶#
-
Throw an exception with the provided message
- IConduit conduit() [abstract] ¶#
-
Access configured conduit
Returns the conduit associated with this buffer. Returns
null if the buffer is purely memory based; that is, it's
not backed by some external medium.
Buffers do not require an external conduit to operate, but
it can be convenient to associate one. For example, methods
fill() & drain() use it to import/export content as necessary.
- size_t bufferSize() [abstract] ¶#
-
Return a preferred size for buffering conduit I/O.
- char[] toString() [abstract] ¶#
-
Return the name of this conduit.
- bool isAlive() [abstract] ¶#
-
Is the conduit alive?
- OutputStream flush() [abstract] ¶#
-
Flush the contents of this buffer to the related conduit.
Throws an IOException on premature eof.
- InputStream clear() [abstract] ¶#
-
Reset position and limit to zero.
- OutputStream copy(InputStream src, size_t max = -1) [abstract] ¶#
-
Copy content via this buffer from the provided src
conduit.
The src conduit has its content transferred through
this buffer via a series of fill & drain operations,
until there is no more content available. The buffer
content should be explicitly flushed by the caller.
Throws an IOException on premature Eof.
- void detach() [abstract] ¶#
-
Release external resources
- void close() [abstract] ¶#
-
Close the stream
Propagate request to an attached OutputStream (this is a
requirement for the OutputStream interface)