Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 17 | #include "io/BigBufferStream.h" |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace aapt { |
| 20 | namespace io { |
| 21 | |
| 22 | // |
| 23 | // BigBufferInputStream |
| 24 | // |
| 25 | |
| 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 | |
| 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 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 56 | bool BigBufferInputStream::CanRewind() const { |
| 57 | return true; |
| 58 | } |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 59 | |
| 60 | bool BigBufferInputStream::Rewind() { |
| 61 | iter_ = buffer_->begin(); |
| 62 | offset_ = 0; |
| 63 | bytes_read_ = 0; |
| 64 | return true; |
| 65 | } |
| 66 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 67 | size_t BigBufferInputStream::ByteCount() const { |
| 68 | return bytes_read_; |
| 69 | } |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 71 | bool BigBufferInputStream::HadError() const { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | size_t BigBufferInputStream::TotalSize() const { |
| 76 | return buffer_->size(); |
| 77 | } |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 78 | |
| 79 | // |
| 80 | // BigBufferOutputStream |
| 81 | // |
| 82 | |
| 83 | bool BigBufferOutputStream::Next(void** data, size_t* size) { |
| 84 | *data = buffer_->NextBlock(size); |
| 85 | return true; |
| 86 | } |
| 87 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 88 | void BigBufferOutputStream::BackUp(size_t count) { |
| 89 | buffer_->BackUp(count); |
| 90 | } |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 92 | size_t BigBufferOutputStream::ByteCount() const { |
| 93 | return buffer_->size(); |
| 94 | } |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 96 | bool BigBufferOutputStream::HadError() const { |
| 97 | return false; |
| 98 | } |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 99 | |
| 100 | } // namespace io |
| 101 | } // namespace aapt |