tango.io.stream.Buffered

License:

BSD style: see license.txt

Version:

Mar 2004: Initial release
Dec 2006: Outback release

Authors:

Kris
alias BufferedInput Bin
alias BufferedOutput Bout
Shorthand aliases.
class BufferedInput : InputFilter, InputBuffer
Buffers the flow of data from a upstream input. A downstream neighbour can locate and use this buffer instead of creating another instance of their own.
(Note that upstream is closer to the source, and downstream is further away.)
alias flush clear
Clear/flush are the same.
alias InputFilter.input input
Access the source.
invariant
Ensure the buffer remains valid between method calls.
this(InputStream stream)
Construct a buffer.

Parameters:

streamAn input stream.
capacityDesired buffer capacity.

Remarks:

Construct a Buffer upon the provided input stream.
this(InputStream stream, size_t capacity)
Construct a buffer.

Parameters:

streamAn input stream.
capacityDesired buffer capacity.

Remarks:

Construct a Buffer upon the provided input stream.
InputBuffer create(InputStream stream) [static]
Attempt to share an upstream Buffer, and create an instance where there's not one available.

Parameters:

streamAn input stream.

Remarks:

If an upstream Buffer instances is visible, it will be shared. Otherwise, a new instance is created based upon the bufferSize exposed by the stream endpoint (conduit).
size_t populate() [final]
Place more data from the source stream into this buffer, and return the number of bytes added. This does not compress the current buffer content, so consider doing that explicitly.

Returns:

Number of bytes added, which will be Eof when there is no further input available. Zero is also a valid response, meaning no data was actually added.
void[] opSlice(size_t start, size_t end) [final]
Return a void[] slice of the buffer from start to end, where end is exclusive.
void[] slice() [final]
Retrieve the valid content.

Returns:

A void[] slice of the buffer.

Remarks:

Return a void[] slice of the buffer, from the current position up to the limit of valid content. The content remains in the buffer for future extraction.
void[] slice(size_t size, bool eat = true) [final]
Access buffer content.

Parameters:

sizeNumber of bytes to access.
eatWhether to consume the content or not.

Returns:

The corresponding buffer slice when successful, or null if there's not enough data available (Eof; Eob).

Remarks:

Read a slice of data from the buffer, loading from the conduit as necessary. The specified number of bytes is sliced from 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.

Note that the slice cannot be larger than the size of the buffer ~ use method fill(void[]) instead where you simply want the content copied, or use conduit.read() to extract directly from an attached conduit. Also note that if you need to retain the slice, then it should be .dup'd before the buffer is compressed or repopulated.

Examples:

1
2
3
4
5
// create a buffer with some content
auto buffer = new Buffer ("hello world");

// consume everything unread
auto slice = buffer.slice (buffer.readable);
size_t reader(scope size_t delegate(const(void)[]) dg) [final]
Read directly from this buffer.

Parameters:

dgCallback to provide buffer access to.

Returns:

Returns whatever the delegate returns.

Remarks:

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.
size_t writer(scope size_t delegate(void[]) dg) [public]
Write into this buffer.

Parameters:

dgThe callback to provide buffer access to.

Returns:

Returns whatever the delegate returns.

Remarks:

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 appropriate number of bytes if it writes valid content, or IConduit.Eof on error.
size_t read(void[] dst) [override, final]
Transfer content into the provided dst.

Parameters:

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 fill(void[] dst, bool exact = false) [final]
Fill the provided buffer. Returns the number of bytes actually read, which will be less that dst.length when Eof has been reached and Eof thereafter.

Parameters:

dstWhere data should be placed.
exactWhether to throw an exception when dst is not filled (an Eof occurs first). Defaults to false.
bool skip(ptrdiff_t size) [final]
Move the current read location.

Parameters:

sizeThe number of bytes to move.

Returns:

Returns true if successful, false otherwise.

Remarks:

Skip ahead by the specified number of bytes, streaming from the associated conduit as necessary.

Can also reverse the read position by 'size' bytes, when size is negative. This may be used to support lookahead operations. Note that a negative size will fail where there is not sufficient content available in the buffer (can't _skip beyond the beginning).
long seek(long offset, Anchor start = Begin) [override, final]
Move the current read location.
bool next(scope size_t delegate(const(void)[]) scan) [final]
Iterator support.

Parameters:

scanThe delegate to invoke with the current content.

Returns:

Returns true if a token was isolated, false otherwise.

Remarks:

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 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 is zero).

Note that additional iterator and/or reader instances will operate in lockstep when bound to a common buffer.
size_t reserve(size_t space) [final]
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.
BufferedInput compress() [@property, final]
Compress buffer space.

Returns:

The buffer instance.

Remarks:

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.
size_t drain(OutputStream dst) [final]
Drain buffer content to the specific conduit.

Returns:

Returns the number of bytes written, or Eof.

Remarks:

Write as much of the buffer that the associated conduit can consume. The conduit is not obliged to consume all content, so some may remain within the buffer.
size_t limit() [@property, final, const]
Access buffer limit.

Returns:

Returns the limit of readable content within this buffer.

Remarks:

Each buffer has a capacity, a limit, and a position. The capacity is the maximum content a buffer can contain, limit represents the extent of valid content, and position marks the current read location.
size_t capacity() [final, const]
Access buffer capacity.

Returns:

Returns the maximum capacity of this buffer.

Remarks:

Each buffer has a capacity, a limit, and a position. The capacity is the maximum content a buffer can contain, limit represents the extent of valid content, and position marks the current read location.
size_t position() [@property, final, const]
Access buffer read position.

Returns:

Returns the current read-position within this buffer.

Remarks:

Each buffer has a capacity, a limit, and a position. The capacity is the maximum content a buffer can contain, limit represents the extent of valid content, and position marks the current read location.
size_t readable() [@property, final, const]
Available content.

Remarks:

Return count of _readable bytes remaining in buffer. This is calculated simply as limit() - position().
inout(T)[] convert(T)(inout(void)[] x) [static]
Cast to a target type without invoking the wrath of the runtime checks for misalignment. Instead, we truncate the array length.
BufferedInput flush() [override, final]
Clear buffer content.

Remarks:

Reset 'position' and 'limit' to zero. This effectively clears all content from the buffer.
void input(InputStream source) [@property, final]
Set the input stream.
void[] load(size_t max = max) [override, final]
Load the bits from a stream, up to an indicated length, and return them all in an array. The function may consume more than the indicated size where additional data is available during a block read operation, but will not wait for more than specified. An Eof terminates the operation.
Returns an array representing the content, and throws IOException on error.
class BufferedOutput : OutputFilter, OutputBuffer
Buffers the flow of data from a upstream output. A downstream neighbour can locate and use this buffer instead of creating another instance of their own.
(Note that upstream is closer to the source, and downstream is further away.)

Don't forget to flush() buffered content before closing.
alias OutputFilter.output output
access the sink
invariant
Ensure the buffer remains valid between method calls.
this(OutputStream stream)
Construct a buffer.

Parameters:

streamAn input stream.
capacityDesired buffer capacity.

Remarks:

Construct a Buffer upon the provided input stream.
this(OutputStream stream, size_t capacity)
Construct a buffer.

Parameters:

streamAn input stream.
capacityDesired buffer capacity.

Remarks:

Construct a Buffer upon the provided input stream.
OutputBuffer create(OutputStream stream) [static]
Attempts to share an upstream BufferedOutput, and creates a new instance where there's not a shared one available.

Parameters:

streamAn output stream.

Remarks:

Where an upstream instance is visible it will be returned. Otherwise, a new instance is created based upon the bufferSize exposed by the associated conduit
void[] slice() [final]
Retrieve the valid content.

Returns:

A void[] slice of the buffer.

Remarks:

Return a void[] slice of the buffer, from the current position up to the limit of valid content. The content remains in the buffer for future extraction.
size_t write(const(void)[] src) [override, final]
Emulate OutputStream.write().

Parameters:

srcThe content to write.

Returns:

Return the number of bytes written, which may be less than provided (conceptually).

Remarks:

Appends src content to the buffer, flushing to an attached conduit as necessary. An IOException is thrown upon write failure.
BufferedOutput append(const(void)[] src) [final]
Append content.

Parameters:

srcThe content to _append.

Returns a chaining reference if all content was written. Throws an IOException indicating Eof or Eob if not.

Remarks:

Append an array to this buffer, and flush to the conduit as necessary. This is often used in lieu of a Writer.
BufferedOutput append(const(void)* src, size_t length) [final]
Append content.

Parameters:

srcThe content to _append.
lengthThe number of bytes in src.

Returns a chaining reference if all content was written. Throws an IOException indicating Eof or Eob if not.

Remarks:

Append an array to this buffer, and flush to the conduit as necessary. This is often used in lieu of a Writer.
size_t writable() [@property, final, const]
Available space.

Remarks:

Return count of _writable bytes available in buffer. This is calculated as capacity() - limit().
size_t limit() [@property, final, const]
Access buffer limit.

Returns:

Returns the limit of readable content within this buffer.

Remarks:

Each buffer has a capacity, a limit, and a position. The capacity is the maximum content a buffer can contain, limit represents the extent of valid content, and position marks the current read location.
size_t capacity() [final, const]
Access buffer capacity.

Returns:

Returns the maximum capacity of this buffer.

Remarks:

Each buffer has a capacity, a limit, and a position. The capacity is the maximum content a buffer can contain, limit represents the extent of valid content, and position marks the current read location.
bool truncate(size_t length) [final]
Truncate buffer content.

Remarks:

Truncate the buffer within its extent. Returns true if the new length is valid, false otherwise.
T[] convert(T)(void[] x) [static]
Cast to a target type without invoking the wrath of the runtime checks for misalignment. Instead, we truncate the array length.
BufferedOutput flush() [override, final]
Flush all buffer content to the specific conduit.

Remarks:

Flush the contents of this buffer. This will block until all content is actually flushed via the associated conduit, whereas drain() will not.

Throws an IOException on premature Eof.
BufferedOutput copy(InputStream src, size_t max = -1) [override, final]
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.
size_t drain(OutputStream dst) [final]
Drain buffer content to the specific conduit.

Returns:

Returns the number of bytes written, or Eof.

Remarks:

Write as much of the buffer that the associated conduit can consume. The conduit is not obliged to consume all content, so some may remain within the buffer.
BufferedOutput clear() [final]
Clear buffer content.

Remarks:

Reset 'position' and 'limit' to zero. This effectively clears all content from the buffer.
void output(OutputStream sink) [@property, final]
Set the output stream.
long seek(long offset, Anchor start = Begin) [override, final]
Seek within this stream. Any and all buffered output is disposed before the upstream is invoked. Use an explicit flush() to emit content prior to seeking.
size_t writer(scope size_t delegate(void[]) dg) [final]
Write into this buffer.

Parameters:

dgThe callback to provide buffer access to.

Returns:

Returns whatever the delegate returns.

Remarks:

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 appropriate number of bytes if it writes valid content, or Eof on error.