Back to index

lightning-sunbird  0.9+nobinonly
nsStorageStream.h
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  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040  * The storage stream provides an internal buffer that can be filled by a
00041  * client using a single output stream.  One or more independent input streams
00042  * can be created to read the data out non-destructively.  The implementation
00043  * uses a segmented buffer internally to avoid realloc'ing of large buffers,
00044  * with the attendant performance loss and heap fragmentation.
00045  */
00046 
00047 #ifndef _nsStorageStream_h_
00048 #define _nsStorageStream_h_
00049 
00050 #include "nsIStorageStream.h"
00051 #include "nsIOutputStream.h"
00052 #include "nsMemory.h"
00053 
00054 #define NS_STORAGESTREAM_CID                       \
00055 { /* 669a9795-6ff7-4ed4-9150-c34ce2971b63 */       \
00056   0x669a9795,                                      \
00057   0x6ff7,                                          \
00058   0x4ed4,                                          \
00059   {0x91, 0x50, 0xc3, 0x4c, 0xe2, 0x97, 0x1b, 0x63} \
00060 }
00061 
00062 #define NS_STORAGESTREAM_CONTRACTID "@mozilla.org/storagestream;1"
00063 #define NS_STORAGESTREAM_CLASSNAME "Storage Stream"
00064 
00065 class nsSegmentedBuffer;
00066 
00067 class nsStorageStream : public nsIStorageStream,
00068                         public nsIOutputStream
00069 {
00070 public:
00071     nsStorageStream();
00072     
00073     NS_DECL_ISUPPORTS
00074     NS_DECL_NSISTORAGESTREAM
00075     NS_DECL_NSIOUTPUTSTREAM
00076 
00077     friend class nsStorageInputStream;
00078 
00079 private:
00080     ~nsStorageStream();
00081 
00082     nsSegmentedBuffer* mSegmentedBuffer;
00083     PRUint32           mSegmentSize;       // All segments, except possibly the last, are of this size
00084                                            //   Must be power-of-2
00085     PRUint32           mSegmentSizeLog2;   // log2(mSegmentSize)
00086     PRBool             mWriteInProgress;   // true, if an un-Close'ed output stream exists
00087     PRInt32            mLastSegmentNum;    // Last segment # in use, -1 initially
00088     char*              mWriteCursor;       // Pointer to next byte to be written
00089     char*              mSegmentEnd;        // Pointer to one byte after end of segment
00090                                            //   containing the write cursor
00091     PRUint32           mLogicalLength;     // Number of bytes written to stream
00092 
00093     NS_METHOD Seek(PRInt32 aPosition);
00094     PRUint32 SegNum(PRUint32 aPosition)    {return aPosition >> mSegmentSizeLog2;}
00095     PRUint32 SegOffset(PRUint32 aPosition) {return aPosition & (mSegmentSize - 1);}
00096 };
00097 
00098 #endif //  _nsStorageStream_h_