1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/io/nsStorageStream.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * The storage stream provides an internal buffer that can be filled by a 1.11 + * client using a single output stream. One or more independent input streams 1.12 + * can be created to read the data out non-destructively. The implementation 1.13 + * uses a segmented buffer internally to avoid realloc'ing of large buffers, 1.14 + * with the attendant performance loss and heap fragmentation. 1.15 + */ 1.16 + 1.17 +#ifndef _nsStorageStream_h_ 1.18 +#define _nsStorageStream_h_ 1.19 + 1.20 +#include "nsIStorageStream.h" 1.21 +#include "nsIOutputStream.h" 1.22 +#include "nsMemory.h" 1.23 +#include "mozilla/Attributes.h" 1.24 + 1.25 +#define NS_STORAGESTREAM_CID \ 1.26 +{ /* 669a9795-6ff7-4ed4-9150-c34ce2971b63 */ \ 1.27 + 0x669a9795, \ 1.28 + 0x6ff7, \ 1.29 + 0x4ed4, \ 1.30 + {0x91, 0x50, 0xc3, 0x4c, 0xe2, 0x97, 0x1b, 0x63} \ 1.31 +} 1.32 + 1.33 +#define NS_STORAGESTREAM_CONTRACTID "@mozilla.org/storagestream;1" 1.34 + 1.35 +class nsSegmentedBuffer; 1.36 + 1.37 +class nsStorageStream MOZ_FINAL : public nsIStorageStream, 1.38 + public nsIOutputStream 1.39 +{ 1.40 +public: 1.41 + nsStorageStream(); 1.42 + 1.43 + NS_DECL_THREADSAFE_ISUPPORTS 1.44 + NS_DECL_NSISTORAGESTREAM 1.45 + NS_DECL_NSIOUTPUTSTREAM 1.46 + 1.47 + friend class nsStorageInputStream; 1.48 + 1.49 +private: 1.50 + ~nsStorageStream(); 1.51 + 1.52 + nsSegmentedBuffer* mSegmentedBuffer; 1.53 + uint32_t mSegmentSize; // All segments, except possibly the last, are of this size 1.54 + // Must be power-of-2 1.55 + uint32_t mSegmentSizeLog2; // log2(mSegmentSize) 1.56 + bool mWriteInProgress; // true, if an un-Close'ed output stream exists 1.57 + int32_t mLastSegmentNum; // Last segment # in use, -1 initially 1.58 + char* mWriteCursor; // Pointer to next byte to be written 1.59 + char* mSegmentEnd; // Pointer to one byte after end of segment 1.60 + // containing the write cursor 1.61 + uint32_t mLogicalLength; // Number of bytes written to stream 1.62 + 1.63 + NS_METHOD Seek(int32_t aPosition); 1.64 + uint32_t SegNum(uint32_t aPosition) {return aPosition >> mSegmentSizeLog2;} 1.65 + uint32_t SegOffset(uint32_t aPosition) {return aPosition & (mSegmentSize - 1);} 1.66 +}; 1.67 + 1.68 +#endif // _nsStorageStream_h_