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"
16 already_AddRefed
<MemoryOutputStream
> MemoryOutputStream::Create(
18 MOZ_ASSERT(aSize
, "Passed zero size!");
20 if (NS_WARN_IF(aSize
> UINT32_MAX
)) {
24 RefPtr
<MemoryOutputStream
> stream
= new MemoryOutputStream();
26 if (NS_WARN_IF(!stream
->mData
.SetLength(aSize
, fallible
))) {
30 return stream
.forget();
33 NS_IMPL_ISUPPORTS(MemoryOutputStream
, nsIOutputStream
)
36 MemoryOutputStream::Close() {
37 mData
.Truncate(mOffset
);
42 MemoryOutputStream::Write(const char* aBuf
, uint32_t aCount
,
44 return WriteSegments(NS_CopySegmentToBuffer
, (char*)aBuf
, aCount
, _retval
);
48 MemoryOutputStream::Flush() { return NS_OK
; }
51 MemoryOutputStream::WriteFrom(nsIInputStream
* aFromStream
, uint32_t aCount
,
53 return NS_ERROR_NOT_IMPLEMENTED
;
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
;
67 if (aCount
> maxCount
) {
71 nsresult rv
= aReader(this, aClosure
, mData
.BeginWriting() + mOffset
, 0,
73 if (NS_SUCCEEDED(rv
)) {
74 MOZ_ASSERT(*_retval
<= aCount
,
75 "Reader should not read more than we asked it to read!");
83 MemoryOutputStream::IsNonBlocking(bool* _retval
) {
90 } // namespace mozilla