Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / dom / quota / MemoryOutputStream.cpp
blob41eb27c6c6f9fe06ef4b2a9abdf59e07298f7f16
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 #include "MemoryOutputStream.h"
9 #include "nsStreamUtils.h"
11 namespace mozilla {
12 namespace dom {
13 namespace quota {
15 // static
16 already_AddRefed<MemoryOutputStream> MemoryOutputStream::Create(
17 uint64_t aSize) {
18 MOZ_ASSERT(aSize, "Passed zero size!");
20 if (NS_WARN_IF(aSize > UINT32_MAX)) {
21 return nullptr;
24 RefPtr<MemoryOutputStream> stream = new MemoryOutputStream();
26 if (NS_WARN_IF(!stream->mData.SetLength(aSize, fallible))) {
27 return nullptr;
30 return stream.forget();
33 NS_IMPL_ISUPPORTS(MemoryOutputStream, nsIOutputStream)
35 NS_IMETHODIMP
36 MemoryOutputStream::Close() {
37 mData.Truncate(mOffset);
38 return NS_OK;
41 NS_IMETHODIMP
42 MemoryOutputStream::Write(const char* aBuf, uint32_t aCount,
43 uint32_t* _retval) {
44 return WriteSegments(NS_CopySegmentToBuffer, (char*)aBuf, aCount, _retval);
47 NS_IMETHODIMP
48 MemoryOutputStream::Flush() { return NS_OK; }
50 NS_IMETHODIMP
51 MemoryOutputStream::WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
52 uint32_t* _retval) {
53 return NS_ERROR_NOT_IMPLEMENTED;
56 NS_IMETHODIMP
57 MemoryOutputStream::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
58 uint32_t aCount, uint32_t* _retval) {
59 MOZ_ASSERT(mData.Length() >= mOffset, "Bad stream state!");
61 uint32_t maxCount = mData.Length() - mOffset;
62 if (maxCount == 0) {
63 *_retval = 0;
64 return NS_OK;
67 if (aCount > maxCount) {
68 aCount = maxCount;
71 nsresult rv = aReader(this, aClosure, mData.BeginWriting() + mOffset, 0,
72 aCount, _retval);
73 if (NS_SUCCEEDED(rv)) {
74 MOZ_ASSERT(*_retval <= aCount,
75 "Reader should not read more than we asked it to read!");
76 mOffset += *_retval;
79 return NS_OK;
82 NS_IMETHODIMP
83 MemoryOutputStream::IsNonBlocking(bool* _retval) {
84 *_retval = false;
85 return NS_OK;
88 } // namespace quota
89 } // namespace dom
90 } // namespace mozilla