blob: 6beb9a186ce5e16c04a7d717c96526c9cbc6ee39 [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
17#ifndef AAPT_IO_FILEINPUTSTREAM_H
18#define AAPT_IO_FILEINPUTSTREAM_H
19
20#include "io/Io.h"
21
22#include <memory>
23#include <string>
24
25#include "android-base/macros.h"
26#include "android-base/unique_fd.h"
27
28namespace aapt {
29namespace io {
30
31class FileInputStream : public InputStream {
32 public:
33 explicit FileInputStream(const std::string& path, size_t buffer_capacity = 4096);
34
35 // Takes ownership of `fd`.
36 explicit FileInputStream(int fd, size_t buffer_capacity = 4096);
37
38 bool Next(const void** data, size_t* size) override;
39
40 void BackUp(size_t count) override;
41
42 size_t ByteCount() const override;
43
44 bool HadError() const override;
45
46 std::string GetError() const override;
47
48 private:
49 DISALLOW_COPY_AND_ASSIGN(FileInputStream);
50
51 android::base::unique_fd fd_;
52 std::string error_;
53 std::unique_ptr<uint8_t[]> buffer_;
54 size_t buffer_capacity_;
55 size_t buffer_offset_;
56 size_t buffer_size_;
57 size_t total_byte_count_;
58};
59
60} // namespace io
61} // namespace aapt
62
63#endif // AAPT_IO_FILEINPUTSTREAM_H