blob: 936f71b34c010b2263e478029329b1ecfd270d70 [file] [log] [blame]
Niels Möllere7547d52018-11-01 09:33:08 +01001/*
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 "rtc_base/stream.h"
15
16namespace rtc {
17
18// MemoryStream dynamically resizes to accomodate written data.
19
20class MemoryStream final : public StreamInterface {
21 public:
22 MemoryStream();
23 explicit MemoryStream(const char* data); // Calls SetData(data, strlen(data))
24 MemoryStream(const void* data, size_t length); // Calls SetData(data, length)
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 SetPosition(size_t position) override;
38 bool GetPosition(size_t* position) const override;
39 bool GetSize(size_t* size) const override;
40 bool ReserveSize(size_t size) override;
41
42 char* GetBuffer() { return buffer_; }
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_