Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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/Io.h" |
| 18 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 19 | #include <cstring> |
| 20 | |
| 21 | namespace aapt { |
| 22 | namespace io { |
| 23 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | bool Copy(OutputStream* out, InputStream* in) { |
| 25 | const void* in_buffer; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 26 | size_t in_len; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | while (in->Next(&in_buffer, &in_len)) { |
| 28 | void* out_buffer; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 29 | size_t out_len; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | if (!out->Next(&out_buffer, &out_len)) { |
| 31 | return !out->HadError(); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 32 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 34 | const size_t bytes_to_copy = in_len < out_len ? in_len : out_len; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | memcpy(out_buffer, in_buffer, bytes_to_copy); |
| 36 | out->BackUp(out_len - bytes_to_copy); |
| 37 | in->BackUp(in_len - bytes_to_copy); |
| 38 | } |
| 39 | return !in->HadError(); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | } // namespace io |
| 43 | } // namespace aapt |