blob: 1e48508bb4b24719777d9a9fea138b7bb25d9bd2 [file] [log] [blame]
Adam Lesinskid0f492d2017-04-03 18:12:45 -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#ifndef AAPT_IO_UTIL_H
18#define AAPT_IO_UTIL_H
19
20#include <string>
21
22#include "google/protobuf/message_lite.h"
23
Adam Lesinski46708052017-09-29 14:49:15 -070024#include "format/Archive.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070025#include "io/File.h"
26#include "io/Io.h"
27#include "process/IResourceTableConsumer.h"
28
29namespace aapt {
30namespace io {
31
32bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, const std::string& out_path,
33 uint32_t compression_flags, IArchiveWriter* writer);
34
35bool CopyFileToArchive(IAaptContext* context, IFile* file, const std::string& out_path,
36 uint32_t compression_flags, IArchiveWriter* writer);
37
38bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::MessageLite* proto_msg,
39 const std::string& out_path, uint32_t compression_flags,
40 IArchiveWriter* writer);
41
42// Copies the data from in to out. Returns false if there was an error.
43// If there was an error, check the individual streams' HadError/GetError methods.
44bool Copy(OutputStream* out, InputStream* in);
Adam Lesinski00451162017-10-03 07:44:08 -070045bool Copy(::google::protobuf::io::ZeroCopyOutputStream* out, InputStream* in);
46
47class OutputStreamAdaptor : public io::OutputStream {
48 public:
49 explicit OutputStreamAdaptor(::google::protobuf::io::ZeroCopyOutputStream* out) : out_(out) {
50 }
51
52 bool Next(void** data, size_t* size) override {
53 int out_size;
54 bool result = out_->Next(data, &out_size);
55 *size = static_cast<size_t>(out_size);
56 if (!result) {
57 error_ocurred_ = true;
58 }
59 return result;
60 }
61
62 void BackUp(size_t count) override {
63 out_->BackUp(static_cast<int>(count));
64 }
65
66 size_t ByteCount() const override {
67 return static_cast<size_t>(out_->ByteCount());
68 }
69
70 bool HadError() const override {
71 return error_ocurred_;
72 }
73
74 private:
75 DISALLOW_COPY_AND_ASSIGN(OutputStreamAdaptor);
76
77 ::google::protobuf::io::ZeroCopyOutputStream* out_;
78 bool error_ocurred_ = false;
79};
80
81class ZeroCopyInputAdaptor : public ::google::protobuf::io::ZeroCopyInputStream {
82 public:
83 explicit ZeroCopyInputAdaptor(io::InputStream* in) : in_(in) {
84 }
85
86 bool Next(const void** data, int* size) override {
87 size_t out_size;
88 bool result = in_->Next(data, &out_size);
89 *size = static_cast<int>(out_size);
90 return result;
91 }
92
93 void BackUp(int count) override {
94 in_->BackUp(static_cast<size_t>(count));
95 }
96
97 bool Skip(int count) override {
98 const void* data;
99 int size;
100 while (Next(&data, &size)) {
101 if (size > count) {
102 BackUp(size - count);
103 return true;
104 } else {
105 count -= size;
106 }
107 }
108 return false;
109 }
110
111 ::google::protobuf::int64 ByteCount() const override {
112 return static_cast<::google::protobuf::int64>(in_->ByteCount());
113 }
114
115 private:
116 DISALLOW_COPY_AND_ASSIGN(ZeroCopyInputAdaptor);
117
118 io::InputStream* in_;
119};
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700120
121} // namespace io
122} // namespace aapt
123
124#endif /* AAPT_IO_UTIL_H */