blob: 4df2b6051e4403cb3b46e4d4069a5bdac2805af5 [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
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15
Niels Möllere7547d52018-11-01 09:33:08 +010016#include "rtc_base/stream.h"
17
18namespace rtc {
19
20// MemoryStream dynamically resizes to accomodate written data.
21
22class MemoryStream final : public StreamInterface {
23 public:
24 MemoryStream();
25 explicit MemoryStream(const char* data); // Calls SetData(data, strlen(data))
26 MemoryStream(const void* data, size_t length); // Calls SetData(data, length)
27 ~MemoryStream() override;
28
29 StreamState GetState() const override;
30 StreamResult Read(void* buffer,
31 size_t bytes,
32 size_t* bytes_read,
33 int* error) override;
34 StreamResult Write(const void* buffer,
35 size_t bytes,
36 size_t* bytes_written,
37 int* error) override;
38 void Close() override;
39 bool SetPosition(size_t position) override;
40 bool GetPosition(size_t* position) const override;
41 bool GetSize(size_t* size) const override;
42 bool ReserveSize(size_t size) override;
43
44 char* GetBuffer() { return buffer_; }
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_