• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2018 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef RTC_BASE_MEMORY_STREAM_H_
12 #define RTC_BASE_MEMORY_STREAM_H_
13 
14 #include <stddef.h>
15 
16 #include "rtc_base/stream.h"
17 
18 namespace rtc {
19 
20 // MemoryStream dynamically resizes to accomodate written data.
21 
22 class MemoryStream final : public StreamInterface {
23  public:
24   MemoryStream();
25   ~MemoryStream() override;
26 
27   StreamState GetState() const override;
28   StreamResult Read(void* buffer,
29                     size_t bytes,
30                     size_t* bytes_read,
31                     int* error) override;
32   StreamResult Write(const void* buffer,
33                      size_t bytes,
34                      size_t* bytes_written,
35                      int* error) override;
36   void Close() override;
37   bool GetSize(size_t* size) const;
38   bool ReserveSize(size_t size);
39 
40   bool SetPosition(size_t position);
41   bool GetPosition(size_t* position) const;
42   void Rewind();
43 
GetBuffer()44   char* GetBuffer() { return buffer_; }
GetBuffer()45   const char* GetBuffer() const { return buffer_; }
46 
47   void SetData(const void* data, size_t length);
48 
49  private:
50   StreamResult DoReserve(size_t size, int* error);
51 
52   // Invariant: 0 <= seek_position <= data_length_ <= buffer_length_
53   char* buffer_ = nullptr;
54   size_t buffer_length_ = 0;
55   size_t data_length_ = 0;
56   size_t seek_position_ = 0;
57 };
58 
59 }  // namespace rtc
60 
61 #endif  // RTC_BASE_MEMORY_STREAM_H_
62