Backed out changeset de3502ab8cb5 (bug 1847180) for causing gtest failures at AllExte...
[gecko.git] / xpcom / io / SnappyCompressOutputStream.h
blob895691034be6e1a0c788c68ae2ee92673246414b
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_SnappyCompressOutputStream_h__
8 #define mozilla_SnappyCompressOutputStream_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/UniquePtr.h"
12 #include "nsCOMPtr.h"
13 #include "nsIOutputStream.h"
14 #include "nsISupportsImpl.h"
15 #include "SnappyFrameUtils.h"
17 namespace mozilla {
19 class SnappyCompressOutputStream final : public nsIOutputStream,
20 protected detail::SnappyFrameUtils {
21 public:
22 // Maximum compression block size.
23 static const size_t kMaxBlockSize;
25 // Construct a new blocking output stream to compress data to
26 // the given base stream. The base stream must also be blocking.
27 // The compression block size may optionally be set to a value
28 // up to kMaxBlockSize.
29 explicit SnappyCompressOutputStream(nsIOutputStream* aBaseStream,
30 size_t aBlockSize = kMaxBlockSize);
32 // The compression block size. To optimize stream performance
33 // try to write to the stream in segments at least this size.
34 size_t BlockSize() const;
36 private:
37 virtual ~SnappyCompressOutputStream();
39 nsresult FlushToBaseStream();
40 nsresult MaybeFlushStreamIdentifier();
41 nsresult WriteAll(const char* aBuf, uint32_t aCount,
42 uint32_t* aBytesWrittenOut);
44 nsCOMPtr<nsIOutputStream> mBaseStream;
45 const size_t mBlockSize;
47 // Buffer holding copied uncompressed data. This must be copied here
48 // so that the compression can be performed on a single flat buffer.
49 mozilla::UniquePtr<char[]> mBuffer;
51 // The next byte in the uncompressed data to copy incoming data to.
52 size_t mNextByte;
54 // Buffer holding the resulting compressed data.
55 mozilla::UniquePtr<char[]> mCompressedBuffer;
56 size_t mCompressedBufferLength;
58 // The first thing written to the stream must be a stream identifier.
59 bool mStreamIdentifierWritten;
61 public:
62 NS_DECL_THREADSAFE_ISUPPORTS
63 NS_DECL_NSIOUTPUTSTREAM
66 } // namespace mozilla
68 #endif // mozilla_SnappyCompressOutputStream_h__