• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 #pragma once
16 
17 #include "aemu/base/CppMacros.h"
18 #include "aemu/base/files/Stream.h"
19 
20 #include <vector>
21 
22 namespace android {
23 namespace base {
24 
25 // An implementation of the Stream interface on top of a vector.
26 class MemStream : public Stream {
27 public:
28     using Buffer = std::vector<char>;
29 
30     MemStream(int reserveSize = 512);
31     MemStream(Buffer&& data);
32 
33     MemStream(MemStream&& other) = default;
34     MemStream& operator=(MemStream&& other) = default;
35 
36     int writtenSize() const;
37     int readPos() const;
38     int readSize() const;
39 
40     // Stream interface implementation.
41     ssize_t read(void* buffer, size_t size) override;
42     ssize_t write(const void* buffer, size_t size) override;
43 
44     // protobuf support
setProtobuf(void * pb)45     void setProtobuf(void* pb) { mPb = pb; }
getProtobuf()46     void* getProtobuf() override { return mPb; }
47 
48     // Snapshot support.
49     void save(Stream* stream) const;
50     void load(Stream* stream);
51 
buffer()52     const Buffer& buffer() const { return mData; }
53 
54     void rewind();
55 
56 private:
57     DISALLOW_COPY_AND_ASSIGN(MemStream);
58 
59     Buffer mData;
60     int mReadPos = 0;
61     void* mPb = nullptr;
62 };
63 
64 }  // namespace base
65 }  // namespace android
66