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