aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/streams/memory_stream.cc
blob: 54f127a4e99cfccaa99f257d21287b2aff292288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <brillo/streams/memory_stream.h>

#include <limits>

#include <base/bind.h>
#include <brillo/message_loops/message_loop.h>
#include <brillo/streams/stream_errors.h>
#include <brillo/streams/stream_utils.h>

namespace brillo {

MemoryStream::MemoryStream(
    std::unique_ptr<data_container::DataContainerInterface> container,
    size_t stream_position)
    : container_{std::move(container)}, stream_position_{stream_position} {}

StreamPtr MemoryStream::OpenRef(const void* buffer,
                                size_t size,
                                ErrorPtr* error) {
  std::unique_ptr<data_container::ReadOnlyBuffer> container{
      new data_container::ReadOnlyBuffer{buffer, size}};
  return CreateEx(std::move(container), 0, error);
}

StreamPtr MemoryStream::OpenCopyOf(const void* buffer,
                                   size_t size,
                                   ErrorPtr* error) {
  std::unique_ptr<data_container::ReadOnlyVectorCopy<uint8_t>> container{
      new data_container::ReadOnlyVectorCopy<uint8_t>{
          reinterpret_cast<const uint8_t*>(buffer), size}};
  return CreateEx(std::move(container), 0, error);
}

StreamPtr MemoryStream::OpenRef(const std::string& buffer, ErrorPtr* error) {
  std::unique_ptr<data_container::ReadOnlyStringRef> container{
      new data_container::ReadOnlyStringRef{buffer}};
  return CreateEx(std::move(container), 0, error);
}

StreamPtr MemoryStream::OpenCopyOf(std::string buffer, ErrorPtr* error) {
  std::unique_ptr<data_container::ReadOnlyStringCopy> container{
      new data_container::ReadOnlyStringCopy{std::move(buffer)}};
  return CreateEx(std::move(container), 0, error);
}

StreamPtr MemoryStream::OpenRef(const char* buffer, ErrorPtr* error) {
  return OpenRef(buffer, std::strlen(buffer), error);
}

StreamPtr MemoryStream::OpenCopyOf(const char* buffer, ErrorPtr* error) {
  return OpenCopyOf(buffer, std::strlen(buffer), error);
}

StreamPtr MemoryStream::Create(size_t reserve_size, ErrorPtr* error) {
  std::unique_ptr<data_container::ByteBuffer> container{
      new data_container::ByteBuffer{reserve_size}};
  return CreateEx(std::move(container), 0, error);
}

StreamPtr MemoryStream::CreateRef(std::string* buffer, ErrorPtr* error) {
  std::unique_ptr<data_container::StringPtr> container{
      new data_container::StringPtr{buffer}};
  return CreateEx(std::move(container), 0, error);
}

StreamPtr MemoryStream::CreateRefForAppend(std::string* buffer,
                                           ErrorPtr* error) {
  std::unique_ptr<data_container::StringPtr> container{
      new data_container::StringPtr{buffer}};
  return CreateEx(std::move(container), buffer->size(), error);
}

StreamPtr MemoryStream::CreateEx(
    std::unique_ptr<data_container::DataContainerInterface> container,
    size_t stream_position,
    ErrorPtr* error) {
  ignore_result(error);  // Unused.
  return StreamPtr{new MemoryStream(std::move(container), stream_position)};
}

bool MemoryStream::IsOpen() const { return container_ != nullptr; }
bool MemoryStream::CanRead() const { return IsOpen(); }

bool MemoryStream::CanWrite() const {
  return IsOpen() && !container_->IsReadOnly();
}

bool MemoryStream::CanSeek() const { return IsOpen(); }
bool MemoryStream::CanGetSize() const { return IsOpen(); }

uint64_t MemoryStream::GetSize() const {
  return IsOpen() ? container_->GetSize() : 0;
}

bool MemoryStream::SetSizeBlocking(uint64_t size, ErrorPtr* error) {
  if (!CheckContainer(error))
    return false;
  return container_->Resize(size, error);
}

uint64_t MemoryStream::GetRemainingSize() const {
  uint64_t pos = GetPosition();
  uint64_t size = GetSize();
  return (pos < size) ? size - pos : 0;
}

uint64_t MemoryStream::GetPosition() const {
  return IsOpen() ? stream_position_ : 0;
}

bool MemoryStream::Seek(int64_t offset,
                        Whence whence,
                        uint64_t* new_position,
                        ErrorPtr* error) {
  uint64_t pos = 0;
  if (!CheckContainer(error) ||
      !stream_utils::CalculateStreamPosition(FROM_HERE, offset, whence,
                                             stream_position_, GetSize(), &pos,
                                             error)) {
    return false;
  }
  if (pos > static_cast<uint64_t>(std::numeric_limits<size_t>::max())) {
    // This can only be the case on 32 bit systems.
    brillo::Error::AddTo(error, FROM_HERE, errors::stream::kDomain,
                         errors::stream::kInvalidParameter,
                         "Stream pointer position is outside allowed limits");
    return false;
  }

  stream_position_ = static_cast<size_t>(pos);
  if (new_position)
    *new_position = stream_position_;
  return true;
}

bool MemoryStream::ReadNonBlocking(void* buffer,
                                   size_t size_to_read,
                                   size_t* size_read,
                                   bool* end_of_stream,
                                   ErrorPtr* error) {
  if (!CheckContainer(error))
    return false;
  size_t read = 0;
  if (!container_->Read(buffer, size_to_read, stream_position_, &read, error))
    return false;
  stream_position_ += read;
  *size_read = read;
  if (end_of_stream)
    *end_of_stream = (read == 0) && (size_to_read != 0);
  return true;
}

bool MemoryStream::WriteNonBlocking(const void* buffer,
                                    size_t size_to_write,
                                    size_t* size_written,
                                    ErrorPtr* error) {
  if (!CheckContainer(error))
    return false;
  if (!container_->Write(buffer, size_to_write, stream_position_, size_written,
                         error)) {
    return false;
  }
  stream_position_ += *size_written;
  return true;
}

bool MemoryStream::FlushBlocking(ErrorPtr* error) {
  return CheckContainer(error);
}

bool MemoryStream::CloseBlocking(ErrorPtr* error) {
  ignore_result(error);  // Unused.
  container_.reset();
  return true;
}

bool MemoryStream::CheckContainer(ErrorPtr* error) const {
  return container_ || stream_utils::ErrorStreamClosed(FROM_HERE, error);
}

bool MemoryStream::WaitForData(AccessMode mode,
                               const base::Callback<void(AccessMode)>& callback,
                               ErrorPtr* /* error */) {
  MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, mode));
  return true;
}

bool MemoryStream::WaitForDataBlocking(AccessMode in_mode,
                                       base::TimeDelta /* timeout */,
                                       AccessMode* out_mode,
                                       ErrorPtr* /* error */) {
  if (out_mode)
    *out_mode = in_mode;
  return true;
}

}  // namespace brillo