Back to index

lightning-sunbird  0.9+nobinonly
nsStorageStream.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Pierre Phaneuf <pp@ludusdesign.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041  * The storage stream provides an internal buffer that can be filled by a
00042  * client using a single output stream.  One or more independent input streams
00043  * can be created to read the data out non-destructively.  The implementation
00044  * uses a segmented buffer internally to avoid realloc'ing of large buffers,
00045  * with the attendant performance loss and heap fragmentation.
00046  */
00047 
00048 #include "nsStorageStream.h"
00049 #include "nsSegmentedBuffer.h"
00050 #include "nsCOMPtr.h"
00051 #include "prbit.h"
00052 #include "nsIInputStream.h"
00053 #include "nsISeekableStream.h"
00054 #include "prlog.h"
00055 #include "nsInt64.h"
00056 #if defined(PR_LOGGING)
00057 //
00058 // Log module for StorageStream logging...
00059 //
00060 // To enable logging (see prlog.h for full details):
00061 //
00062 //    set NSPR_LOG_MODULES=StorageStreamLog:5
00063 //    set NSPR_LOG_FILE=nspr.log
00064 //
00065 // this enables PR_LOG_DEBUG level information and places all output in
00066 // the file nspr.log
00067 //
00068 PRLogModuleInfo* StorageStreamLog = nsnull;
00069 
00070 #endif /* PR_LOGGING */
00071 
00072 nsStorageStream::nsStorageStream()
00073     : mSegmentedBuffer(0), mSegmentSize(0), mWriteInProgress(PR_FALSE),
00074       mLastSegmentNum(-1), mWriteCursor(0), mSegmentEnd(0), mLogicalLength(0)
00075 {
00076 #if defined(PR_LOGGING)
00077     //
00078     // Initialize the global PRLogModule for socket transport logging 
00079     // if necessary...
00080     //
00081     if (nsnull == StorageStreamLog) {
00082         StorageStreamLog = PR_NewLogModule("StorageStreamLog");
00083     }
00084 #endif /* PR_LOGGING */
00085 
00086   PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00087          ("Creating nsStorageStream [%x].\n", this));
00088 }
00089 
00090 nsStorageStream::~nsStorageStream()
00091 {
00092        if (mSegmentedBuffer)
00093            delete mSegmentedBuffer;
00094 }
00095 
00096 NS_IMPL_THREADSAFE_ISUPPORTS2(nsStorageStream,
00097                               nsIStorageStream,
00098                               nsIOutputStream)
00099 
00100 NS_IMETHODIMP
00101 nsStorageStream::Init(PRUint32 segmentSize, PRUint32 maxSize,
00102                       nsIMemory *segmentAllocator)
00103 {
00104     mSegmentedBuffer = new nsSegmentedBuffer();
00105     if (!mSegmentedBuffer)
00106         return NS_ERROR_OUT_OF_MEMORY;
00107     
00108     mSegmentSize = segmentSize;
00109     mSegmentSizeLog2 = PR_FloorLog2(segmentSize);
00110 
00111     // Segment size must be a power of two
00112     if (mSegmentSize != ((PRUint32)1 << mSegmentSizeLog2))
00113         return NS_ERROR_INVALID_ARG;
00114 
00115     return mSegmentedBuffer->Init(segmentSize, maxSize, segmentAllocator);
00116 }
00117 
00118 NS_IMETHODIMP
00119 nsStorageStream::GetOutputStream(PRInt32 aStartingOffset, 
00120                                  nsIOutputStream * *aOutputStream)
00121 {
00122     NS_ENSURE_ARG(aOutputStream);
00123     NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED);
00124     
00125     if (mWriteInProgress)
00126         return NS_ERROR_NOT_AVAILABLE;
00127 
00128     nsresult rv = Seek(aStartingOffset);
00129     if (NS_FAILED(rv)) return rv;
00130 
00131     // Enlarge the last segment in the buffer so that it is the same size as
00132     // all the other segments in the buffer.  (It may have been realloc'ed
00133     // smaller in the Close() method.)
00134     if (mLastSegmentNum >= 0)
00135         mSegmentedBuffer->ReallocLastSegment(mSegmentSize);
00136 
00137     // Need to re-Seek, since realloc might have changed segment base pointer
00138     rv = Seek(aStartingOffset);
00139     if (NS_FAILED(rv)) return rv;
00140 
00141     NS_ADDREF(this);
00142     *aOutputStream = NS_STATIC_CAST(nsIOutputStream*, this);
00143     mWriteInProgress = PR_TRUE;
00144     return NS_OK;
00145 }
00146 
00147 NS_IMETHODIMP
00148 nsStorageStream::Close()
00149 {
00150     NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED);
00151     
00152     mWriteInProgress = PR_FALSE;
00153     
00154     PRInt32 segmentOffset = SegOffset(mLogicalLength);
00155 
00156     // Shrink the final segment in the segmented buffer to the minimum size
00157     // needed to contain the data, so as to conserve memory.
00158     if (segmentOffset)
00159         mSegmentedBuffer->ReallocLastSegment(segmentOffset);
00160     
00161     mWriteCursor = 0;
00162     mSegmentEnd = 0;
00163 
00164     PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00165            ("nsStorageStream [%x] Close mWriteCursor=%x mSegmentEnd=%x\n",
00166             this, mWriteCursor, mSegmentEnd));
00167 
00168     return NS_OK;
00169 }
00170 
00171 NS_IMETHODIMP
00172 nsStorageStream::Flush()
00173 {
00174     return NS_OK;
00175 }
00176 
00177 NS_IMETHODIMP
00178 nsStorageStream::Write(const char *aBuffer, PRUint32 aCount, PRUint32 *aNumWritten)
00179 {
00180     NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED);
00181     
00182     const char* readCursor;
00183     PRUint32 count, availableInSegment, remaining;
00184     nsresult rv = NS_OK;
00185 
00186     NS_ENSURE_ARG_POINTER(aNumWritten);
00187     NS_ENSURE_ARG(aBuffer);
00188 
00189     PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00190            ("nsStorageStream [%x] Write mWriteCursor=%x mSegmentEnd=%x aCount=%d\n",
00191             this, mWriteCursor, mSegmentEnd, aCount));
00192 
00193     remaining = aCount;
00194     readCursor = aBuffer;
00195     // If no segments have been created yet, create one even if we don't have
00196     // to write any data; this enables creating an input stream which reads from
00197     // the very end of the data for any amount of data in the stream (i.e.
00198     // this stream contains N bytes of data and newInputStream(N) is called),
00199     // even for N=0 (with the caveat that we require .write("", 0) be called to
00200     // initialize internal buffers).
00201     PRBool firstTime = mSegmentedBuffer->GetSegmentCount() == 0;
00202     while (remaining || NS_UNLIKELY(firstTime)) {
00203         firstTime = PR_FALSE;
00204         availableInSegment = mSegmentEnd - mWriteCursor;
00205         if (!availableInSegment) {
00206             mWriteCursor = mSegmentedBuffer->AppendNewSegment();
00207             if (!mWriteCursor) {
00208                 mSegmentEnd = 0;
00209                 rv = NS_ERROR_OUT_OF_MEMORY;
00210                 goto out;
00211             }
00212             mLastSegmentNum++;
00213             mSegmentEnd = mWriteCursor + mSegmentSize;
00214             availableInSegment = mSegmentEnd - mWriteCursor;
00215             PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00216                    ("nsStorageStream [%x] Write (new seg) mWriteCursor=%x mSegmentEnd=%x\n",
00217                     this, mWriteCursor, mSegmentEnd));
00218         }
00219        
00220         count = PR_MIN(availableInSegment, remaining);
00221         memcpy(mWriteCursor, readCursor, count);
00222         remaining -= count;
00223         readCursor += count;
00224         mWriteCursor += count;
00225         PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00226                ("nsStorageStream [%x] Writing mWriteCursor=%x mSegmentEnd=%x count=%d\n",
00227                 this, mWriteCursor, mSegmentEnd, count));
00228     };
00229 
00230  out:
00231     *aNumWritten = aCount - remaining;
00232     mLogicalLength += *aNumWritten;
00233 
00234     PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00235            ("nsStorageStream [%x] Wrote mWriteCursor=%x mSegmentEnd=%x numWritten=%d\n",
00236             this, mWriteCursor, mSegmentEnd, *aNumWritten));
00237     return rv;
00238 }
00239 
00240 NS_IMETHODIMP 
00241 nsStorageStream::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
00242 {
00243     NS_NOTREACHED("WriteFrom");
00244     return NS_ERROR_NOT_IMPLEMENTED;
00245 }
00246 
00247 NS_IMETHODIMP 
00248 nsStorageStream::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
00249 {
00250     NS_NOTREACHED("WriteSegments");
00251     return NS_ERROR_NOT_IMPLEMENTED;
00252 }
00253 
00254 NS_IMETHODIMP 
00255 nsStorageStream::IsNonBlocking(PRBool *aNonBlocking)
00256 {
00257     *aNonBlocking = PR_TRUE;
00258     return NS_OK;
00259 }
00260 
00261 NS_IMETHODIMP
00262 nsStorageStream::GetLength(PRUint32 *aLength)
00263 {
00264     NS_ENSURE_ARG(aLength);
00265     *aLength = mLogicalLength;
00266     return NS_OK;
00267 }
00268 
00269 // Truncate the buffer by deleting the end segments
00270 NS_IMETHODIMP
00271 nsStorageStream::SetLength(PRUint32 aLength)
00272 {
00273     NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED);
00274     
00275     if (mWriteInProgress)
00276         return NS_ERROR_NOT_AVAILABLE;
00277 
00278     if (aLength > mLogicalLength)
00279         return NS_ERROR_INVALID_ARG;
00280 
00281     PRInt32 newLastSegmentNum = SegNum(aLength);
00282     PRInt32 segmentOffset = SegOffset(aLength);
00283     if (segmentOffset == 0)
00284         newLastSegmentNum--;
00285 
00286     while (newLastSegmentNum < mLastSegmentNum) {
00287         mSegmentedBuffer->DeleteLastSegment();
00288         mLastSegmentNum--;
00289     }
00290 
00291     mLogicalLength = aLength;
00292     return NS_OK;
00293 }
00294 
00295 NS_IMETHODIMP
00296 nsStorageStream::GetWriteInProgress(PRBool *aWriteInProgress)
00297 {
00298     NS_ENSURE_ARG(aWriteInProgress);
00299 
00300     *aWriteInProgress = mWriteInProgress;
00301     return NS_OK;
00302 }
00303 
00304 NS_METHOD
00305 nsStorageStream::Seek(PRInt32 aPosition)
00306 {
00307     NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED);
00308     
00309     // An argument of -1 means "seek to end of stream"
00310     if (aPosition == -1)
00311         aPosition = mLogicalLength;
00312 
00313     // Seeking beyond the buffer end is illegal
00314     if ((PRUint32)aPosition > mLogicalLength)
00315         return NS_ERROR_INVALID_ARG;
00316 
00317     // Seeking backwards in the write stream results in truncation
00318     SetLength(aPosition);
00319 
00320     // Special handling for seek to start-of-buffer
00321     if (aPosition == 0) {
00322         mWriteCursor = 0;
00323         mSegmentEnd = 0;
00324         PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00325                ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n",
00326                 this, mWriteCursor, mSegmentEnd));
00327         return NS_OK;
00328     }
00329 
00330     // Segment may have changed, so reset pointers
00331     mWriteCursor = mSegmentedBuffer->GetSegment(mLastSegmentNum);
00332     NS_ASSERTION(mWriteCursor, "null mWriteCursor");
00333     mSegmentEnd = mWriteCursor + mSegmentSize;
00334 
00335     // Adjust write cursor for current segment offset.  This test is necessary
00336     // because SegNum may reference the next-to-be-allocated segment, in which
00337     // case we need to be pointing at the end of the last segment.
00338     PRInt32 segmentOffset = SegOffset(aPosition);
00339     if (segmentOffset == 0 && (SegNum(aPosition) > (PRUint32) mLastSegmentNum))
00340         mWriteCursor = mSegmentEnd;
00341     else
00342         mWriteCursor += segmentOffset;
00343     
00344     PR_LOG(StorageStreamLog, PR_LOG_DEBUG, 
00345            ("nsStorageStream [%x] Seek mWriteCursor=%x mSegmentEnd=%x\n",
00346             this, mWriteCursor, mSegmentEnd));
00347     return NS_OK;
00348 }
00349 
00351 
00352 // There can be many nsStorageInputStreams for a single nsStorageStream
00353 class nsStorageInputStream : public nsIInputStream
00354                            , public nsISeekableStream
00355 {
00356 public:
00357     nsStorageInputStream(nsStorageStream *aStorageStream, PRUint32 aSegmentSize)
00358         : mStorageStream(aStorageStream), mReadCursor(0),
00359           mSegmentEnd(0), mSegmentNum(0),
00360           mSegmentSize(aSegmentSize), mLogicalCursor(0)
00361        {
00362         NS_ADDREF(mStorageStream);
00363        }
00364 
00365     NS_DECL_ISUPPORTS
00366     NS_DECL_NSIINPUTSTREAM
00367     NS_DECL_NSISEEKABLESTREAM
00368 
00369 private:
00370     ~nsStorageInputStream()
00371     {
00372         NS_IF_RELEASE(mStorageStream);
00373     }
00374 
00375 protected:
00376     NS_METHOD Seek(PRUint32 aPosition);
00377 
00378     friend class nsStorageStream;
00379 
00380 private:
00381     nsStorageStream* mStorageStream;
00382     const char*      mReadCursor;    // Next memory location to read byte, or NULL
00383     const char*      mSegmentEnd;    // One byte past end of current buffer segment
00384     PRUint32         mSegmentNum;    // Segment number containing read cursor
00385     PRUint32         mSegmentSize;   // All segments, except the last, are of this size
00386     PRUint32         mLogicalCursor; // Logical offset into stream
00387 
00388     PRUint32 SegNum(PRUint32 aPosition)    {return aPosition >> mStorageStream->mSegmentSizeLog2;}
00389     PRUint32 SegOffset(PRUint32 aPosition) {return aPosition & (mSegmentSize - 1);}
00390 };
00391 
00392 NS_IMPL_THREADSAFE_ISUPPORTS2(nsStorageInputStream,
00393                               nsIInputStream,
00394                               nsISeekableStream)
00395 
00396 NS_IMETHODIMP
00397 nsStorageStream::NewInputStream(PRInt32 aStartingOffset, nsIInputStream* *aInputStream)
00398 {
00399     NS_ENSURE_TRUE(mSegmentedBuffer, NS_ERROR_NOT_INITIALIZED);
00400     
00401     nsStorageInputStream *inputStream = new nsStorageInputStream(this, mSegmentSize);
00402     if (!inputStream)
00403         return NS_ERROR_OUT_OF_MEMORY;
00404 
00405     NS_ADDREF(inputStream);
00406 
00407     nsresult rv = inputStream->Seek(aStartingOffset);
00408     if (NS_FAILED(rv)) {
00409         NS_RELEASE(inputStream);
00410         return rv;
00411     }
00412 
00413     *aInputStream = inputStream;
00414     return NS_OK;
00415 }
00416 
00417 NS_IMETHODIMP
00418 nsStorageInputStream::Close()
00419 {
00420     return NS_OK;
00421 }
00422 
00423 NS_IMETHODIMP
00424 nsStorageInputStream::Available(PRUint32 *aAvailable)
00425 {
00426     *aAvailable = mStorageStream->mLogicalLength - mLogicalCursor;
00427     return NS_OK;
00428 }
00429 
00430 NS_IMETHODIMP
00431 nsStorageInputStream::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
00432 {
00433     char* writeCursor;
00434     PRUint32 count, availableInSegment, remainingCapacity;
00435 
00436     remainingCapacity = aCount;
00437     writeCursor = aBuffer;
00438     while (remainingCapacity) {
00439         availableInSegment = mSegmentEnd - mReadCursor;
00440         if (!availableInSegment) {
00441             PRUint32 available = mStorageStream->mLogicalLength - mLogicalCursor;
00442             if (!available)
00443                 goto out;
00444            
00445             mReadCursor = mStorageStream->mSegmentedBuffer->GetSegment(++mSegmentNum);
00446             mSegmentEnd = mReadCursor + PR_MIN(mSegmentSize, available);
00447         }
00448        
00449         count = PR_MIN(availableInSegment, remainingCapacity);
00450         memcpy(writeCursor, mReadCursor, count);
00451         remainingCapacity -= count;
00452         mReadCursor += count;
00453         writeCursor += count;
00454         mLogicalCursor += count;
00455     };
00456 
00457  out:
00458     *aNumRead = aCount - remainingCapacity;
00459 
00460     PRBool isWriteInProgress = PR_FALSE;
00461     if (NS_FAILED(mStorageStream->GetWriteInProgress(&isWriteInProgress)))
00462         isWriteInProgress = PR_FALSE;
00463 
00464     if (*aNumRead == 0 && isWriteInProgress)
00465         return NS_BASE_STREAM_WOULD_BLOCK;
00466     else
00467         return NS_OK;
00468 }
00469 
00470 NS_IMETHODIMP 
00471 nsStorageInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 aCount, PRUint32 *aNumRead)
00472 {
00473     PRUint32 count, availableInSegment, remainingCapacity, bytesConsumed;
00474     nsresult rv;
00475 
00476     remainingCapacity = aCount;
00477     while (remainingCapacity) {
00478         availableInSegment = mSegmentEnd - mReadCursor;
00479         if (!availableInSegment) {
00480             PRUint32 available = mStorageStream->mLogicalLength - mLogicalCursor;
00481             if (!available)
00482                 goto out;
00483 
00484             mReadCursor = mStorageStream->mSegmentedBuffer->GetSegment(++mSegmentNum);
00485             mSegmentEnd = mReadCursor + PR_MIN(mSegmentSize, available);
00486             availableInSegment = mSegmentEnd - mReadCursor;
00487         }
00488        
00489         count = PR_MIN(availableInSegment, remainingCapacity);
00490         rv = writer(this, closure, mReadCursor, mLogicalCursor, count, &bytesConsumed);
00491         if (NS_FAILED(rv) || (bytesConsumed == 0))
00492           break;
00493         remainingCapacity -= bytesConsumed;
00494         mReadCursor += bytesConsumed;
00495         mLogicalCursor += bytesConsumed;
00496     };
00497 
00498  out:
00499     *aNumRead = aCount - remainingCapacity;
00500 
00501     PRBool isWriteInProgress = PR_FALSE;
00502     if (NS_FAILED(mStorageStream->GetWriteInProgress(&isWriteInProgress)))
00503         isWriteInProgress = PR_FALSE;
00504 
00505     if (*aNumRead == 0 && isWriteInProgress)
00506       return NS_BASE_STREAM_WOULD_BLOCK;
00507     else
00508       return NS_OK;
00509 }
00510 
00511 NS_IMETHODIMP 
00512 nsStorageInputStream::IsNonBlocking(PRBool *aNonBlocking)
00513 {
00514     *aNonBlocking = PR_TRUE;
00515     return NS_OK;
00516 }
00517 
00518 NS_IMETHODIMP
00519 nsStorageInputStream::Seek(PRInt32 aWhence, PRInt64 aOffset)
00520 {
00521     nsInt64 pos = aOffset;
00522 
00523     switch (aWhence) {
00524     case NS_SEEK_SET:
00525         break;
00526     case NS_SEEK_CUR:
00527         pos += mLogicalCursor;
00528         break;
00529     case NS_SEEK_END:
00530         pos += mStorageStream->mLogicalLength;
00531         break;
00532     default:
00533         NS_NOTREACHED("unexpected whence value");
00534         return NS_ERROR_UNEXPECTED;
00535     }
00536     nsInt64 logicalCursor(mLogicalCursor);
00537     if (pos == logicalCursor)
00538         return NS_OK;
00539 
00540     return Seek(pos);
00541 }
00542 
00543 NS_IMETHODIMP
00544 nsStorageInputStream::Tell(PRInt64 *aResult)
00545 {
00546     LL_UI2L(*aResult, mLogicalCursor);
00547     return NS_OK;
00548 }
00549 
00550 NS_IMETHODIMP
00551 nsStorageInputStream::SetEOF()
00552 {
00553     NS_NOTREACHED("nsStorageInputStream::SetEOF");
00554     return NS_ERROR_NOT_IMPLEMENTED;
00555 }
00556 
00557 NS_METHOD
00558 nsStorageInputStream::Seek(PRUint32 aPosition)
00559 {
00560     PRUint32 length = mStorageStream->mLogicalLength;
00561     if (aPosition > length)
00562         return NS_ERROR_INVALID_ARG;
00563 
00564     if (length == 0)
00565         return NS_OK;
00566 
00567     mSegmentNum = SegNum(aPosition);
00568     PRUint32 segmentOffset = SegOffset(aPosition);
00569     mReadCursor = mStorageStream->mSegmentedBuffer->GetSegment(mSegmentNum) +
00570         segmentOffset;
00571     PRUint32 available = length - aPosition;
00572     mSegmentEnd = mReadCursor + PR_MIN(mSegmentSize - segmentOffset, available);
00573     mLogicalCursor = aPosition;
00574     return NS_OK;
00575 }
00576 
00577 NS_COM nsresult
00578 NS_NewStorageStream(PRUint32 segmentSize, PRUint32 maxSize, nsIStorageStream **result)
00579 {
00580     NS_ENSURE_ARG(result);
00581 
00582     nsStorageStream* storageStream = new nsStorageStream();
00583     if (!storageStream) return NS_ERROR_OUT_OF_MEMORY;
00584     
00585     NS_ADDREF(storageStream);
00586     nsresult rv = storageStream->Init(segmentSize, maxSize, nsnull);
00587     if (NS_FAILED(rv)) {
00588         NS_RELEASE(storageStream);
00589         return rv;
00590     }
00591     *result = storageStream;
00592     return NS_OK;
00593 }