No Bug, mozilla-central repo-update HSTS HPKP remote-settings tld-suffixes ct-logs...
[gecko.git] / xpcom / io / StreamBufferSinkImpl.h
blob9fba413ef226f8757c2e677be98256325b18d0a7
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"
14 namespace mozilla {
16 class BufferSink final : public StreamBufferSink {
17 public:
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);
24 if (!maybeBuffer) {
25 return nullptr;
28 return MakeUnique<BufferSink>(maybeBuffer.extract());
31 mozilla::Span<char> Data() override { return mBuffer.AsWritableSpan(); }
33 private:
34 Buffer<char> mBuffer;
37 class nsBorrowedSink final : public StreamBufferSink {
38 public:
39 explicit nsBorrowedSink(mozilla::Span<char> aBuffer) : mBuffer(aBuffer) {}
41 mozilla::Span<char> Data() override { return mBuffer; }
43 private:
44 mozilla::Span<char> mBuffer;
47 } // namespace mozilla
49 #endif // mozilla_StreamBufferSinkImpl_h