/src/mozilla-central/dom/quota/MemoryOutputStream.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef mozilla_dom_quota_MemoryOutputStream_h |
8 | | #define mozilla_dom_quota_MemoryOutputStream_h |
9 | | |
10 | | #include "nsIOutputStream.h" |
11 | | |
12 | | namespace mozilla { |
13 | | namespace dom { |
14 | | namespace quota { |
15 | | |
16 | | // An output stream so you can read your potentially-async input stream into |
17 | | // a contiguous buffer in the form of an nsCString using NS_AsyncCopy. |
18 | | // Back when streams were more synchronous and people didn't know blocking I/O |
19 | | // was bad, if you wanted to read a stream into a flat buffer, you could use |
20 | | // NS_ReadInputStreamToString/NS_ReadInputStreamToBuffer. But those don't work |
21 | | // with async streams. This can be used to replace hand-rolled Read/AsyncWait() |
22 | | // loops. Because you specify the expected size up front, the nsCString buffer |
23 | | // is pre-allocated so wasteful reallocations can be avoided. However, |
24 | | // nsCString currently may over-allocate and this can be problematic on 32-bit |
25 | | // windows until we're rid of that build configuration. |
26 | | class MemoryOutputStream final |
27 | | : public nsIOutputStream |
28 | | { |
29 | | nsCString mData; |
30 | | uint64_t mOffset; |
31 | | |
32 | | public: |
33 | | static already_AddRefed<MemoryOutputStream> |
34 | | Create(uint64_t aSize); |
35 | | |
36 | | const nsCString& |
37 | | Data() const |
38 | | { |
39 | | return mData; |
40 | | } |
41 | | |
42 | | private: |
43 | | MemoryOutputStream() |
44 | | : mOffset(0) |
45 | 0 | { } |
46 | | |
47 | | virtual ~MemoryOutputStream() |
48 | 0 | { } |
49 | | |
50 | | NS_DECL_THREADSAFE_ISUPPORTS |
51 | | NS_DECL_NSIOUTPUTSTREAM |
52 | | }; |
53 | | |
54 | | } // namespace quota |
55 | | } // namespace dom |
56 | | } // namespace mozilla |
57 | | |
58 | | #endif /* mozilla_dom_quota_MemoryOutputStream_h */ |