memory_stream.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. ~MemoryStream() override;
  20. StreamState GetState() const override;
  21. StreamResult Read(void* buffer,
  22. size_t bytes,
  23. size_t* bytes_read,
  24. int* error) override;
  25. StreamResult Write(const void* buffer,
  26. size_t bytes,
  27. size_t* bytes_written,
  28. int* error) override;
  29. void Close() override;
  30. bool GetSize(size_t* size) const;
  31. bool ReserveSize(size_t size);
  32. bool SetPosition(size_t position);
  33. bool GetPosition(size_t* position) const;
  34. void Rewind();
  35. char* GetBuffer() { return buffer_; }
  36. const char* GetBuffer() const { return buffer_; }
  37. void SetData(const void* data, size_t length);
  38. private:
  39. StreamResult DoReserve(size_t size, int* error);
  40. // Invariant: 0 <= seek_position <= data_length_ <= buffer_length_
  41. char* buffer_ = nullptr;
  42. size_t buffer_length_ = 0;
  43. size_t data_length_ = 0;
  44. size_t seek_position_ = 0;
  45. };
  46. } // namespace rtc
  47. #endif // RTC_BASE_MEMORY_STREAM_H_