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 #include "io/StringStream.h"
18
19 using ::android::StringPiece;
20
21 namespace aapt {
22 namespace io {
23
StringInputStream(StringPiece str)24 StringInputStream::StringInputStream(StringPiece str) : str_(str), offset_(0u) {
25 }
26
Next(const void ** data,size_t * size)27 bool StringInputStream::Next(const void** data, size_t* size) {
28 if (offset_ == str_.size()) {
29 return false;
30 }
31
32 *data = str_.data() + offset_;
33 *size = str_.size() - offset_;
34 offset_ = str_.size();
35 return true;
36 }
37
BackUp(size_t count)38 void StringInputStream::BackUp(size_t count) {
39 if (count > offset_) {
40 offset_ = 0u;
41 } else {
42 offset_ -= count;
43 }
44 }
45
ByteCount() const46 size_t StringInputStream::ByteCount() const {
47 return offset_;
48 }
49
TotalSize() const50 size_t StringInputStream::TotalSize() const {
51 return str_.size();
52 }
53
ReadFullyAtOffset(void * data,size_t byte_count,off64_t offset)54 bool StringInputStream::ReadFullyAtOffset(void* data, size_t byte_count, off64_t offset) {
55 if (byte_count == 0) {
56 return true;
57 }
58 if (offset < 0) {
59 return false;
60 }
61 if (offset > std::numeric_limits<off64_t>::max() - byte_count) {
62 return false;
63 }
64 if (offset + byte_count > str_.size()) {
65 return false;
66 }
67 memcpy(data, str_.data() + offset, byte_count);
68 return true;
69 }
70
StringOutputStream(std::string * str,size_t buffer_capacity)71 StringOutputStream::StringOutputStream(std::string* str, size_t buffer_capacity)
72 : str_(str),
73 buffer_capacity_(buffer_capacity),
74 buffer_offset_(0u),
75 buffer_(new char[buffer_capacity]) {
76 }
77
~StringOutputStream()78 StringOutputStream::~StringOutputStream() {
79 Flush();
80 }
81
Next(void ** data,size_t * size)82 bool StringOutputStream::Next(void** data, size_t* size) {
83 if (buffer_offset_ == buffer_capacity_) {
84 FlushImpl();
85 }
86
87 *data = buffer_.get() + buffer_offset_;
88 *size = buffer_capacity_ - buffer_offset_;
89 buffer_offset_ = buffer_capacity_;
90 return true;
91 }
92
BackUp(size_t count)93 void StringOutputStream::BackUp(size_t count) {
94 if (count > buffer_offset_) {
95 buffer_offset_ = 0u;
96 } else {
97 buffer_offset_ -= count;
98 }
99 }
100
ByteCount() const101 size_t StringOutputStream::ByteCount() const {
102 return str_->size() + buffer_offset_;
103 }
104
Flush()105 void StringOutputStream::Flush() {
106 if (buffer_offset_ != 0u) {
107 FlushImpl();
108 }
109 }
110
FlushImpl()111 void StringOutputStream::FlushImpl() {
112 str_->append(buffer_.get(), buffer_offset_);
113 buffer_offset_ = 0u;
114 }
115
116 } // namespace io
117 } // namespace aapt
118