Bug 1492908 [wpt PR 13122] - Update wpt metadata, a=testonly
[gecko.git] / xpcom / io / SnappyUncompressInputStream.h
blob0d24c0d11571ceed959ceccbb482ddea52517279
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
7 #ifndef mozilla_SnappyUncompressInputStream_h__
8 #define mozilla_SnappyUncompressInputStream_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/UniquePtr.h"
12 #include "nsCOMPtr.h"
13 #include "nsIInputStream.h"
14 #include "nsISupportsImpl.h"
15 #include "SnappyFrameUtils.h"
17 namespace mozilla {
19 class SnappyUncompressInputStream final : public nsIInputStream
20 , protected detail::SnappyFrameUtils
22 public:
23 // Construct a new blocking stream to uncompress the given base stream. The
24 // base stream must also be blocking. The base stream does not have to be
25 // buffered.
26 explicit SnappyUncompressInputStream(nsIInputStream* aBaseStream);
28 private:
29 virtual ~SnappyUncompressInputStream();
31 // Parse the next chunk of data. This may populate mBuffer and set
32 // mBufferFillSize. This should not be called when mBuffer already
33 // contains data.
34 nsresult ParseNextChunk(uint32_t* aBytesReadOut);
36 // Convenience routine to Read() from the base stream until we get
37 // the given number of bytes or reach EOF.
39 // aBuf - The buffer to write the bytes into.
40 // aCount - Max number of bytes to read. If the stream closes
41 // fewer bytes my be read.
42 // aMinValidCount - A minimum expected number of bytes. If we find
43 // fewer than this many bytes, then return
44 // NS_ERROR_CORRUPTED_CONTENT. If nothing was read due
45 // due to EOF (aBytesReadOut == 0), then NS_OK is returned.
46 // aBytesReadOut - An out parameter indicating how many bytes were read.
47 nsresult ReadAll(char* aBuf, uint32_t aCount, uint32_t aMinValidCount,
48 uint32_t* aBytesReadOut);
50 // Convenience routine to determine how many bytes of uncompressed data
51 // we currently have in our buffer.
52 size_t UncompressedLength() const;
54 nsCOMPtr<nsIInputStream> mBaseStream;
56 // Buffer to hold compressed data. Must copy here since we need a large
57 // flat buffer to run the uncompress process on. Always the same length
58 // of SnappyFrameUtils::MaxCompressedBufferLength(snappy::kBlockSize)
59 // bytes long.
60 mozilla::UniquePtr<char[]> mCompressedBuffer;
62 // Buffer storing the resulting uncompressed data. Exactly snappy::kBlockSize
63 // bytes long.
64 mozilla::UniquePtr<char[]> mUncompressedBuffer;
66 // Number of bytes of uncompressed data in mBuffer.
67 size_t mUncompressedBytes;
69 // Next byte of mBuffer to return in ReadSegments(). Must be less than
70 // mBufferFillSize
71 size_t mNextByte;
73 // Next chunk in the stream that has been parsed during read-ahead.
74 ChunkType mNextChunkType;
76 // Length of next chunk's length that has been determined during read-ahead.
77 size_t mNextChunkDataLength;
79 // The stream must begin with a StreamIdentifier chunk. Are we still
80 // expecting it?
81 bool mNeedFirstStreamIdentifier;
83 public:
84 NS_DECL_THREADSAFE_ISUPPORTS
85 NS_DECL_NSIINPUTSTREAM
88 } // namespace mozilla
90 #endif // mozilla_SnappyUncompressInputStream_h__