xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/spdy/core/array_output_buffer.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_SPDY_CORE_ARRAY_OUTPUT_BUFFER_H_
6 #define QUICHE_SPDY_CORE_ARRAY_OUTPUT_BUFFER_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 
11 #include "quiche/common/platform/api/quiche_export.h"
12 #include "quiche/spdy/core/zero_copy_output_buffer.h"
13 
14 namespace spdy {
15 
16 class QUICHE_EXPORT ArrayOutputBuffer : public ZeroCopyOutputBuffer {
17  public:
18   // |buffer| is pointed to the output to write to, and |size| is the capacity
19   // of the output.
ArrayOutputBuffer(char * buffer,int64_t size)20   ArrayOutputBuffer(char* buffer, int64_t size)
21       : current_(buffer), begin_(buffer), capacity_(size) {}
~ArrayOutputBuffer()22   ~ArrayOutputBuffer() override {}
23 
24   ArrayOutputBuffer(const ArrayOutputBuffer&) = delete;
25   ArrayOutputBuffer& operator=(const ArrayOutputBuffer&) = delete;
26 
27   void Next(char** data, int* size) override;
28   void AdvanceWritePtr(int64_t count) override;
29   uint64_t BytesFree() const override;
30 
Size()31   size_t Size() const { return current_ - begin_; }
Begin()32   char* Begin() const { return begin_; }
33 
34   // Resets the buffer to its original state.
Reset()35   void Reset() {
36     capacity_ += Size();
37     current_ = begin_;
38   }
39 
40  private:
41   char* current_ = nullptr;
42   char* begin_ = nullptr;
43   uint64_t capacity_ = 0;
44 };
45 
46 }  // namespace spdy
47 
48 #endif  // QUICHE_SPDY_CORE_ARRAY_OUTPUT_BUFFER_H_
49