|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsBufferedStreams_h__ |
|
7 #define nsBufferedStreams_h__ |
|
8 |
|
9 #include "nsIBufferedStreams.h" |
|
10 #include "nsIInputStream.h" |
|
11 #include "nsIOutputStream.h" |
|
12 #include "nsISafeOutputStream.h" |
|
13 #include "nsISeekableStream.h" |
|
14 #include "nsIStreamBufferAccess.h" |
|
15 #include "nsCOMPtr.h" |
|
16 #include "nsIIPCSerializableInputStream.h" |
|
17 |
|
18 //////////////////////////////////////////////////////////////////////////////// |
|
19 |
|
20 class nsBufferedStream : public nsISeekableStream |
|
21 { |
|
22 public: |
|
23 NS_DECL_THREADSAFE_ISUPPORTS |
|
24 NS_DECL_NSISEEKABLESTREAM |
|
25 |
|
26 nsBufferedStream(); |
|
27 virtual ~nsBufferedStream(); |
|
28 |
|
29 nsresult Close(); |
|
30 |
|
31 protected: |
|
32 nsresult Init(nsISupports* stream, uint32_t bufferSize); |
|
33 NS_IMETHOD Fill() = 0; |
|
34 NS_IMETHOD Flush() = 0; |
|
35 |
|
36 uint32_t mBufferSize; |
|
37 char* mBuffer; |
|
38 |
|
39 // mBufferStartOffset is the offset relative to the start of mStream. |
|
40 int64_t mBufferStartOffset; |
|
41 |
|
42 // mCursor is the read cursor for input streams, or write cursor for |
|
43 // output streams, and is relative to mBufferStartOffset. |
|
44 uint32_t mCursor; |
|
45 |
|
46 // mFillPoint is the amount available in the buffer for input streams, |
|
47 // or the high watermark of bytes written into the buffer, and therefore |
|
48 // is relative to mBufferStartOffset. |
|
49 uint32_t mFillPoint; |
|
50 |
|
51 nsISupports* mStream; // cast to appropriate subclass |
|
52 |
|
53 bool mBufferDisabled; |
|
54 bool mEOF; // True if mStream is at EOF |
|
55 uint8_t mGetBufferCount; |
|
56 }; |
|
57 |
|
58 //////////////////////////////////////////////////////////////////////////////// |
|
59 |
|
60 class nsBufferedInputStream : public nsBufferedStream, |
|
61 public nsIBufferedInputStream, |
|
62 public nsIStreamBufferAccess, |
|
63 public nsIIPCSerializableInputStream |
|
64 { |
|
65 public: |
|
66 NS_DECL_ISUPPORTS_INHERITED |
|
67 NS_DECL_NSIINPUTSTREAM |
|
68 NS_DECL_NSIBUFFEREDINPUTSTREAM |
|
69 NS_DECL_NSISTREAMBUFFERACCESS |
|
70 NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM |
|
71 |
|
72 nsBufferedInputStream() : nsBufferedStream() {} |
|
73 virtual ~nsBufferedInputStream() {} |
|
74 |
|
75 static nsresult |
|
76 Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); |
|
77 |
|
78 nsIInputStream* Source() { |
|
79 return (nsIInputStream*)mStream; |
|
80 } |
|
81 |
|
82 protected: |
|
83 NS_IMETHOD Fill(); |
|
84 NS_IMETHOD Flush() { return NS_OK; } // no-op for input streams |
|
85 }; |
|
86 |
|
87 //////////////////////////////////////////////////////////////////////////////// |
|
88 |
|
89 class nsBufferedOutputStream : public nsBufferedStream, |
|
90 public nsISafeOutputStream, |
|
91 public nsIBufferedOutputStream, |
|
92 public nsIStreamBufferAccess |
|
93 { |
|
94 public: |
|
95 NS_DECL_ISUPPORTS_INHERITED |
|
96 NS_DECL_NSIOUTPUTSTREAM |
|
97 NS_DECL_NSISAFEOUTPUTSTREAM |
|
98 NS_DECL_NSIBUFFEREDOUTPUTSTREAM |
|
99 NS_DECL_NSISTREAMBUFFERACCESS |
|
100 |
|
101 nsBufferedOutputStream() : nsBufferedStream() {} |
|
102 virtual ~nsBufferedOutputStream() { nsBufferedOutputStream::Close(); } |
|
103 |
|
104 static nsresult |
|
105 Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); |
|
106 |
|
107 nsIOutputStream* Sink() { |
|
108 return (nsIOutputStream*)mStream; |
|
109 } |
|
110 |
|
111 protected: |
|
112 NS_IMETHOD Fill() { return NS_OK; } // no-op for input streams |
|
113 |
|
114 nsCOMPtr<nsISafeOutputStream> mSafeStream; // QI'd from mStream |
|
115 }; |
|
116 |
|
117 //////////////////////////////////////////////////////////////////////////////// |
|
118 |
|
119 #endif // nsBufferedStreams_h__ |