blob: 3b0766750eae8088f542808830b3ca86124af181 [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
32class FileInputStream : public InputStream {
33 public:
34 explicit FileInputStream(const std::string& path, size_t buffer_capacity = 4096);
35
36 // Takes ownership of `fd`.
37 explicit FileInputStream(int fd, size_t buffer_capacity = 4096);
38
39 bool Next(const void** data, size_t* size) override;
40
41 void BackUp(size_t count) override;
42
43 size_t ByteCount() const override;
44
45 bool HadError() const override;
46
47 std::string GetError() const override;
48
49 private:
50 DISALLOW_COPY_AND_ASSIGN(FileInputStream);
51
52 android::base::unique_fd fd_;
53 std::string error_;
54 std::unique_ptr<uint8_t[]> buffer_;
55 size_t buffer_capacity_;
56 size_t buffer_offset_;
57 size_t buffer_size_;
58 size_t total_byte_count_;
59};
60
Adam Lesinski00451162017-10-03 07:44:08 -070061class FileOutputStream : public OutputStream {
62 public:
63 explicit FileOutputStream(const std::string& path, int mode = O_RDWR | O_CREAT | O_BINARY,
64 size_t buffer_capacity = 4096);
65
66 // Takes ownership of `fd`.
67 explicit FileOutputStream(int fd, size_t buffer_capacity = 4096);
68
69 ~FileOutputStream();
70
71 bool Next(void** data, size_t* size) override;
72
73 // Immediately flushes out the contents of the buffer to disk.
74 bool Flush();
75
76 void BackUp(size_t count) override;
77
78 size_t ByteCount() const override;
79
80 bool HadError() const override;
81
82 std::string GetError() const override;
83
84 private:
85 DISALLOW_COPY_AND_ASSIGN(FileOutputStream);
86
87 bool FlushImpl();
88
89 android::base::unique_fd fd_;
90 std::string error_;
91 std::unique_ptr<uint8_t[]> buffer_;
92 size_t buffer_capacity_;
93 size_t buffer_offset_;
94 size_t total_byte_count_;
95};
96
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070097} // namespace io
98} // namespace aapt
99
Adam Lesinski00451162017-10-03 07:44:08 -0700100#endif // AAPT_IO_FILESTREAM_H