nsBufferedStreams.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef nsBufferedStreams_h__
  6. #define nsBufferedStreams_h__
  7. #include "nsIBufferedStreams.h"
  8. #include "nsIInputStream.h"
  9. #include "nsIOutputStream.h"
  10. #include "nsISafeOutputStream.h"
  11. #include "nsISeekableStream.h"
  12. #include "nsIStreamBufferAccess.h"
  13. #include "nsCOMPtr.h"
  14. #include "nsIIPCSerializableInputStream.h"
  15. ////////////////////////////////////////////////////////////////////////////////
  16. class nsBufferedStream : public nsISeekableStream
  17. {
  18. public:
  19. NS_DECL_THREADSAFE_ISUPPORTS
  20. NS_DECL_NSISEEKABLESTREAM
  21. nsBufferedStream();
  22. nsresult Close();
  23. protected:
  24. virtual ~nsBufferedStream();
  25. nsresult Init(nsISupports* stream, uint32_t bufferSize);
  26. NS_IMETHOD Fill() = 0;
  27. NS_IMETHOD Flush() = 0;
  28. uint32_t mBufferSize;
  29. char* mBuffer;
  30. // mBufferStartOffset is the offset relative to the start of mStream.
  31. int64_t mBufferStartOffset;
  32. // mCursor is the read cursor for input streams, or write cursor for
  33. // output streams, and is relative to mBufferStartOffset.
  34. uint32_t mCursor;
  35. // mFillPoint is the amount available in the buffer for input streams,
  36. // or the high watermark of bytes written into the buffer, and therefore
  37. // is relative to mBufferStartOffset.
  38. uint32_t mFillPoint;
  39. nsISupports* mStream; // cast to appropriate subclass
  40. bool mBufferDisabled;
  41. bool mEOF; // True if mStream is at EOF
  42. uint8_t mGetBufferCount;
  43. };
  44. ////////////////////////////////////////////////////////////////////////////////
  45. class nsBufferedInputStream : public nsBufferedStream,
  46. public nsIBufferedInputStream,
  47. public nsIStreamBufferAccess,
  48. public nsIIPCSerializableInputStream
  49. {
  50. public:
  51. NS_DECL_ISUPPORTS_INHERITED
  52. NS_DECL_NSIINPUTSTREAM
  53. NS_DECL_NSIBUFFEREDINPUTSTREAM
  54. NS_DECL_NSISTREAMBUFFERACCESS
  55. NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
  56. nsBufferedInputStream() : nsBufferedStream() {}
  57. static nsresult
  58. Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
  59. nsIInputStream* Source() {
  60. return (nsIInputStream*)mStream;
  61. }
  62. protected:
  63. virtual ~nsBufferedInputStream() {}
  64. NS_IMETHOD Fill() override;
  65. NS_IMETHOD Flush() override { return NS_OK; } // no-op for input streams
  66. };
  67. ////////////////////////////////////////////////////////////////////////////////
  68. class nsBufferedOutputStream : public nsBufferedStream,
  69. public nsISafeOutputStream,
  70. public nsIBufferedOutputStream,
  71. public nsIStreamBufferAccess
  72. {
  73. public:
  74. NS_DECL_ISUPPORTS_INHERITED
  75. NS_DECL_NSIOUTPUTSTREAM
  76. NS_DECL_NSISAFEOUTPUTSTREAM
  77. NS_DECL_NSIBUFFEREDOUTPUTSTREAM
  78. NS_DECL_NSISTREAMBUFFERACCESS
  79. nsBufferedOutputStream() : nsBufferedStream() {}
  80. static nsresult
  81. Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
  82. nsIOutputStream* Sink() {
  83. return (nsIOutputStream*)mStream;
  84. }
  85. protected:
  86. virtual ~nsBufferedOutputStream() { nsBufferedOutputStream::Close(); }
  87. NS_IMETHOD Fill() override { return NS_OK; } // no-op for output streams
  88. nsCOMPtr<nsISafeOutputStream> mSafeStream; // QI'd from mStream
  89. };
  90. ////////////////////////////////////////////////////////////////////////////////
  91. #endif // nsBufferedStreams_h__