blob: eb99033e1cbef76194f3a81246514a49fcb0a06d [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
17#include "io/BigBufferInputStream.h"
18#include "io/BigBufferOutputStream.h"
19
20namespace aapt {
21namespace io {
22
23//
24// BigBufferInputStream
25//
26
27bool BigBufferInputStream::Next(const void** data, size_t* size) {
28 if (iter_ == buffer_->end()) {
29 return false;
30 }
31
32 if (offset_ == iter_->size) {
33 ++iter_;
34 if (iter_ == buffer_->end()) {
35 return false;
36 }
37 offset_ = 0;
38 }
39
40 *data = iter_->buffer.get() + offset_;
41 *size = iter_->size - offset_;
42 bytes_read_ += iter_->size - offset_;
43 offset_ = iter_->size;
44 return true;
45}
46
47void BigBufferInputStream::BackUp(size_t count) {
48 if (count > offset_) {
49 bytes_read_ -= offset_;
50 offset_ = 0;
51 } else {
52 offset_ -= count;
53 bytes_read_ -= count;
54 }
55}
56
57bool BigBufferInputStream::CanRewind() const { return true; }
58
59bool BigBufferInputStream::Rewind() {
60 iter_ = buffer_->begin();
61 offset_ = 0;
62 bytes_read_ = 0;
63 return true;
64}
65
66size_t BigBufferInputStream::ByteCount() const { return bytes_read_; }
67
68bool BigBufferInputStream::HadError() const { return false; }
69
70//
71// BigBufferOutputStream
72//
73
74bool BigBufferOutputStream::Next(void** data, size_t* size) {
75 *data = buffer_->NextBlock(size);
76 return true;
77}
78
79void BigBufferOutputStream::BackUp(size_t count) { buffer_->BackUp(count); }
80
81size_t BigBufferOutputStream::ByteCount() const { return buffer_->size(); }
82
83bool BigBufferOutputStream::HadError() const { return false; }
84
85} // namespace io
86} // namespace aapt