Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Buffer Class Reference

Inheritance diagram for Buffer:

IBuffer GrowableBuffer MappedBuffer VersionMapped::MappedFile List of all members.

Public Member Functions

 this (uint capacity=0)
 this (void[] data)
 this (void[] data, uint readable)
IBuffer create ()
void[] getContent ()
IBuffer setValidContent (void[] data)
IBuffer setContent (void[] data, uint readable)
void[] get (uint size, bool eat=true)
IBuffer append (void[] src)
char[] toString ()
bool skip (int size)
uint readable ()
uint writable ()
int write (int(*dg)(void[]))
int read (int(*dg)(void[]))
IBuffer compress ()
void flush ()
IBuffer clear ()
uint getLimit ()
uint getCapacity ()
uint getPosition ()
IConduit getConduit ()
void setConduit (IConduit conduit)

Protected Member Functions

void error (char[] msg)
bool grow (uint size)
void copy (void *src, uint size)

Static Protected Attributes

static char[] overflow = "output buffer overflow"
static char[] underflow = "input buffer underflow"
static char[] eofRead = "end-of-file whilst reading"
static char[] eofWrite = "end-of-file whilst writing"

Private Attributes

void[] data
uint limit
uint capacity
uint position
IConduit conduit

Detailed Description

The basic premise behind this IO package is as follows:

An example of how to append a buffer follows:

        char[] foo = "to write some D";

        // create a small buffer
        Buffer buf = new Buffer (256);

        // append some text directly to it
        buf.append("now is the time for all good men ").append(foo);

        // output the combined string
        Stdout (buf.toString) (CR);

You might use a GrowableBuffer instead where you wish to append beyond a preset limit. One common usage of buffers is in conjunction with a conduit, such as FileConduit. Each Conduit exposes a factory for creating a buffer of the most appropriate size or flavour:

        FileConduit fc = new FileConduit ("file.name");
        Buffer buf = fc.createBuffer ();

However, this is typically hidden by higher level constructors such as those of Reader and Writer derivitives. For example:

        FileConduit fc = new FileConduit ("file.name");
        Reader reader = new Reader (fc);

There is indeed a buffer between the Reader and Conduit, but explicit construction is unecessary in many cases. See both Reader and Writer for examples of formatted IO.

Definition at line 151 of file Buffer.d.


Member Function Documentation

this uint  capacity = 0  )  [inline]
 

Construct a Buffer with the specified number of bytes.

Reimplemented in GrowableBuffer.

Definition at line 180 of file Buffer.d.

References capacity.

this void[]  data  )  [inline]
 

Prime buffer with an application-supplied array. There is no readable data present, and writing begins at position 0.

Definition at line 192 of file Buffer.d.

References data.

this void[]  data,
uint  readable
[inline]
 

Prime buffer with an application-supplied array, and indicate how much readable data is already there. A write operation will begin writing immediately after the existing content.

Definition at line 206 of file Buffer.d.

References data, readable(), and setContent().

void error char[]  msg  )  [inline, protected]
 

Throw an exception with the provided message

Definition at line 217 of file Buffer.d.

Referenced by append(), flush(), and get().

IBuffer create  )  [inline]
 

Create an instance of an IBuffer. Use this when you don't know anything about the concrete implementation, and have only the IBuffer interface available

Returns a Buffer with no content.

Reimplemented from IBuffer.

Definition at line 232 of file Buffer.d.

void [] getContent  )  [inline]
 

Return the backing array

Reimplemented from IBuffer.

Definition at line 243 of file Buffer.d.

References data.

IBuffer setValidContent void[]  data  )  [inline]
 

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

Reimplemented from IBuffer.

Definition at line 257 of file Buffer.d.

References setContent().

Referenced by StartLine::VersionUseTokenizer::parse(), ResponseLine::parse(), and FileBucket::Record::write().

IBuffer setContent void[]  data,
uint  readable
[inline]
 

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

Reimplemented from IBuffer.

Definition at line 271 of file Buffer.d.

Referenced by append(), FileBucket::Record::read(), setValidContent(), and this().

bool grow uint  size  )  [inline, protected]
 

Overridable method to grow the buffer size when it becomes full. Default is to not grow at all.

Reimplemented from IBuffer.

Reimplemented in GrowableBuffer.

Definition at line 290 of file Buffer.d.

Referenced by append().

void copy void *  src,
uint  size
[inline, protected]
 

Bulk copy of data from 'src'. Limit is adjusted by 'size' bytes.

Reimplemented in MappedBuffer.

Definition at line 302 of file Buffer.d.

References data, and limit.

Referenced by append().

void [] get uint  size,
bool  eat = true
[inline]
 

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

Reimplemented from IBuffer.

Definition at line 321 of file Buffer.d.

References capacity, compress(), conduit, data, IConduit::Eof, eofRead, error(), position, IConduit::read(), readable(), underflow, and writable().

Referenced by skip().

IBuffer append void[]  src  )  [inline]
 

Append an array of data to 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.

Reimplemented from IBuffer.

Definition at line 362 of file Buffer.d.

References capacity, conduit, copy(), data, error(), flush(), grow(), overflow, setContent(), and writable().

Referenced by testAppend(), and testClient().

char [] toString  )  [inline]
 

Return a char[] slice of the buffer up to the limit of valid content.

Reimplemented from IBuffer.

Definition at line 401 of file Buffer.d.

References data, limit, and position.

Referenced by VirtualCache::put(), and testAppend().

bool skip int  size  )  [inline]
 

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.

Reimplemented from IBuffer.

Definition at line 418 of file Buffer.d.

References get(), and position.

uint readable  )  [inline]
 

Return count of readable bytes remaining in buffer. This is calculated simply as limit() - position()

Reimplemented from IBuffer.

Definition at line 440 of file Buffer.d.

References limit, and position.

Referenced by compress(), flush(), get(), and this().

uint writable  )  [inline]
 

Return count of writable bytes available in buffer. This is calculated simply as capacity() - limit()

Reimplemented from IBuffer.

Reimplemented in MappedBuffer.

Definition at line 452 of file Buffer.d.

References capacity, and limit.

Referenced by append(), and get().

int write int(*)(void[])  dg  )  [inline]
 

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.

Returns whatever the delegate returns.

Reimplemented from IBuffer.

Reimplemented in MappedBuffer.

Definition at line 470 of file Buffer.d.

References assert(), capacity, data, and limit.

int read int(*)(void[])  dg  )  [inline]
 

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.

Reimplemented from IBuffer.

Definition at line 496 of file Buffer.d.

References assert(), data, limit, and position.

IBuffer compress  )  [inline]
 

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.

Reimplemented from IBuffer.

Reimplemented in MappedBuffer.

Definition at line 521 of file Buffer.d.

References data, limit, position, and readable().

Referenced by get().

void flush  )  [inline]
 

flush the contents of this buffer to the related conduit. Throws an IOException on premature eof.

Reimplemented from IBuffer.

Reimplemented in MappedBuffer.

Definition at line 541 of file Buffer.d.

References conduit, IConduit::Eof, eofWrite, error(), readable(), and IConduit::write().

Referenced by append(), and FileBucket::Record::write().

IBuffer clear  )  [inline]
 

Reset 'position' and 'limit' to zero

Reimplemented from IBuffer.

Reimplemented in MappedBuffer.

Definition at line 555 of file Buffer.d.

References limit, and position.

Referenced by Cluster::broadcast(), VirtualCache::put(), HttpMessage::setConduit(), testBuffer(), and testClient().

uint getLimit  )  [inline]
 

Returns the limit of readable content within this buffer

Reimplemented from IBuffer.

Definition at line 567 of file Buffer.d.

References limit.

uint getCapacity  )  [inline]
 

Returns the total capacity of this buffer

Reimplemented from IBuffer.

Definition at line 578 of file Buffer.d.

References capacity.

uint getPosition  )  [inline]
 

Returns the current read-position within this buffer

Reimplemented from IBuffer.

Definition at line 589 of file Buffer.d.

References position.

Referenced by Cluster::broadcast(), and ResponseLine::parse().

IConduit getConduit  )  [inline]
 

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 read and write use it to import/export content as necessary.

Reimplemented from IBuffer.

Definition at line 606 of file Buffer.d.

References conduit.

Referenced by HttpMessage::getConduit().

void setConduit IConduit  conduit  )  [inline]
 

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.

Reimplemented from IBuffer.

Reimplemented in MappedBuffer.

Definition at line 621 of file Buffer.d.

Referenced by SocketConduit::createBuffer(), HttpMessage::setConduit(), and FileBucket::this().


Member Data Documentation

void [] data [private]
 

Definition at line 153 of file Buffer.d.

Referenced by append(), compress(), copy(), get(), getContent(), read(), this(), toString(), and write().

uint limit [private]
 

Definition at line 154 of file Buffer.d.

Referenced by clear(), compress(), copy(), getLimit(), read(), readable(), toString(), writable(), and write().

uint capacity [private]
 

Definition at line 155 of file Buffer.d.

Referenced by append(), get(), getCapacity(), this(), writable(), and write().

uint position [private]
 

Definition at line 156 of file Buffer.d.

Referenced by clear(), compress(), get(), getPosition(), read(), readable(), skip(), and toString().

IConduit conduit [private]
 

Definition at line 157 of file Buffer.d.

Referenced by append(), flush(), get(), and getConduit().

char [] overflow = "output buffer overflow" [static, protected]
 

Definition at line 160 of file Buffer.d.

Referenced by append().

char [] underflow = "input buffer underflow" [static, protected]
 

Definition at line 161 of file Buffer.d.

Referenced by get().

char [] eofRead = "end-of-file whilst reading" [static, protected]
 

Definition at line 162 of file Buffer.d.

Referenced by get().

char [] eofWrite = "end-of-file whilst writing" [static, protected]
 

Definition at line 163 of file Buffer.d.

Referenced by flush().


The documentation for this class was generated from the following file:
Generated on Mon Nov 14 10:59:48 2005 for Mango by  doxygen 1.4.0