1 // Copyright 2015 The Android Open Source Project 2 // 3 // This software is licensed under the terms of the GNU General Public 4 // License version 2, as published by the Free Software Foundation, and 5 // may be copied, distributed, and modified under those terms. 6 // 7 // This program is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 // GNU General Public License for more details. 11 12 #pragma once 13 14 #include "android/base/Compiler.h" 15 #include "android/base/files/Stream.h" 16 17 #include <vector> 18 19 namespace android { 20 namespace base { 21 22 // An implementation of the Stream interface on top of a vector. 23 class MemStream : public Stream { 24 public: 25 using Buffer = std::vector<char>; 26 27 MemStream(int reserveSize = 512); 28 MemStream(Buffer&& data); 29 30 MemStream(MemStream&& other) = default; 31 MemStream& operator=(MemStream&& other) = default; 32 33 int writtenSize() const; 34 int readPos() const; 35 int readSize() const; 36 37 // Stream interface implementation. 38 ssize_t read(void* buffer, size_t size) override; 39 ssize_t write(const void* buffer, size_t size) override; 40 41 // Snapshot support. 42 void save(Stream* stream) const; 43 void load(Stream* stream); 44 45 const Buffer& buffer() const { return mData; } 46 47 private: 48 DISALLOW_COPY_AND_ASSIGN(MemStream); 49 50 Buffer mData; 51 int mReadPos = 0; 52 }; 53 54 } // namespace base 55 } // namespace android 56