Back to index

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

#include <nsBufferedStreams.h>

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

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIINPUTSTREAM
NS_DECL_NSIBUFFEREDINPUTSTREAM
NS_DECL_NSISTREAMBUFFERACCESS 
nsBufferedInputStream ()
virtual ~nsBufferedInputStream ()
nsIInputStreamSource ()
nsresult Close ()
void seek (in long whence, in long long offset)
 seek
long long tell ()
 tell
void setEOF ()
 setEOF
void init (in nsIInputStream fillFromStream, in unsigned long bufferSize)
void close ()
 Close the stream.
unsigned long available ()
unsigned long read (in charPtr aBuf, in unsigned long aCount)
 Read data from the stream.
unsigned long readSegments (in nsWriteSegmentFun aWriter, in voidPtr aClosure, in unsigned long aCount)
 Low-level read 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 ()
NS_IMETHOD Flush ()
nsresult Init (nsISupports *stream, PRUint32 bufferSize)

Protected Attributes

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

Detailed Description

Definition at line 90 of file nsBufferedStreams.h.


Constructor & Destructor Documentation

NS_DECL_ISUPPORTS_INHERITED NS_DECL_NSIINPUTSTREAM NS_DECL_NSIBUFFEREDINPUTSTREAM NS_DECL_NSISTREAMBUFFERACCESS nsBufferedInputStream::nsBufferedInputStream ( ) [inline]

Definition at line 100 of file nsBufferedStreams.h.

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

Definition at line 101 of file nsBufferedStreams.h.

{}

Member Function Documentation

unsigned long nsIInputStream::available ( ) [inherited]
Returns:
number of bytes currently available in the stream

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 nsIInputStream::close ( ) [inherited]

Close the stream.

static NS_METHOD nsBufferedInputStream::Create ( nsISupports *  aOuter,
REFNSIID  aIID,
void **  aResult 
) [static]

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_IMETHODIMP nsBufferedInputStream::Fill ( ) [protected, virtual]

Implements nsBufferedStream.

Definition at line 369 of file nsBufferedStreams.cpp.

{
    if (mBufferDisabled)
        return NS_OK;
    NS_ENSURE_TRUE(mStream, NS_BASE_STREAM_CLOSED);

    nsresult rv;
    PRInt32 rem = PRInt32(mFillPoint - mCursor);
    if (rem > 0) {
        // slide the remainder down to the start of the buffer
        // |<------------->|<--rem-->|<--->|
        // b               c         f     s
        memcpy(mBuffer, mBuffer + mCursor, rem);
    }
    mBufferStartOffset += mCursor;
    mFillPoint = rem;
    mCursor = 0;

    PRUint32 amt;
    rv = Source()->Read(mBuffer + mFillPoint, mBufferSize - mFillPoint, &amt);
    if (NS_FAILED(rv)) return rv;

    mFillPoint += amt;
    return NS_OK;
}

Here is the call graph for this function:

NS_IMETHOD nsBufferedInputStream::Flush ( void  ) [inline, protected, virtual]

Implements nsBufferedStream.

Definition at line 112 of file nsBufferedStreams.h.

{ return NS_OK; } // no-op for input streams
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 nsIBufferedInputStream::init ( in nsIInputStream  fillFromStream,
in unsigned long  bufferSize 
) [inherited]
Parameters:
fillFromStream- 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
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.
unsigned long nsIInputStream::read ( in charPtr  aBuf,
in unsigned long  aCount 
) [inherited]

Read data from the stream.

Parameters:
aBufthe buffer into which the data is to be read
aCountthe maximum number of bytes to be read
Returns:
number of bytes read (may be less than aCount).
0 if reached end of file
Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif reading from the input stream would block the calling thread (non-blocking mode only)
<other-error>on failure
unsigned long nsIInputStream::readSegments ( in nsWriteSegmentFun  aWriter,
in voidPtr  aClosure,
in unsigned long  aCount 
) [inherited]

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

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

Parameters:
aWriterthe "consumer" of the data to be read
aClosureopaque parameter passed to writer
aCountthe maximum number of bytes to be read
Returns:
number of bytes read (may be less than aCount)
0 if reached end of file (or if aWriter refused to consume data)
Exceptions:
NS_BASE_STREAM_WOULD_BLOCKif reading from the input 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 input stream).

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 106 of file nsBufferedStreams.h.

                             { 
        return (nsIInputStream*)mStream;
    }

Here is the caller graph for this function:

tell

This method reports the current offset, in bytes, from the start of the 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.

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: