1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef AAPT_IO_STRINGSTREAM_H 18 #define AAPT_IO_STRINGSTREAM_H 19 20 #include "io/Io.h" 21 22 #include <memory> 23 24 #include "android-base/macros.h" 25 #include "androidfw/StringPiece.h" 26 27 namespace aapt { 28 namespace io { 29 30 class StringInputStream : public KnownSizeInputStream { 31 public: 32 explicit StringInputStream(const android::StringPiece& str); 33 34 bool Next(const void** data, size_t* size) override; 35 36 void BackUp(size_t count) override; 37 38 size_t ByteCount() const override; 39 HadError()40 inline bool HadError() const override { 41 return false; 42 } 43 GetError()44 inline std::string GetError() const override { 45 return {}; 46 } 47 48 size_t TotalSize() const override; 49 50 private: 51 DISALLOW_COPY_AND_ASSIGN(StringInputStream); 52 53 android::StringPiece str_; 54 size_t offset_; 55 }; 56 57 class StringOutputStream : public OutputStream { 58 public: 59 explicit StringOutputStream(std::string* str, size_t buffer_capacity = 4096u); 60 61 ~StringOutputStream(); 62 63 bool Next(void** data, size_t* size) override; 64 65 void BackUp(size_t count) override; 66 67 void Flush(); 68 69 size_t ByteCount() const override; 70 HadError()71 inline bool HadError() const override { 72 return false; 73 } 74 GetError()75 inline std::string GetError() const override { 76 return {}; 77 } 78 79 private: 80 DISALLOW_COPY_AND_ASSIGN(StringOutputStream); 81 82 void FlushImpl(); 83 84 std::string* str_; 85 size_t buffer_capacity_; 86 size_t buffer_offset_; 87 std::unique_ptr<char[]> buffer_; 88 }; 89 90 } // namespace io 91 } // namespace aapt 92 93 #endif // AAPT_IO_STRINGSTREAM_H 94