Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
nsStorageStream Class Reference

#include <nsStorageStream.h>

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

List of all members.

Public Member Functions

 nsStorageStream ()
void init (in PRUint32 segmentSize, in PRUint32 maxSize, in nsIMemory segmentAllocator)
 Initialize the stream, setting up the amount of space that will be allocated for the stream's backing-store.
nsIOutputStream getOutputStream (in PRInt32 startPosition)
 Get a reference to the one and only output stream for this instance.
nsIInputStream newInputStream (in PRInt32 startPosition)
 Create a new input stream to read data (written by the singleton output stream) from the internal buffer.
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 ()

Public Attributes

NS_DECL_ISUPPORTS
NS_DECL_NSISTORAGESTREAM
friend class 
nsStorageInputStream
attribute PRUint32 length
 The length attribute indicates the total number of bytes stored in the nsIStorageStream internal buffer, regardless of any consumption by input streams.
readonly attribute boolean writeInProgress
 True, when output stream has not yet been Close'ed.

Private Member Functions

 ~nsStorageStream ()
NS_METHOD Seek (PRInt32 aPosition)
PRUint32 SegNum (PRUint32 aPosition)
PRUint32 SegOffset (PRUint32 aPosition)

Private Attributes

nsSegmentedBuffermSegmentedBuffer
PRUint32 mSegmentSize
PRUint32 mSegmentSizeLog2
PRBool mWriteInProgress
PRInt32 mLastSegmentNum
char * mWriteCursor
char * mSegmentEnd
PRUint32 mLogicalLength

Detailed Description

Definition at line 67 of file nsStorageStream.h.


Constructor & Destructor Documentation

Definition at line 72 of file nsStorageStream.cpp.

    : mSegmentedBuffer(0), mSegmentSize(0), mWriteInProgress(PR_FALSE),
      mLastSegmentNum(-1), mWriteCursor(0), mSegmentEnd(0), mLogicalLength(0)
{
#if defined(PR_LOGGING)
    //
    // Initialize the global PRLogModule for socket transport logging 
    // if necessary...
    //
    if (nsnull == StorageStreamLog) {
        StorageStreamLog = PR_NewLogModule("StorageStreamLog");
    }
#endif /* PR_LOGGING */

  PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
         ("Creating nsStorageStream [%x].\n", this));
}

Here is the caller graph for this function:

Definition at line 90 of file nsStorageStream.cpp.


Member Function Documentation

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

Get a reference to the one and only output stream for this instance.

The zero-based startPosition argument is used is used to set the initial write cursor position. The startPosition cannot be set larger than the current buffer length. Calling this method has the side-effect of truncating the internal buffer to startPosition bytes.

void nsIStorageStream::init ( in PRUint32  segmentSize,
in PRUint32  maxSize,
in nsIMemory  segmentAllocator 
) [inherited]

Initialize the stream, setting up the amount of space that will be allocated for the stream's backing-store.

Parameters:
segmentSizeSize of each segment. Must be a power of two.
maxSizeMaximum total size of this stream. length will always be less than or equal to this value. Passing PR_UINT32_MAX is safe.
segmentAllocatorWhich allocator to use for the segments. May be null, in which case a default allocator will be used.
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.

Create a new input stream to read data (written by the singleton output stream) from the internal buffer.

Multiple, independent input streams can be created.

NS_METHOD nsStorageStream::Seek ( PRInt32  aPosition) [private]

Definition at line 305 of file nsStorageStream.cpp.

{
    NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED);
    
    // An argument of -1 means "seek to end of stream"
    if (aPosition == -1)
        aPosition = mLogicalLength;

    // Seeking beyond the buffer end is illegal
    if ((PRUint32)aPosition > mLogicalLength)
        return NS_ERROR_INVALID_ARG;

    // Seeking backwards in the write stream results in truncation
    SetLength(aPosition);

    // Special handling for seek to start-of-buffer
    if (aPosition == 0) {
        mWriteCursor = 0;
        mSegmentEnd = 0;
        PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
               ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n",
                this, mWriteCursor, mSegmentEnd));
        return NS_OK;
    }

    // Segment may have changed, so reset pointers
    mWriteCursor = mSegmentedBuffer->GetSegment(mLastSegmentNum);
    NS_ASSERTION(mWriteCursor, "null mWriteCursor");
    mSegmentEnd = mWriteCursor + mSegmentSize;

    // Adjust write cursor for current segment offset.  This test is necessary
    // because SegNum may reference the next-to-be-allocated segment, in which
    // case we need to be pointing at the end of the last segment.
    PRInt32 segmentOffset = SegOffset(aPosition);
    if (segmentOffset == 0 && (SegNum(aPosition) > (PRUint32) mLastSegmentNum))
        mWriteCursor = mSegmentEnd;
    else
        mWriteCursor += segmentOffset;
    
    PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
           ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n",
            this, mWriteCursor, mSegmentEnd));
    return NS_OK;
}

Here is the call graph for this function:

PRUint32 nsStorageStream::SegNum ( PRUint32  aPosition) [inline, private]

Definition at line 94 of file nsStorageStream.h.

{return aPosition >> mSegmentSizeLog2;}

Here is the caller graph for this function:

PRUint32 nsStorageStream::SegOffset ( PRUint32  aPosition) [inline, private]

Definition at line 95 of file nsStorageStream.h.

{return aPosition & (mSegmentSize - 1);}

Here is the caller graph for this function:

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

The length attribute indicates the total number of bytes stored in the nsIStorageStream internal buffer, regardless of any consumption by input streams.

Assigning to the length field can be used to truncate the buffer data, but can not be used when either the instance's output stream is in use.

writeInProgress

Definition at line 95 of file nsIStorageStream.idl.

Definition at line 87 of file nsStorageStream.h.

Definition at line 91 of file nsStorageStream.h.

Definition at line 82 of file nsStorageStream.h.

Definition at line 89 of file nsStorageStream.h.

Definition at line 83 of file nsStorageStream.h.

Definition at line 85 of file nsStorageStream.h.

Definition at line 88 of file nsStorageStream.h.

Definition at line 86 of file nsStorageStream.h.

NS_DECL_ISUPPORTS NS_DECL_NSISTORAGESTREAM friend class nsStorageStream::nsStorageInputStream

Definition at line 77 of file nsStorageStream.h.

True, when output stream has not yet been Close'ed.

Definition at line 100 of file nsIStorageStream.idl.


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