Line data Source code
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 : #include "nsIAsyncInputStream.h"
18 :
19 : ////////////////////////////////////////////////////////////////////////////////
20 :
21 : class nsBufferedStream : public nsISeekableStream
22 : {
23 : public:
24 : NS_DECL_THREADSAFE_ISUPPORTS
25 : NS_DECL_NSISEEKABLESTREAM
26 :
27 : nsBufferedStream();
28 :
29 : nsresult Close();
30 :
31 : protected:
32 : virtual ~nsBufferedStream();
33 :
34 : nsresult Init(nsISupports* stream, uint32_t bufferSize);
35 : nsresult GetData(nsISupports **aResult);
36 : NS_IMETHOD Fill() = 0;
37 : NS_IMETHOD Flush() = 0;
38 :
39 : uint32_t mBufferSize;
40 : char* mBuffer;
41 :
42 : // mBufferStartOffset is the offset relative to the start of mStream.
43 : int64_t mBufferStartOffset;
44 :
45 : // mCursor is the read cursor for input streams, or write cursor for
46 : // output streams, and is relative to mBufferStartOffset.
47 : uint32_t mCursor;
48 :
49 : // mFillPoint is the amount available in the buffer for input streams,
50 : // or the high watermark of bytes written into the buffer, and therefore
51 : // is relative to mBufferStartOffset.
52 : uint32_t mFillPoint;
53 :
54 : nsISupports* mStream; // cast to appropriate subclass
55 :
56 : bool mBufferDisabled;
57 : bool mEOF; // True if mStream is at EOF
58 : uint8_t mGetBufferCount;
59 : };
60 :
61 : ////////////////////////////////////////////////////////////////////////////////
62 :
63 : class nsBufferedInputStream : public nsBufferedStream,
64 : public nsIBufferedInputStream,
65 : public nsIStreamBufferAccess,
66 : public nsIIPCSerializableInputStream,
67 : public nsIAsyncInputStream,
68 : public nsIInputStreamCallback
69 : {
70 : public:
71 : NS_DECL_ISUPPORTS_INHERITED
72 : NS_DECL_NSIINPUTSTREAM
73 : NS_DECL_NSIBUFFEREDINPUTSTREAM
74 : NS_DECL_NSISTREAMBUFFERACCESS
75 : NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
76 : NS_DECL_NSIASYNCINPUTSTREAM
77 : NS_DECL_NSIINPUTSTREAMCALLBACK
78 :
79 101 : nsBufferedInputStream() : nsBufferedStream() {}
80 :
81 : static nsresult
82 : Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
83 :
84 207 : nsIInputStream* Source() {
85 207 : return (nsIInputStream*)mStream;
86 : }
87 :
88 : protected:
89 303 : virtual ~nsBufferedInputStream() {}
90 :
91 : bool IsIPCSerializable() const;
92 : bool IsAsyncInputStream() const;
93 :
94 : NS_IMETHOD Fill() override;
95 0 : NS_IMETHOD Flush() override { return NS_OK; } // no-op for input streams
96 :
97 : nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
98 : };
99 :
100 : ////////////////////////////////////////////////////////////////////////////////
101 :
102 : class nsBufferedOutputStream : public nsBufferedStream,
103 : public nsISafeOutputStream,
104 : public nsIBufferedOutputStream,
105 : public nsIStreamBufferAccess
106 : {
107 : public:
108 : NS_DECL_ISUPPORTS_INHERITED
109 : NS_DECL_NSIOUTPUTSTREAM
110 : NS_DECL_NSISAFEOUTPUTSTREAM
111 : NS_DECL_NSIBUFFEREDOUTPUTSTREAM
112 : NS_DECL_NSISTREAMBUFFERACCESS
113 :
114 12 : nsBufferedOutputStream() : nsBufferedStream() {}
115 :
116 : static nsresult
117 : Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
118 :
119 104 : nsIOutputStream* Sink() {
120 104 : return (nsIOutputStream*)mStream;
121 : }
122 :
123 : protected:
124 24 : virtual ~nsBufferedOutputStream() { nsBufferedOutputStream::Close(); }
125 :
126 0 : NS_IMETHOD Fill() override { return NS_OK; } // no-op for output streams
127 :
128 : nsCOMPtr<nsISafeOutputStream> mSafeStream; // QI'd from mStream
129 : };
130 :
131 : ////////////////////////////////////////////////////////////////////////////////
132 :
133 : #endif // nsBufferedStreams_h__
|