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(rtc::ArrayView<uint8_t> buffer, 29 size_t& bytes_read, 30 int& error) override; 31 StreamResult Write(rtc::ArrayView<const uint8_t> buffer, 32 size_t& bytes_written, 33 int& error) override; 34 void Close() override; 35 bool GetSize(size_t* size) const; 36 bool ReserveSize(size_t size); 37 38 bool SetPosition(size_t position); 39 bool GetPosition(size_t* position) const; 40 void Rewind(); 41 GetBuffer()42 char* GetBuffer() { return buffer_; } GetBuffer()43 const char* GetBuffer() const { return buffer_; } 44 45 void SetData(const void* data, size_t length); 46 47 private: 48 StreamResult DoReserve(size_t size, int* error); 49 50 // Invariant: 0 <= seek_position <= data_length_ <= buffer_length_ 51 char* buffer_ = nullptr; 52 size_t buffer_length_ = 0; 53 size_t data_length_ = 0; 54 size_t seek_position_ = 0; 55 }; 56 57 } // namespace rtc 58 59 #endif // RTC_BASE_MEMORY_STREAM_H_ 60