• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "aemu/base/files/MemStream.h"
16 
17 #include "aemu/base/files/StreamSerializing.h"
18 
19 #include <algorithm>
20 #include <utility>
21 #include <cstring>
22 
23 namespace gfxstream {
24 namespace guest {
25 
MemStream(int reserveSize)26 MemStream::MemStream(int reserveSize) {
27     mData.reserve(reserveSize);
28 }
29 
MemStream(Buffer && data)30 MemStream::MemStream(Buffer&& data) : mData(std::move(data)) {}
31 
read(void * buffer,size_t size)32 ssize_t MemStream::read(void* buffer, size_t size) {
33     const auto sizeToRead = std::min<int>(size, readSize());
34     memcpy(buffer, mData.data() + mReadPos, sizeToRead);
35     mReadPos += sizeToRead;
36     return sizeToRead;
37 }
38 
write(const void * buffer,size_t size)39 ssize_t MemStream::write(const void* buffer, size_t size) {
40     mData.insert(mData.end(), (const char*)buffer, (const char*)buffer + size);
41     return size;
42 }
43 
writtenSize() const44 int MemStream::writtenSize() const {
45     return (int)mData.size();
46 }
47 
readPos() const48 int MemStream::readPos() const {
49     return mReadPos;
50 }
51 
readSize() const52 int MemStream::readSize() const {
53     return mData.size() - mReadPos;
54 }
55 
save(Stream * stream) const56 void MemStream::save(Stream* stream) const {
57     saveBuffer(stream, mData);
58 }
59 
load(Stream * stream)60 void MemStream::load(Stream* stream) {
61     loadBuffer(stream, &mData);
62     mReadPos = 0;
63 }
64 
65 }  // namespace base
66 }  // namespace android
67