FileInput.h 2.2 KB

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