nsDiskCacheStreams.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef _nsDiskCacheStreams_h_
  7. #define _nsDiskCacheStreams_h_
  8. #include "mozilla/MemoryReporting.h"
  9. #include "nsDiskCacheBinding.h"
  10. #include "nsCache.h"
  11. #include "nsIInputStream.h"
  12. #include "nsIOutputStream.h"
  13. #include "mozilla/Atomics.h"
  14. class nsDiskCacheDevice;
  15. class nsDiskCacheStreamIO : public nsIOutputStream {
  16. public:
  17. explicit nsDiskCacheStreamIO(nsDiskCacheBinding * binding);
  18. NS_DECL_THREADSAFE_ISUPPORTS
  19. NS_DECL_NSIOUTPUTSTREAM
  20. nsresult GetInputStream(uint32_t offset, nsIInputStream ** inputStream);
  21. nsresult GetOutputStream(uint32_t offset, nsIOutputStream ** outputStream);
  22. nsresult ClearBinding();
  23. void IncrementInputStreamCount() { mInStreamCount++; }
  24. void DecrementInputStreamCount()
  25. {
  26. mInStreamCount--;
  27. NS_ASSERTION(mInStreamCount >= 0, "mInStreamCount has gone negative");
  28. }
  29. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
  30. // GCC 2.95.2 requires this to be defined, although we never call it.
  31. // and OS/2 requires that it not be private
  32. nsDiskCacheStreamIO() { NS_NOTREACHED("oops"); }
  33. private:
  34. virtual ~nsDiskCacheStreamIO();
  35. nsresult OpenCacheFile(int flags, PRFileDesc ** fd);
  36. nsresult ReadCacheBlocks(uint32_t bufferSize);
  37. nsresult FlushBufferToFile();
  38. void UpdateFileSize();
  39. void DeleteBuffer();
  40. nsresult CloseOutputStream();
  41. nsresult SeekAndTruncate(uint32_t offset);
  42. nsDiskCacheBinding * mBinding; // not an owning reference
  43. nsDiskCacheDevice * mDevice;
  44. mozilla::Atomic<int32_t> mInStreamCount;
  45. PRFileDesc * mFD;
  46. uint32_t mStreamEnd; // current size of data
  47. uint32_t mBufSize; // current end of buffer
  48. char * mBuffer;
  49. bool mOutputStreamIsOpen;
  50. };
  51. #endif // _nsDiskCacheStreams_h_