tango.io.model.IBuffer

License:

BSD style: see license.txt

Version:

Mar 2004: Initial release Dec 2006: Outback release

Author:

Kris
class IBuffer : IConduit, Buffered [abstract] #
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.

Buffers are sometimes memory-only, in which case there is nothing left to do when a client has consumed all the content. Other buffers are themselves bound to an external device called a conduit. When this is the case, a consumer will eventually cause a buffer to reload via its associated conduit and previous buffer content will be lost. A similar approach is applied to clients which populate a buffer, whereby the content of a full buffer will be flushed to a bound conduit before continuing. Another variation is that of a memory-mapped buffer, whereby the buffer content is mapped directly to virtual memory exposed via the OS. This can be used to address large files as an array of content.

See tango.io.Buffer for more info.

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

Params:

rawThe content to consume. This is consumed verbatim, and in raw binary format ~ no implicit conversions are performed.

Remarks:

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)

Examples:

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

Params:

dstdestination of the content
bytessize of dst

Returns:

A reference to the populated content

Remarks:

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

Remarks:

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

Params:

sinkthe stream to attach to

Remarks:

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

Params:

sourcethe stream to attach to

Remarks:

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.

Params:

dstdestination of the content

Returns:

Return the number of bytes read, which may be less than dst.length. Eof is returned when no further content is available.

Remarks:

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()

Params:

srcthe content to write

Returns:

Return the number of bytes written, which will be Eof when the content cannot be written.

Remarks:

Appends all of dst to the buffer, flushing to an attached conduit as necessary.
OutputStream output() [abstract] #
Exposes configured output stream

Returns:

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.

Remarks:

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:

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.

Remarks:

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:

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.

Remarks:

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.

Remarks:

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

Remarks:

Propagate request to an attached OutputStream (this is a requirement for the OutputStream interface)
interface Buffered #
Supported by streams which are prepared to share an internal buffer instance. This is intended to avoid a situation whereby content is shunted unnecessarily from one buffer to another when "decorator" streams are connected together in arbitrary ways. Do not implement this if the internal buffer should not be accessed directly by another stream e.g. if wrapper methods manipulate content on the way in or out of the buffer.