blob: f5c5737cb1492762b52558b976e2d311ba7a85d6 [file] [log] [blame]
Adam Lesinski21efb682016-09-14 17:35:43 -07001/*
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 Lesinski21efb682016-09-14 17:35:43 -070019#include <cstring>
20
21namespace aapt {
22namespace io {
23
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024bool Copy(OutputStream* out, InputStream* in) {
25 const void* in_buffer;
Adam Lesinski06460ef2017-03-14 18:52:13 -070026 size_t in_len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027 while (in->Next(&in_buffer, &in_len)) {
28 void* out_buffer;
Adam Lesinski06460ef2017-03-14 18:52:13 -070029 size_t out_len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 if (!out->Next(&out_buffer, &out_len)) {
31 return !out->HadError();
Adam Lesinski21efb682016-09-14 17:35:43 -070032 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033
Adam Lesinski06460ef2017-03-14 18:52:13 -070034 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 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 Lesinski21efb682016-09-14 17:35:43 -070040}
41
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042} // namespace io
43} // namespace aapt