Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes
nsBufferedOutputStream Class Reference

#include <nsBufferedStreams.h>

Inheritance diagram for nsBufferedOutputStream:
Inheritance graph
[legend]
Collaboration diagram for nsBufferedOutputStream:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIOUTPUTSTREAM
NS_DECL_NSISAFEOUTPUTSTREAM
NS_DECL_NSIBUFFEREDOUTPUTSTREAM
NS_DECL_NSISTREAMBUFFERACCESS 
nsBufferedOutputStream ()
virtual ~nsBufferedOutputStream ()
nsIOutputStreamSink ()
nsresult Close ()
void seek (in long whence, in long long offset)
 seek
long long tell ()
 tell
void setEOF ()
 setEOF
void finish ()
 Call this method to close the stream and cause the original target to be overwritten.
void init (in nsIOutputStream sinkToStream, in unsigned long bufferSize)
void close ()
 Close the stream.
void flush ()
 Flush the stream.
unsigned long write (in string aBuf, in unsigned long aCount)
 Write data into the stream.
unsigned long writeFrom (in nsIInputStream aFromStream, in unsigned long aCount)
 Writes data into the stream from an input stream.
unsigned long writeSegments (in nsReadSegmentFun aReader, in voidPtr aClosure, in unsigned long aCount)
 Low-level write method that has access to the stream's underlying buffer.
boolean isNonBlocking ()
charPtr getBuffer (in PRUint32 aLength, in PRUint32 aAlignMask)
 Get access to a contiguous, aligned run of bytes in the stream's buffer.
void putBuffer (in charPtr aBuffer, in PRUint32 aLength)
 Relinquish access to the stream's buffer, filling if at end of an input buffer, flushing if completing an output buffer.
void disableBuffering ()
 Disable and enable buffering on the stream implementing this interface.
void enableBuffering ()

Static Public Member Functions

static NS_METHOD Create (nsISupports *aOuter, REFNSIID aIID, void **aResult)

Public Attributes

const PRInt32 NS_SEEK_SET = 0
const PRInt32 NS_SEEK_CUR = 1
const PRInt32 NS_SEEK_END = 2
readonly attribute nsISupports unbufferedStream
 The underlying, unbuffered input or output stream.

Protected Member Functions

NS_IMETHOD Fill ()
nsresult Init (nsISupports *stream, PRUint32 bufferSize)
NS_IMETHOD Flush ()=0

Protected Attributes

nsCOMPtr< nsISafeOutputStreammSafeStream
PRUint32 mBufferSize
char * mBuffer
nsInt64 mBufferStartOffset
PRUint32 mCursor
PRUint32 mFillPoint
nsISupports * mStream
PRPackedBool mBufferDisabled
PRUint8 mGetBufferCount

Detailed Description

Definition at line 117 of file nsBufferedStreams.h.


Constructor & Destructor Documentation

NS_DECL_ISUPPORTS_INHERITED NS_DECL_NSIOUTPUTSTREAM NS_DECL_NSISAFEOUTPUTSTREAM NS_DECL_NSIBUFFEREDOUTPUTSTREAM NS_DECL_NSISTREAMBUFFERACCESS nsBufferedOutputStream::nsBufferedOutputStream ( ) [inline]

Definition at line 129 of file nsBufferedStreams.h.

virtual nsBufferedOutputStream::~nsBufferedOutputStream ( ) [inline, virtual]

Definition at line 130 of file nsBufferedStreams.h.

Here is the call graph for this function:


Member Function Documentation

Definition at line 104 of file nsBufferedStreams.cpp.

{
    NS_IF_RELEASE(mStream);
    if (mBuffer) {
        delete[] mBuffer;
        mBuffer = nsnull;
        mBufferSize = 0;
        mBufferStartOffset = 0;
        mCursor = 0;
        mFillPoint = 0;
    }
#ifdef METERING
    {
        static FILE *tfp;
        if (!tfp) {
            tfp = fopen("/tmp/bufstats", "w");
            if (tfp)
                setvbuf(tfp, NULL, _IOLBF, 0);
        }
        if (tfp) {
            fprintf(tfp, "seeks within buffer:    %u\n",
                    bufstats.mSeeksWithinBuffer);
            fprintf(tfp, "seeks outside buffer:   %u\n",
                    bufstats.mSeeksOutsideBuffer);
            fprintf(tfp, "buffer read on seek:    %u\n",
                    bufstats.mBufferReadUponSeek);
            fprintf(tfp, "buffer unread on seek:  %u\n",
                    bufstats.mBufferUnreadUponSeek);
            fprintf(tfp, "bytes read from buffer: %u\n",
                    bufstats.mBytesReadFromBuffer);
            for (PRUint32 i = 0; i < bufstats.mBigSeekIndex; i++) {
                fprintf(tfp, "bigseek[%u] = {old: %u, new: %u}\n",
                        i,
                        bufstats.mBigSeek[i].mOldOffset,
                        bufstats.mBigSeek[i].mNewOffset);
            }
        }
    }
#endif
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIOutputStream::close ( ) [inherited]

Close the stream.

Forces the output stream to flush any buffered data.

Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif unable to flush without blocking the calling thread (non-blocking mode only)
NS_METHOD nsBufferedOutputStream::Create ( nsISupports *  aOuter,
REFNSIID  aIID,
void **  aResult 
) [static]

Definition at line 494 of file nsBufferedStreams.cpp.

{
    NS_ENSURE_NO_AGGREGATION(aOuter);

    nsBufferedOutputStream* stream = new nsBufferedOutputStream();
    if (stream == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    NS_ADDREF(stream);
    nsresult rv = stream->QueryInterface(aIID, aResult);
    NS_RELEASE(stream);
    return rv;
}

Here is the call graph for this function:

Disable and enable buffering on the stream implementing this interface.

DisableBuffering flushes an output stream's buffer, and invalidates an input stream's buffer.

NS_IMETHOD nsBufferedOutputStream::Fill ( ) [inline, protected, virtual]

Implements nsBufferedStream.

Definition at line 140 of file nsBufferedStreams.h.

{ return NS_OK; } // no-op for input streams

Call this method to close the stream and cause the original target to be overwritten.

Note: if any call to |write| failed to write out all of the data given to it, then calling this method will |close| the stream and return failure. Further, if closing the stream fails, this method will return failure. The original target will be overwritten only if all calls to |write| succeeded and the stream was successfully closed.

NS_IMETHOD nsBufferedStream::Flush ( ) [protected, pure virtual, inherited]

Implemented in nsBufferedInputStream.

void nsIOutputStream::flush ( ) [inherited]

Flush the stream.

Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif unable to flush without blocking the calling thread (non-blocking mode only)
charPtr nsIStreamBufferAccess::getBuffer ( in PRUint32  aLength,
in PRUint32  aAlignMask 
) [inherited]

Get access to a contiguous, aligned run of bytes in the stream's buffer.

Exactly one successful getBuffer call must occur before a putBuffer call taking the non-null pointer returned by the successful getBuffer.

The run of bytes are the next bytes (modulo alignment padding) to read for an input stream, and the next bytes (modulo alignment padding) to store before (eventually) writing buffered data to an output stream. There can be space beyond this run of bytes in the buffer for further accesses before the fill or flush point is reached.

Parameters:
aLengthCount of contiguous bytes requested at the address A that satisfies (A & aAlignMask) == 0 in the buffer, starting from the current stream position, mapped to a buffer address B. The stream implementation must pad from B to A by skipping bytes (if input stream) or storing zero bytes (if output stream).
aAlignMaskBit-mask computed by subtracting 1 from the power-of-two alignment modulus (e.g., 3 or sizeof(PRUint32)-1 for PRUint32 alignment).
Returns:
The aligned pointer to aLength bytes in the buffer, or null if the buffer has no room for aLength bytes starting at the next address A after the current position that satisfies (A & aAlignMask) == 0.
void nsIBufferedOutputStream::init ( in nsIOutputStream  sinkToStream,
in unsigned long  bufferSize 
) [inherited]
Parameters:
sinkToStream- add buffering to this stream
bufferSize- specifies the maximum buffer size
nsresult nsBufferedStream::Init ( nsISupports *  stream,
PRUint32  bufferSize 
) [protected, inherited]

Definition at line 88 of file nsBufferedStreams.cpp.

{
    NS_ASSERTION(stream, "need to supply a stream");
    NS_ASSERTION(mStream == nsnull, "already inited");
    mStream = stream;
    NS_IF_ADDREF(mStream);
    mBufferSize = bufferSize;
    mBufferStartOffset = 0;
    mCursor = 0;
    mBuffer = new char[bufferSize];
    if (mBuffer == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    return NS_OK;
}
Returns:
true if stream is non-blocking

NOTE: writing to a blocking output stream will block the calling thread until all given data can be consumed by the stream.

void nsIStreamBufferAccess::putBuffer ( in charPtr  aBuffer,
in PRUint32  aLength 
) [inherited]

Relinquish access to the stream's buffer, filling if at end of an input buffer, flushing if completing an output buffer.

After a getBuffer call that returns non-null, putBuffer must be called.

Parameters:
aBufferA non-null pointer returned by getBuffer on the same stream buffer access object.
aLengthThe same count of contiguous bytes passed to the getBuffer call that returned aBuffer.
void nsISeekableStream::seek ( in long  whence,
in long long  offset 
) [inherited]

seek

This method moves the stream offset of the steam implementing this interface.

Parameters:
whencespecifies how to interpret the 'offset' parameter in setting the stream offset associated with the implementing stream.
offsetspecifies a value, in bytes, that is used in conjunction with the 'whence' parameter to set the stream offset of the implementing stream. A negative value causes seeking in the reverse direction.

setEOF

This method truncates the stream at the current offset.

Definition at line 135 of file nsBufferedStreams.h.

                            { 
        return (nsIOutputStream*)mStream;
    }

tell

This method reports the current offset, in bytes, from the start of the stream.

unsigned long nsIOutputStream::write ( in string  aBuf,
in unsigned long  aCount 
) [inherited]

Write data into the stream.

Parameters:
aBufthe buffer containing the data to be written
aCountthe maximum number of bytes to be written
Returns:
number of bytes written (may be less than aCount)
Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif writing to the output stream would block the calling thread (non-blocking mode only)
<other-error>on failure
unsigned long nsIOutputStream::writeFrom ( in nsIInputStream  aFromStream,
in unsigned long  aCount 
) [inherited]

Writes data into the stream from an input stream.

Parameters:
aFromStreamthe stream containing the data to be written
aCountthe maximum number of bytes to be written
Returns:
number of bytes written (may be less than aCount)
Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif writing to the output stream would block the calling thread (non-blocking mode only)
<other-error>on failure

NOTE: This method is defined by this interface in order to allow the output stream to efficiently copy the data from the input stream into its internal buffer (if any). If this method was provided as an external facility, a separate char* buffer would need to be used in order to call the output stream's other Write method.

unsigned long nsIOutputStream::writeSegments ( in nsReadSegmentFun  aReader,
in voidPtr  aClosure,
in unsigned long  aCount 
) [inherited]

Low-level write method that has access to the stream's underlying buffer.

The reader function may be called multiple times for segmented buffers. WriteSegments is expected to keep calling the reader until either there is nothing left to write or the reader returns an error. WriteSegments should not call the reader with zero bytes to provide.

Parameters:
aReaderthe "provider" of the data to be written
aClosureopaque parameter passed to reader
aCountthe maximum number of bytes to be written
Returns:
number of bytes written (may be less than aCount)
Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif writing to the output stream would block the calling thread (non-blocking mode only)
<other-error>on failure

NOTE: this function may be unimplemented if a stream has no underlying buffer (e.g., socket output stream).


Member Data Documentation

char* nsBufferedStream::mBuffer [protected, inherited]

Definition at line 68 of file nsBufferedStreams.h.

Definition at line 84 of file nsBufferedStreams.h.

PRUint32 nsBufferedStream::mBufferSize [protected, inherited]

Definition at line 67 of file nsBufferedStreams.h.

Definition at line 71 of file nsBufferedStreams.h.

PRUint32 nsBufferedStream::mCursor [protected, inherited]

Definition at line 75 of file nsBufferedStreams.h.

PRUint32 nsBufferedStream::mFillPoint [protected, inherited]

Definition at line 80 of file nsBufferedStreams.h.

Definition at line 85 of file nsBufferedStreams.h.

Definition at line 142 of file nsBufferedStreams.h.

nsISupports* nsBufferedStream::mStream [protected, inherited]

Definition at line 82 of file nsBufferedStreams.h.

Definition at line 62 of file nsISeekableStream.idl.

Definition at line 68 of file nsISeekableStream.idl.

Definition at line 56 of file nsISeekableStream.idl.

readonly attribute nsISupports nsIStreamBufferAccess::unbufferedStream [inherited]

The underlying, unbuffered input or output stream.

Definition at line 109 of file nsIStreamBufferAccess.idl.


The documentation for this class was generated from the following files: