• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "rtc_base/memory_stream.h"
12 
13 #include <errno.h>
14 #include <string.h>
15 
16 #include <algorithm>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace rtc {
21 
GetState() const22 StreamState MemoryStream::GetState() const {
23   return SS_OPEN;
24 }
25 
Read(void * buffer,size_t bytes,size_t * bytes_read,int * error)26 StreamResult MemoryStream::Read(void* buffer,
27                                 size_t bytes,
28                                 size_t* bytes_read,
29                                 int* error) {
30   if (seek_position_ >= data_length_) {
31     return SR_EOS;
32   }
33   size_t available = data_length_ - seek_position_;
34   if (bytes > available) {
35     // Read partial buffer
36     bytes = available;
37   }
38   memcpy(buffer, &buffer_[seek_position_], bytes);
39   seek_position_ += bytes;
40   if (bytes_read) {
41     *bytes_read = bytes;
42   }
43   return SR_SUCCESS;
44 }
45 
Write(const void * buffer,size_t bytes,size_t * bytes_written,int * error)46 StreamResult MemoryStream::Write(const void* buffer,
47                                  size_t bytes,
48                                  size_t* bytes_written,
49                                  int* error) {
50   size_t available = buffer_length_ - seek_position_;
51   if (0 == available) {
52     // Increase buffer size to the larger of:
53     // a) new position rounded up to next 256 bytes
54     // b) double the previous length
55     size_t new_buffer_length =
56         std::max(((seek_position_ + bytes) | 0xFF) + 1, buffer_length_ * 2);
57     StreamResult result = DoReserve(new_buffer_length, error);
58     if (SR_SUCCESS != result) {
59       return result;
60     }
61     RTC_DCHECK(buffer_length_ >= new_buffer_length);
62     available = buffer_length_ - seek_position_;
63   }
64 
65   if (bytes > available) {
66     bytes = available;
67   }
68   memcpy(&buffer_[seek_position_], buffer, bytes);
69   seek_position_ += bytes;
70   if (data_length_ < seek_position_) {
71     data_length_ = seek_position_;
72   }
73   if (bytes_written) {
74     *bytes_written = bytes;
75   }
76   return SR_SUCCESS;
77 }
78 
Close()79 void MemoryStream::Close() {
80   // nothing to do
81 }
82 
SetPosition(size_t position)83 bool MemoryStream::SetPosition(size_t position) {
84   if (position > data_length_)
85     return false;
86   seek_position_ = position;
87   return true;
88 }
89 
GetPosition(size_t * position) const90 bool MemoryStream::GetPosition(size_t* position) const {
91   if (position)
92     *position = seek_position_;
93   return true;
94 }
95 
Rewind()96 void MemoryStream::Rewind() {
97   seek_position_ = 0;
98 }
99 
GetSize(size_t * size) const100 bool MemoryStream::GetSize(size_t* size) const {
101   if (size)
102     *size = data_length_;
103   return true;
104 }
105 
ReserveSize(size_t size)106 bool MemoryStream::ReserveSize(size_t size) {
107   return (SR_SUCCESS == DoReserve(size, nullptr));
108 }
109 
110 ///////////////////////////////////////////////////////////////////////////////
111 
MemoryStream()112 MemoryStream::MemoryStream() {}
113 
~MemoryStream()114 MemoryStream::~MemoryStream() {
115   delete[] buffer_;
116 }
117 
SetData(const void * data,size_t length)118 void MemoryStream::SetData(const void* data, size_t length) {
119   data_length_ = buffer_length_ = length;
120   delete[] buffer_;
121   buffer_ = new char[buffer_length_];
122   memcpy(buffer_, data, data_length_);
123   seek_position_ = 0;
124 }
125 
DoReserve(size_t size,int * error)126 StreamResult MemoryStream::DoReserve(size_t size, int* error) {
127   if (buffer_length_ >= size)
128     return SR_SUCCESS;
129 
130   if (char* new_buffer = new char[size]) {
131     memcpy(new_buffer, buffer_, data_length_);
132     delete[] buffer_;
133     buffer_ = new_buffer;
134     buffer_length_ = size;
135     return SR_SUCCESS;
136   }
137 
138   if (error) {
139     *error = ENOMEM;
140   }
141   return SR_ERROR;
142 }
143 
144 }  // namespace rtc
145