blob: 9704caae4719f5f8210ffcd74c180cc22bb64784 [file] [log] [blame]
Adam Lesinski06460ef2017-03-14 18:52:13 -07001/*
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
Adam Lesinski00451162017-10-03 07:44:08 -070017#include "io/BigBufferStream.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070018
19namespace aapt {
20namespace io {
21
22//
23// BigBufferInputStream
24//
25
26bool BigBufferInputStream::Next(const void** data, size_t* size) {
27 if (iter_ == buffer_->end()) {
28 return false;
29 }
30
31 if (offset_ == iter_->size) {
32 ++iter_;
33 if (iter_ == buffer_->end()) {
34 return false;
35 }
36 offset_ = 0;
37 }
38
39 *data = iter_->buffer.get() + offset_;
40 *size = iter_->size - offset_;
41 bytes_read_ += iter_->size - offset_;
42 offset_ = iter_->size;
43 return true;
44}
45
46void BigBufferInputStream::BackUp(size_t count) {
47 if (count > offset_) {
48 bytes_read_ -= offset_;
49 offset_ = 0;
50 } else {
51 offset_ -= count;
52 bytes_read_ -= count;
53 }
54}
55
Adam Lesinski00451162017-10-03 07:44:08 -070056bool BigBufferInputStream::CanRewind() const {
57 return true;
58}
Adam Lesinski06460ef2017-03-14 18:52:13 -070059
60bool BigBufferInputStream::Rewind() {
61 iter_ = buffer_->begin();
62 offset_ = 0;
63 bytes_read_ = 0;
64 return true;
65}
66
Adam Lesinski00451162017-10-03 07:44:08 -070067size_t BigBufferInputStream::ByteCount() const {
68 return bytes_read_;
69}
Adam Lesinski06460ef2017-03-14 18:52:13 -070070
Adam Lesinski00451162017-10-03 07:44:08 -070071bool BigBufferInputStream::HadError() const {
72 return false;
73}
74
75size_t BigBufferInputStream::TotalSize() const {
76 return buffer_->size();
77}
Adam Lesinski06460ef2017-03-14 18:52:13 -070078
79//
80// BigBufferOutputStream
81//
82
83bool BigBufferOutputStream::Next(void** data, size_t* size) {
84 *data = buffer_->NextBlock(size);
85 return true;
86}
87
Adam Lesinski00451162017-10-03 07:44:08 -070088void BigBufferOutputStream::BackUp(size_t count) {
89 buffer_->BackUp(count);
90}
Adam Lesinski06460ef2017-03-14 18:52:13 -070091
Adam Lesinski00451162017-10-03 07:44:08 -070092size_t BigBufferOutputStream::ByteCount() const {
93 return buffer_->size();
94}
Adam Lesinski06460ef2017-03-14 18:52:13 -070095
Adam Lesinski00451162017-10-03 07:44:08 -070096bool BigBufferOutputStream::HadError() const {
97 return false;
98}
Adam Lesinski06460ef2017-03-14 18:52:13 -070099
100} // namespace io
101} // namespace aapt