FileInput.h revision a0b40ae71d251a40161881780bc317c025d7678f
1/*
2 * Copyright 2014 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 IMG_UTILS_FILE_INPUT_H
18#define IMG_UTILS_FILE_INPUT_H
19
20#include <img_utils/Input.h>
21
22#include <cutils/compiler.h>
23#include <utils/Errors.h>
24#include <utils/String8.h>
25#include <stdio.h>
26#include <stdint.h>
27
28namespace android {
29namespace img_utils {
30
31/**
32 * Utility class for reading from a file.
33 */
34class ANDROID_API FileInput : public Input {
35    public:
36        /**
37         * Create a file input for the given path.
38         */
39        explicit FileInput(String8 path);
40
41        virtual ~FileInput();
42
43        /**
44         * Open a file descriptor to the path given in the constructor.
45         *
46         * Returns OK on success, or a negative error code.
47         */
48        virtual status_t open();
49
50        /**
51         * Read bytes from the file into the given buffer.  At most, the number
52         * of bytes given in the count argument will be read.  Bytes will be written
53         * into the given buffer starting at the index given in the offset argument.
54         *
55         * Returns the number of bytes read, or NOT_ENOUGH_DATA if at the end of the file.  If an
56         * error has occurred, this will return a negative error code other than NOT_ENOUGH_DATA.
57         */
58        virtual ssize_t read(uint8_t* buf, size_t offset, size_t count);
59
60        /**
61         * Close the file descriptor to the path given in the constructor.
62         *
63         * Returns OK on success, or a negative error code.
64         */
65        virtual status_t close();
66    private:
67        FILE *mFp;
68        String8 mPath;
69        bool mOpen;
70};
71
72} /*namespace img_utils*/
73} /*namespace android*/
74
75
76#endif /*IMG_UTILS_INPUT_H*/
77