You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef RTC_BASE_MEMORY_STREAM_H_
  11. #define RTC_BASE_MEMORY_STREAM_H_
  12. #include <stddef.h>
  13. #include "rtc_base/stream.h"
  14. namespace rtc {
  15. // MemoryStream dynamically resizes to accomodate written data.
  16. class MemoryStream final : public StreamInterface {
  17. public:
  18. MemoryStream();
  19. explicit MemoryStream(const char* data); // Calls SetData(data, strlen(data))
  20. MemoryStream(const void* data, size_t length); // Calls SetData(data, length)
  21. ~MemoryStream() override;
  22. StreamState GetState() const override;
  23. StreamResult Read(void* buffer,
  24. size_t bytes,
  25. size_t* bytes_read,
  26. int* error) override;
  27. StreamResult Write(const void* buffer,
  28. size_t bytes,
  29. size_t* bytes_written,
  30. int* error) override;
  31. void Close() override;
  32. bool GetSize(size_t* size) const;
  33. bool ReserveSize(size_t size);
  34. bool SetPosition(size_t position);
  35. bool GetPosition(size_t* position) const;
  36. void Rewind();
  37. char* GetBuffer() { return buffer_; }
  38. const char* GetBuffer() const { return buffer_; }
  39. void SetData(const void* data, size_t length);
  40. private:
  41. StreamResult DoReserve(size_t size, int* error);
  42. // Invariant: 0 <= seek_position <= data_length_ <= buffer_length_
  43. char* buffer_ = nullptr;
  44. size_t buffer_length_ = 0;
  45. size_t data_length_ = 0;
  46. size_t seek_position_ = 0;
  47. };
  48. } // namespace rtc
  49. #endif // RTC_BASE_MEMORY_STREAM_H_