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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_StreamBufferSinkImpl_h
8 #define mozilla_StreamBufferSinkImpl_h
10 #include "mozilla/Buffer.h"
11 #include "mozilla/StreamBufferSink.h"
12 #include "mozilla/UniquePtr.h"
16 class BufferSink final
: public StreamBufferSink
{
18 explicit BufferSink(Buffer
<char>&& aBuffer
) : mBuffer(std::move(aBuffer
)) {}
20 explicit BufferSink(size_t aLength
) : mBuffer(aLength
) {}
22 static UniquePtr
<BufferSink
> Alloc(size_t aLength
) {
23 auto maybeBuffer
= Buffer
<char>::Alloc(aLength
);
28 return MakeUnique
<BufferSink
>(maybeBuffer
.extract());
31 mozilla::Span
<char> Data() override
{ return mBuffer
.AsWritableSpan(); }
37 class nsBorrowedSink final
: public StreamBufferSink
{
39 explicit nsBorrowedSink(mozilla::Span
<char> aBuffer
) : mBuffer(aBuffer
) {}
41 mozilla::Span
<char> Data() override
{ return mBuffer
; }
44 mozilla::Span
<char> mBuffer
;
47 } // namespace mozilla
49 #endif // mozilla_StreamBufferSinkImpl_h