blob: 4ed1ad556f107de11f821424fc996e2ced3206fe [file] [log] [blame]
Adam Lesinskiefeb7af2017-08-02 14:57:43 -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
Adam Lesinski00451162017-10-03 07:44:08 -070017#ifndef AAPT_IO_FILESTREAM_H
18#define AAPT_IO_FILESTREAM_H
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070019
20#include "io/Io.h"
21
22#include <memory>
23#include <string>
24
Adam Lesinski00451162017-10-03 07:44:08 -070025#include "android-base/file.h" // for O_BINARY
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070026#include "android-base/macros.h"
27#include "android-base/unique_fd.h"
28
29namespace aapt {
30namespace io {
31
Adam Lesinski93190b72017-11-03 15:20:17 -070032constexpr size_t kDefaultBufferCapacity = 4096u;
33
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070034class FileInputStream : public InputStream {
35 public:
Adam Lesinski93190b72017-11-03 15:20:17 -070036 explicit FileInputStream(const std::string& path,
37 size_t buffer_capacity = kDefaultBufferCapacity);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070038
Adam Lesinski93190b72017-11-03 15:20:17 -070039 // Take ownership of `fd`.
40 explicit FileInputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070041
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_;
59 size_t buffer_offset_;
60 size_t buffer_size_;
61 size_t total_byte_count_;
62};
63
Adam Lesinski00451162017-10-03 07:44:08 -070064class FileOutputStream : public OutputStream {
65 public:
66 explicit FileOutputStream(const std::string& path, int mode = O_RDWR | O_CREAT | O_BINARY,
Adam Lesinski93190b72017-11-03 15:20:17 -070067 size_t buffer_capacity = kDefaultBufferCapacity);
68
69 // Does not take ownership of `fd`.
70 explicit FileOutputStream(int fd, size_t buffer_capacity = kDefaultBufferCapacity);
Adam Lesinski00451162017-10-03 07:44:08 -070071
72 // Takes ownership of `fd`.
Adam Lesinski93190b72017-11-03 15:20:17 -070073 explicit FileOutputStream(android::base::unique_fd fd,
74 size_t buffer_capacity = kDefaultBufferCapacity);
Adam Lesinski00451162017-10-03 07:44:08 -070075
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
Adam Lesinski93190b72017-11-03 15:20:17 -070096 android::base::unique_fd owned_fd_;
97 int fd_;
Adam Lesinski00451162017-10-03 07:44:08 -070098 std::string error_;
99 std::unique_ptr<uint8_t[]> buffer_;
100 size_t buffer_capacity_;
101 size_t buffer_offset_;
102 size_t total_byte_count_;
103};
104
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700105} // namespace io
106} // namespace aapt
107
Adam Lesinski00451162017-10-03 07:44:08 -0700108#endif // AAPT_IO_FILESTREAM_H