blob: 75a584654f2f6773fcff2cbe2eb92774c146bb80 [file] [log] [blame]
Lingfeng Yangc5ae0372019-12-13 11:20:29 -08001// 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 "base/CppMacros.h"
18#include "base/Stream.h"
19
20#include <vector>
21
22namespace android {
23namespace base {
24
25// An implementation of the Stream interface on top of a vector.
26class MemStream : public Stream {
27public:
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 // Snapshot support.
45 void save(Stream* stream) const;
46 void load(Stream* stream);
47
48 const Buffer& buffer() const { return mData; }
49
50private:
51 DISALLOW_COPY_AND_ASSIGN(MemStream);
52
53 Buffer mData;
54 int mReadPos = 0;
55};
56
57} // namespace base
58} // namespace android