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/BigBufferStream.h"
18 
19 namespace aapt {
20 namespace io {
21 
22 //
23 // BigBufferInputStream
24 //
25 
Next(const void ** data,size_t * size)26 bool 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 
BackUp(size_t count)46 void 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 
CanRewind() const56 bool BigBufferInputStream::CanRewind() const {
57   return true;
58 }
59 
Rewind()60 bool BigBufferInputStream::Rewind() {
61   iter_ = buffer_->begin();
62   offset_ = 0;
63   bytes_read_ = 0;
64   return true;
65 }
66 
ByteCount() const67 size_t BigBufferInputStream::ByteCount() const {
68   return bytes_read_;
69 }
70 
HadError() const71 bool BigBufferInputStream::HadError() const {
72   return false;
73 }
74 
TotalSize() const75 size_t BigBufferInputStream::TotalSize() const {
76   return buffer_->size();
77 }
78 
79 //
80 // BigBufferOutputStream
81 //
82 
Next(void ** data,size_t * size)83 bool BigBufferOutputStream::Next(void** data, size_t* size) {
84   *data = buffer_->NextBlock(size);
85   return true;
86 }
87 
BackUp(size_t count)88 void BigBufferOutputStream::BackUp(size_t count) {
89   buffer_->BackUp(count);
90 }
91 
ByteCount() const92 size_t BigBufferOutputStream::ByteCount() const {
93   return buffer_->size();
94 }
95 
HadError() const96 bool BigBufferOutputStream::HadError() const {
97   return false;
98 }
99 
100 }  // namespace io
101 }  // namespace aapt
102