michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsBufferedStreams_h__ michael@0: #define nsBufferedStreams_h__ michael@0: michael@0: #include "nsIBufferedStreams.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsISafeOutputStream.h" michael@0: #include "nsISeekableStream.h" michael@0: #include "nsIStreamBufferAccess.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIIPCSerializableInputStream.h" michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class nsBufferedStream : public nsISeekableStream michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSISEEKABLESTREAM michael@0: michael@0: nsBufferedStream(); michael@0: virtual ~nsBufferedStream(); michael@0: michael@0: nsresult Close(); michael@0: michael@0: protected: michael@0: nsresult Init(nsISupports* stream, uint32_t bufferSize); michael@0: NS_IMETHOD Fill() = 0; michael@0: NS_IMETHOD Flush() = 0; michael@0: michael@0: uint32_t mBufferSize; michael@0: char* mBuffer; michael@0: michael@0: // mBufferStartOffset is the offset relative to the start of mStream. michael@0: int64_t mBufferStartOffset; michael@0: michael@0: // mCursor is the read cursor for input streams, or write cursor for michael@0: // output streams, and is relative to mBufferStartOffset. michael@0: uint32_t mCursor; michael@0: michael@0: // mFillPoint is the amount available in the buffer for input streams, michael@0: // or the high watermark of bytes written into the buffer, and therefore michael@0: // is relative to mBufferStartOffset. michael@0: uint32_t mFillPoint; michael@0: michael@0: nsISupports* mStream; // cast to appropriate subclass michael@0: michael@0: bool mBufferDisabled; michael@0: bool mEOF; // True if mStream is at EOF michael@0: uint8_t mGetBufferCount; michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class nsBufferedInputStream : public nsBufferedStream, michael@0: public nsIBufferedInputStream, michael@0: public nsIStreamBufferAccess, michael@0: public nsIIPCSerializableInputStream michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_NSIINPUTSTREAM michael@0: NS_DECL_NSIBUFFEREDINPUTSTREAM michael@0: NS_DECL_NSISTREAMBUFFERACCESS michael@0: NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM michael@0: michael@0: nsBufferedInputStream() : nsBufferedStream() {} michael@0: virtual ~nsBufferedInputStream() {} michael@0: michael@0: static nsresult michael@0: Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); michael@0: michael@0: nsIInputStream* Source() { michael@0: return (nsIInputStream*)mStream; michael@0: } michael@0: michael@0: protected: michael@0: NS_IMETHOD Fill(); michael@0: NS_IMETHOD Flush() { return NS_OK; } // no-op for input streams michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class nsBufferedOutputStream : public nsBufferedStream, michael@0: public nsISafeOutputStream, michael@0: public nsIBufferedOutputStream, michael@0: public nsIStreamBufferAccess michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_NSIOUTPUTSTREAM michael@0: NS_DECL_NSISAFEOUTPUTSTREAM michael@0: NS_DECL_NSIBUFFEREDOUTPUTSTREAM michael@0: NS_DECL_NSISTREAMBUFFERACCESS michael@0: michael@0: nsBufferedOutputStream() : nsBufferedStream() {} michael@0: virtual ~nsBufferedOutputStream() { nsBufferedOutputStream::Close(); } michael@0: michael@0: static nsresult michael@0: Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); michael@0: michael@0: nsIOutputStream* Sink() { michael@0: return (nsIOutputStream*)mStream; michael@0: } michael@0: michael@0: protected: michael@0: NS_IMETHOD Fill() { return NS_OK; } // no-op for input streams michael@0: michael@0: nsCOMPtr mSafeStream; // QI'd from mStream michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #endif // nsBufferedStreams_h__