1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
* The contents of this file are subject to the Netscape Public
3
* License Version 1.1 (the "License"); you may not use this file
4
* except in compliance with the License. You may obtain a copy of
5
* the License at http://www.mozilla.org/NPL/
7
* Software distributed under the License is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the License for the specific language governing
10
* rights and limitations under the License.
12
* The Original Code is Mozilla Communicator client code, released
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
23
* The storage stream provides an internal buffer that can be filled by a
24
* client using a single output stream. One or more independent input streams
25
* can be created to read the data out non-destructively. The implementation
26
* uses a segmented buffer internally to avoid realloc'ing of large buffers,
27
* with the attendant performance loss and heap fragmentation.
30
#ifndef _nsStorageStream_h_
31
#define _nsStorageStream_h_
33
#include "nsIStorageStream.h"
34
#include "nsIOutputStream.h"
37
#define NS_STORAGESTREAM_CID \
38
{ /* 669a9795-6ff7-4ed4-9150-c34ce2971b63 */ \
42
{0x91, 0x50, 0xc3, 0x4c, 0xe2, 0x97, 0x1b, 0x63} \
45
#define NS_STORAGESTREAM_CONTRACTID "@mozilla.org/storagestream;1"
46
#define NS_STORAGESTREAM_CLASSNAME "Storage Stream"
48
class nsSegmentedBuffer;
50
class nsStorageStream : public nsIStorageStream,
51
public nsIOutputStream
56
NS_METHOD Init(PRUint32 segmentSize, PRUint32 maxSize, nsIMemory *segmentAllocator = 0);
59
NS_DECL_NSISTORAGESTREAM
60
NS_DECL_NSIOUTPUTSTREAM
62
friend class nsStorageInputStream;
67
nsSegmentedBuffer* mSegmentedBuffer;
68
PRUint32 mSegmentSize; // All segments, except possibly the last, are of this size
70
PRUint32 mSegmentSizeLog2; // log2(mSegmentSize)
71
PRBool mWriteInProgress; // true, if an un-Close'ed output stream exists
72
PRInt32 mLastSegmentNum; // Last segment # in use, -1 initially
73
char* mWriteCursor; // Pointer to next byte to be written
74
char* mSegmentEnd; // Pointer to one byte after end of segment
75
// containing the write cursor
76
PRUint32 mLogicalLength; // Number of bytes written to stream
78
NS_METHOD Seek(PRInt32 aPosition);
79
PRUint32 SegNum(PRUint32 aPosition) {return aPosition >> mSegmentSizeLog2;}
80
PRUint32 SegOffset(PRUint32 aPosition) {return aPosition & (mSegmentSize - 1);}
83
#endif // _nsStorageStream_h_