123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
/*******************************************************************************

        copyright:      Copyright (c) 2004 Kris Bell. All rights reserved

        license:        BSD style: $(LICENSE)

        version:        Mar 2004: Initial release$(BR)
                        Dec 2006: Outback release

        authors:        Kris

*******************************************************************************/

module tango.io.stream.Buffered;

public import tango.io.model.IConduit;

private import tango.io.device.Conduit;

/******************************************************************************

******************************************************************************/

public alias BufferedInput  Bin;        /// Shorthand aliases.
public alias BufferedOutput Bout;       /// ditto

/******************************************************************************

******************************************************************************/

extern (C)
{
        int printf (char*, ...);
        private void * memmove (void *dst, const(void) *src, size_t);
}

/******************************************************************************

******************************************************************************/

private enum immutable(char)[] underflow = "input buffer is empty";
private enum immutable(char)[] eofRead   = "end-of-flow whilst reading";
private enum immutable(char)[] eofWrite  = "end-of-flow whilst writing";
private enum immutable(char)[] overflow  = "output buffer is full";


/*******************************************************************************

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

*******************************************************************************/

class BufferedInput : InputFilter, InputBuffer
{
        alias flush             clear;          /// Clear/flush are the same.
        alias InputFilter.input input;          /// Access the source.

        private void[]        data;             // The raw data buffer.
        private size_t        index;            // Current read position.
        private size_t        extent;           // Limit of valid content.
        private size_t        dimension;        // Maximum extent of content.

        /***********************************************************************

                Ensure the buffer remains valid between method calls.

        ***********************************************************************/

        invariant()
        {
                assert (index <= extent);
                assert (extent <= dimension);
        }

        /***********************************************************************

                Construct a buffer.

                Params:
                stream = An input stream.
                capacity = Desired buffer capacity.

                Remarks:
                Construct a Buffer upon the provided input stream.

        ***********************************************************************/

        this (InputStream stream)
        {
                assert (stream);
                this (stream, stream.conduit.bufferSize);
        }

        /***********************************************************************

                Construct a buffer.

                Params:
                stream = An input stream.
                capacity = Desired buffer capacity.

                Remarks:
                Construct a Buffer upon the provided input stream.

        ***********************************************************************/

        this (InputStream stream, size_t capacity)
        {
                set (new ubyte[capacity], 0);
                super (source = stream);
        }

        /***********************************************************************

                Attempt to share an upstream Buffer, and create an instance
                where there's not one available.

                Params:
                stream = An 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).

        ***********************************************************************/

        static InputBuffer create (InputStream stream)
        {
                auto source = stream;
                auto conduit = source.conduit;
                while (cast(Mutator) source is null)
                      {
                      auto b = cast(InputBuffer) source;
                      if (b)
                          return b;
                      if (source is conduit)
                          break;
                      source = source.input;
                      assert (source);
                      }

                return new BufferedInput (stream, conduit.bufferSize);
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final size_t populate ()
        {
                return writer (&input.read);
        }

        /***********************************************************************

                Return a void[] slice of the buffer from start to end, where
                end is exclusive.

        ***********************************************************************/

        final void[] opSlice (size_t start, size_t end)
        {
                assert (start <= extent && end <= extent && start <= end);
                return data [start .. end];
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final void[] slice ()
        {
                return  data [index .. extent];
        }

        /***********************************************************************

                Access buffer content.

                Params:
                size =  Number of bytes to access.
                eat =   Whether 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:
                ---
                // create a buffer with some content
                auto buffer = new Buffer ("hello world");

                // consume everything unread
                auto slice = buffer.slice (buffer.readable);
                ---

        ***********************************************************************/

        final void[] slice (size_t size, bool eat = true)
        {
                if (size > readable)
                   {
                   // make some space? This will try to leave as much content
                   // in the buffer as possible, such that entire records may
                   // be aliased directly from within.
                   if (size > (dimension - index))
                   {
                       if (size <= dimension)
                           compress;
                       else
                          conduit.error (underflow);
                   }
                   // populate tail of buffer with new content
                   do {
                      if (writer (&source.read) is Eof)
                          conduit.error (eofRead);
                      } while (size > readable);
                   }

                auto i = index;
                if (eat)
                    index += size;
                return data [i .. i + size];
        }

        /***********************************************************************

                Read directly from this buffer.

                Params:
                dg = Callback 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.

        ***********************************************************************/

        final size_t reader (scope size_t delegate (const(void)[]) dg)
        {
                auto count = dg (data [index..extent]);

                if (count != Eof)
                   {
                   index += count;
                   assert (index <= extent);
                   }
                return count;
        }

        /***********************************************************************

                Write into this buffer.

                Params:
                dg = The 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.

        ***********************************************************************/

        public size_t writer (scope size_t delegate (void[]) dg)
        {
                auto count = dg (data [extent..dimension]);

                if (count != Eof)
                   {
                   extent += count;
                   assert (extent <= dimension);
                   }
                return count;
        }

        /***********************************************************************

                Transfer content into the provided dst.

                Params:
                dst = Destination 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.

        ***********************************************************************/

        final override size_t read (void[] dst)
        {
                size_t content = readable;
                if (content)
                   {
                   if (content >= dst.length)
                       content = dst.length;

                   // transfer buffer content
                   dst [0 .. content] = data [index .. index + content];
                   index += content;
                   }
                else
                   // pathological cases read directly from conduit
                   if (dst.length > dimension)
                       content = source.read (dst);
                   else
                      {
                      if (writable is 0)
                          index = extent = 0;  // same as clear, without call-chain

                      // keep buffer partially populated
                      if ((content = writer (&source.read)) != Eof && content > 0)
                           content = read (dst);
                      }
                return content;
        }

        /**********************************************************************

                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.

                Params:
                dst = Where data should be placed.
                exact = Whether to throw an exception when dst is not
                        filled (an Eof occurs first). Defaults to false.

        **********************************************************************/

        final size_t fill (void[] dst, bool exact = false)
        {
                size_t len = 0;

                while (len < dst.length)
                      {
                      size_t i = read (dst [len .. $]);
                      if (i is Eof)
                         {
                         if (exact && len < dst.length)
                             conduit.error (eofRead);
                         return (len > 0) ? len : Eof;
                         }
                      len += i;
                      }
                return len;
        }

        /***********************************************************************

                Move the current read location.

                Params:
                size = The 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).

        ***********************************************************************/

        final bool skip (ptrdiff_t size)
        {
                if (size < 0)
                   {
                   size = -size;
                   if (index >= size)
                      {
                      index -= size;
                      return true;
                      }
                   return false;
                   }
                return slice(size) !is null;
        }

        /***********************************************************************

                Move the current read location.

        ***********************************************************************/

        final override long seek (long offset, Anchor start = Anchor.Begin)
        {
                if (start is Anchor.Current)
                   {
                   // handle this specially because we know this is
                   // buffered - we should take into account the buffer
                   // position when seeking
                   offset -= readable;
                   auto bpos = offset + limit;

                   if (bpos >= 0 && bpos < limit)
                      {
                      // the new position is within the current
                      // buffer, skip to that position.
                      skip (cast(int) bpos - cast(int) position);

                      // see if we can return a valid offset
                      auto pos = source.seek (0, Anchor.Current);
                      if (pos != Eof)
                          return pos - readable;
                      return Eof;
                      }
                   // else, position is outside the buffer. Do a real
                   // seek using the adjusted position.
                   }

                clear();
                return source.seek (offset, start);
        }

        /***********************************************************************

                Iterator support.

                Params:
                scan = The 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.

        ***********************************************************************/

        final bool next (scope size_t delegate (const(void)[]) scan)
        {
                while (reader(scan) is Eof)
                      {
                      // did we start at the beginning?
                      if (position)
                          // yep - move partial token to start of buffer
                          compress;
                      else
                         // no more space in the buffer?
                         if (writable is 0)
                             conduit.error ("BufferedInput.next :: input buffer is full");

                      // read another chunk of data
                      if (writer(&source.read) is Eof)
                          return false;
                      }
                return true;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final size_t reserve (size_t space)
        {
                assert (space < dimension);

                if ((dimension - index) < space)
                     compress;
                return index;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        @property final BufferedInput compress ()
        {
                auto r = readable;

                if (index > 0 && r > 0)
                    // content may overlap ...
                    memmove (&data[0], &data[index], r);

                index = 0;
                extent = r;
                return this;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final size_t drain (OutputStream dst)
        {
                assert (dst);

                size_t ret = reader (&dst.write);
                compress;
                return ret;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        @property final const size_t limit ()
        {
                return extent;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final const size_t capacity ()
        {
                return dimension;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        @property final const size_t position ()
        {
                return index;
        }

        /***********************************************************************

                Available content.

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

        ***********************************************************************/

        @property final const size_t readable ()
        {
                return extent - index;
        }

        /***********************************************************************

                Cast to a target type without invoking the wrath of the
                runtime checks for misalignment. Instead, we truncate the
                array length.

        ***********************************************************************/

        static inout(T)[] convert(T)(inout(void)[] x)
        {
                return (cast(inout(T)*) x.ptr) [0 .. (x.length / T.sizeof)];
        }

        /***********************************************************************

                Clear buffer content.

                Remarks:
                Reset 'position' and 'limit' to zero. This effectively
                clears all content from the buffer.

        ***********************************************************************/

        final override BufferedInput flush ()
        {
                index = extent = 0;

                // clear the filter chain also
                if (source)
                    super.flush();
                return this;
        }

        /***********************************************************************

                Set the input stream.

        ***********************************************************************/

        @property final void input (InputStream source)
        {
                this.source = source;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final override void[] load (size_t max = size_t.max)
        {
                load (super.input, super.conduit.bufferSize, max);
                return slice();
        }

        /***********************************************************************

                Import content from the specified conduit, expanding
                as necessary up to the indicated maximum or until an
                Eof occurs.

                Returns the number of bytes contained.

        ***********************************************************************/

        private size_t load (InputStream src, size_t increment, size_t max)
        {
                size_t  len,
                        count;

                // make some room
                compress;

                // explicitly resize?
                if (max != max.max)
                    if ((len = writable) < max)
                         increment = max - len;

                while (count < max)
                      {
                      if (! writable)
                         {
                         dimension += increment;
                         data.length = dimension;
                         }
                      if ((len = writer(&src.read)) is Eof)
                           break;
                      else
                         count += len;
                      }
                return count;
        }

        /***********************************************************************

                Reset the buffer content.

                Params:
                data =          The backing array to buffer within.
                readable =      The number of bytes within data considered
                                valid.

                Returns:
                The buffer instance.

                Remarks:
                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 clear() to reset the content
                (make it all writable).

        ***********************************************************************/

        private final BufferedInput set (void[] data, size_t readable)
        {
                this.data = data;
                this.extent = readable;
                this.dimension = data.length;

                // reset to start of input
                this.index = 0;

                return this;
        }

        /***********************************************************************

                Available space.

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

        ***********************************************************************/

        @property private final const size_t writable ()
        {
                return dimension - extent;
        }
}



/*******************************************************************************

        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.

*******************************************************************************/

class BufferedOutput : OutputFilter, OutputBuffer
{
        alias OutputFilter.output output;       /// access the sink

        private void[]        data;             // the raw data buffer
        private size_t        index;            // current read position
        private size_t        extent;           // limit of valid content
        private size_t        dimension;        // maximum extent of content

        /***********************************************************************

                Ensure the buffer remains valid between method calls.

        ***********************************************************************/

        invariant()
        {
                assert (index <= extent);
                assert (extent <= dimension);
        }

        /***********************************************************************

                Construct a buffer.

                Params:
                stream = An input stream.
                capacity = Desired buffer capacity.

                Remarks:
                Construct a Buffer upon the provided input stream.

        ***********************************************************************/

        this (OutputStream stream)
        {
                assert (stream);
                this (stream, stream.conduit.bufferSize);
        }

        /***********************************************************************

                Construct a buffer.

                Params:
                stream = An input stream.
                capacity = Desired buffer capacity.

                Remarks:
                Construct a Buffer upon the provided input stream.

        ***********************************************************************/

        this (OutputStream stream, size_t capacity)
        {
                set (new ubyte[capacity], 0);
                super (sink = stream);
        }

        /***********************************************************************

                Attempts to share an upstream BufferedOutput, and creates a new
                instance where there's not a shared one available.

                Params:
                stream = An 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

        ***********************************************************************/

        static OutputBuffer create (OutputStream stream)
        {
                auto sink = stream;
                auto conduit = sink.conduit;
                while (cast(Mutator) sink is null)
                      {
                      auto b = cast(OutputBuffer) sink;
                      if (b)
                          return b;
                      if (sink is conduit)
                          break;
                      sink = sink.output;
                      assert (sink);
                      }

                return new BufferedOutput (stream, conduit.bufferSize);
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final void[] slice ()
        {
                return data [index .. extent];
        }

        /***********************************************************************

                Emulate OutputStream.write().

                Params:
                src = The 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.

        ***********************************************************************/

        final override size_t write (const(void)[] src)
        {
                append (src.ptr, src.length);
                return src.length;
        }

        /***********************************************************************

                Append content.

                Params:
                src = The 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.

        ***********************************************************************/

        final BufferedOutput append (const(void)[] src)
        {
                return append (src.ptr, src.length);
        }

        /***********************************************************************

                Append content.

                Params:
                src = The content to _append.
                length = The 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.

        ***********************************************************************/

        final BufferedOutput append (const(void)* src, size_t length)
        {
                if (length > writable)
                   {
                   flush();

                   // check for pathological case
                   if (length > dimension)
                       do {
                          auto written = sink.write (src [0 .. length]);
                          if (written is Eof)
                              conduit.error (eofWrite);
                          length -= written;
                          src += written;
                          } while (length > dimension);
                    }

                // avoid "out of bounds" test on zero length
                if (length)
                   {
                   // content may overlap ...
                   memmove (&data[extent], src, length);
                   extent += length;
                   }
                return this;
        }

        /***********************************************************************

                Available space.

                Remarks:
                Return count of _writable bytes available in buffer. This is
                calculated as capacity() - limit().

        ***********************************************************************/

        @property final const size_t writable ()
        {
                return dimension - extent;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        @property final const size_t limit ()
        {
                return extent;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final const size_t capacity ()
        {
                return dimension;
        }

        /***********************************************************************

                Truncate buffer content.

                Remarks:
                Truncate the buffer within its extent. Returns true if
                the new length is valid, false otherwise.

        ***********************************************************************/

        final bool truncate (size_t length)
        {
                if (length <= data.length)
                   {
                   extent = length;
                   return true;
                   }
                return false;
        }

        /***********************************************************************

                Cast to a target type without invoking the wrath of the
                runtime checks for misalignment. Instead, we truncate the
                array length.

        ***********************************************************************/

        static T[] convert(T)(void[] x)
        {
                return (cast(T*) x.ptr) [0 .. (x.length / T.sizeof)];
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final override BufferedOutput flush ()
        {
                while (readable > 0)
                      {
                      auto ret = reader (&sink.write);
                      if (ret is Eof)
                          conduit.error (eofWrite);
                      }

                // flush the filter chain also
                clear();
                super.flush();
                return this;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final override BufferedOutput copy (InputStream src, size_t max = -1)
        {
                size_t chunk,
                       copied;

                while (copied < max && (chunk = writer(&src.read)) != Eof)
                      {
                      copied += chunk;

                      // don't drain until we actually need to
                      if (writable is 0)
                          if (drain(sink) is Eof)
                              conduit.error (eofWrite);
                      }
                return this;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final size_t drain (OutputStream dst)
        {
                assert (dst);

                size_t ret = reader (&dst.write);
                compress;
                return ret;
        }

        /***********************************************************************

                Clear buffer content.

                Remarks:
                Reset 'position' and 'limit' to zero. This effectively
                clears all content from the buffer.

        ***********************************************************************/

        final BufferedOutput clear ()
        {
                index = extent = 0;
                return this;
        }

        /***********************************************************************

                Set the output stream.

        ***********************************************************************/

        @property final void output (OutputStream sink)
        {
                this.sink = sink;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        final override long seek (long offset, Anchor start = Anchor.Begin)
        {
                clear();
                return super.seek (offset, start);
        }

        /***********************************************************************

                Write into this buffer.

                Params:
                dg = The 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.

        ***********************************************************************/

        final size_t writer (scope size_t delegate (void[]) dg)
        {
                auto count = dg (data [extent..dimension]);

                if (count != Eof)
                   {
                   extent += count;
                   assert (extent <= dimension);
                   }
                return count;
        }

        /***********************************************************************

                Read directly from this buffer.

                Params:
                dg = Callback 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 Eof to indicate an error.

        ***********************************************************************/

        private final size_t reader (scope size_t delegate (const(void)[]) dg)
        {
                auto count = dg (data [index..extent]);

                if (count != Eof)
                   {
                   index += count;
                   assert (index <= extent);
                   }
                return count;
        }

        /***********************************************************************

                Available content.

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

        ***********************************************************************/

        @property private final const size_t readable ()
        {
                return extent - index;
        }

        /***********************************************************************

                Reset the buffer content.

                Params:
                data =     The backing array to buffer within.
                readable = The number of bytes within data considered
                           valid.

                Returns:
                The buffer instance.

                Remarks:
                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 clear() to reset the content
                (make it all writable).

        ***********************************************************************/

        private final BufferedOutput set (void[] data, size_t readable)
        {
                this.data = data;
                this.extent = readable;
                this.dimension = data.length;

                // reset to start of input
                this.index = 0;

                return this;
        }

        /***********************************************************************

                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.

        ***********************************************************************/

        @property private final BufferedOutput compress ()
        {
                size_t r = readable;

                if (index > 0 && r > 0)
                    // content may overlap ...
                    memmove (&data[0], &data[index], r);

                index = 0;
                extent = r;
                return this;
        }
}



/******************************************************************************

******************************************************************************/

debug (Buffered)
{
        void main()
        {
                auto input = new BufferedInput (null);
                auto output = new BufferedOutput (null);
        }
}