Debian Codegraph @

tools/aapt2/io/FileStream.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 #ifndef AAPT_IO_FILESTREAM_H
18 #define AAPT_IO_FILESTREAM_H
19
20 #include "io/Io.h"
21
22 #include <memory>
23 #include <string>
•24 #include <stdint.h>
25
26 #include "android-base/macros.h"
27 #include "android-base/unique_fd.h"
28
29 namespace aapt {
30 namespace io {
31
32 constexpr size_t kDefaultBufferCapacity = 4096u;
33
34 class FileInputStream : public InputStream {
35 public:
36 explicit FileInputStream(const std::string& path,
37 size_t buffer_capacity = kDefaultBufferCapacity);
38
39 // Take ownership of `fd`.
40 explicit FileInputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
41
42 bool Next(const void** data, size_t* size) override;
43
44 void BackUp(size_t count) override;
45
46 size_t ByteCount() const override;
47
48 bool HadError() const override;
49
50 std::string GetError() const override;
51
52 private:
53 DISALLOW_COPY_AND_ASSIGN(FileInputStream);
54
55 android::base::unique_fd fd_;
56 std::string error_;
57 std::unique_ptr<uint8_t[]> buffer_;
58 size_t buffer_capacity_ = 0u;
59 size_t buffer_offset_ = 0u;
60 size_t buffer_size_ = 0u;
61 size_t total_byte_count_ = 0u;
62 };
63
64 class FileOutputStream : public OutputStream {
65 public:
66 explicit FileOutputStream(const std::string& path,
67 size_t buffer_capacity = kDefaultBufferCapacity);
68
69 // Does not take ownership of `fd`.
70 explicit FileOutputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
71
72 // Takes ownership of `fd`.
73 explicit FileOutputStream(android::base::unique_fd fd,
74 size_t buffer_capacity = kDefaultBufferCapacity);
75
76 ~FileOutputStream();
77
78 bool Next(void** data, size_t* size) override;
79
80 // Immediately flushes out the contents of the buffer to disk.
81 bool Flush();
82
83 void BackUp(size_t count) override;
84
85 size_t ByteCount() const override;
86
87 bool HadError() const override;
88
89 std::string GetError() const override;
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(FileOutputStream);
93
94 bool FlushImpl();
95
96 android::base::unique_fd owned_fd_;
97 int fd_;
98 std::string error_;
99 std::unique_ptr<uint8_t[]> buffer_;
100 size_t buffer_capacity_ = 0u;
101 size_t buffer_offset_ = 0u;
102 size_t total_byte_count_ = 0u;
103 };
104
105 } // namespace io
106 } // namespace aapt
107
108 #endif // AAPT_IO_FILESTREAM_H