michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * The storage stream provides an internal buffer that can be filled by a michael@0: * client using a single output stream. One or more independent input streams michael@0: * can be created to read the data out non-destructively. The implementation michael@0: * uses a segmented buffer internally to avoid realloc'ing of large buffers, michael@0: * with the attendant performance loss and heap fragmentation. michael@0: */ michael@0: michael@0: #ifndef _nsStorageStream_h_ michael@0: #define _nsStorageStream_h_ michael@0: michael@0: #include "nsIStorageStream.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsMemory.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #define NS_STORAGESTREAM_CID \ michael@0: { /* 669a9795-6ff7-4ed4-9150-c34ce2971b63 */ \ michael@0: 0x669a9795, \ michael@0: 0x6ff7, \ michael@0: 0x4ed4, \ michael@0: {0x91, 0x50, 0xc3, 0x4c, 0xe2, 0x97, 0x1b, 0x63} \ michael@0: } michael@0: michael@0: #define NS_STORAGESTREAM_CONTRACTID "@mozilla.org/storagestream;1" michael@0: michael@0: class nsSegmentedBuffer; michael@0: michael@0: class nsStorageStream MOZ_FINAL : public nsIStorageStream, michael@0: public nsIOutputStream michael@0: { michael@0: public: michael@0: nsStorageStream(); michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSISTORAGESTREAM michael@0: NS_DECL_NSIOUTPUTSTREAM michael@0: michael@0: friend class nsStorageInputStream; michael@0: michael@0: private: michael@0: ~nsStorageStream(); michael@0: michael@0: nsSegmentedBuffer* mSegmentedBuffer; michael@0: uint32_t mSegmentSize; // All segments, except possibly the last, are of this size michael@0: // Must be power-of-2 michael@0: uint32_t mSegmentSizeLog2; // log2(mSegmentSize) michael@0: bool mWriteInProgress; // true, if an un-Close'ed output stream exists michael@0: int32_t mLastSegmentNum; // Last segment # in use, -1 initially michael@0: char* mWriteCursor; // Pointer to next byte to be written michael@0: char* mSegmentEnd; // Pointer to one byte after end of segment michael@0: // containing the write cursor michael@0: uint32_t mLogicalLength; // Number of bytes written to stream michael@0: michael@0: NS_METHOD Seek(int32_t aPosition); michael@0: uint32_t SegNum(uint32_t aPosition) {return aPosition >> mSegmentSizeLog2;} michael@0: uint32_t SegOffset(uint32_t aPosition) {return aPosition & (mSegmentSize - 1);} michael@0: }; michael@0: michael@0: #endif // _nsStorageStream_h_