juce_FileInputStream.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. An input stream that reads from a local file.
  22. @see InputStream, FileOutputStream, File::createInputStream
  23. @tags{Core}
  24. */
  25. class JUCE_API FileInputStream : public InputStream
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a FileInputStream to read from the given file.
  30. After creating a FileInputStream, you should use openedOk() or failedToOpen()
  31. to make sure that it's OK before trying to read from it! If it failed, you
  32. can call getStatus() to get more error information.
  33. */
  34. explicit FileInputStream (const File& fileToRead);
  35. /** Destructor. */
  36. ~FileInputStream() override;
  37. //==============================================================================
  38. /** Returns the file that this stream is reading from. */
  39. const File& getFile() const noexcept { return file; }
  40. /** Returns the status of the file stream.
  41. The result will be ok if the file opened successfully. If an error occurs while
  42. opening or reading from the file, this will contain an error message.
  43. */
  44. const Result& getStatus() const noexcept { return status; }
  45. /** Returns true if the stream couldn't be opened for some reason.
  46. @see getResult()
  47. */
  48. bool failedToOpen() const noexcept { return status.failed(); }
  49. /** Returns true if the stream opened without problems.
  50. @see getResult()
  51. */
  52. bool openedOk() const noexcept { return status.wasOk(); }
  53. //==============================================================================
  54. int64 getTotalLength() override;
  55. int read (void*, int) override;
  56. bool isExhausted() override;
  57. int64 getPosition() override;
  58. bool setPosition (int64) override;
  59. private:
  60. //==============================================================================
  61. const File file;
  62. void* fileHandle = nullptr;
  63. int64 currentPosition = 0;
  64. Result status { Result::ok() };
  65. void openHandle();
  66. size_t readInternal (void*, size_t);
  67. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileInputStream)
  68. };
  69. } // namespace juce