juce_FileInputStream.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_FILEINPUTSTREAM_H_INCLUDED
  22. #define JUCE_FILEINPUTSTREAM_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. An input stream that reads from a local file.
  26. @see InputStream, FileOutputStream, File::createInputStream
  27. */
  28. class JUCE_API FileInputStream : public InputStream
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a FileInputStream to read from the given file.
  33. After creating a FileInputStream, you should use openedOk() or failedToOpen()
  34. to make sure that it's OK before trying to read from it! If it failed, you
  35. can call getStatus() to get more error information.
  36. */
  37. explicit FileInputStream (const File& fileToRead);
  38. /** Destructor. */
  39. ~FileInputStream();
  40. //==============================================================================
  41. /** Returns the file that this stream is reading from. */
  42. const File& getFile() const noexcept { return file; }
  43. /** Returns the status of the file stream.
  44. The result will be ok if the file opened successfully. If an error occurs while
  45. opening or reading from the file, this will contain an error message.
  46. */
  47. const Result& getStatus() const noexcept { return status; }
  48. /** Returns true if the stream couldn't be opened for some reason.
  49. @see getResult()
  50. */
  51. bool failedToOpen() const noexcept { return status.failed(); }
  52. /** Returns true if the stream opened without problems.
  53. @see getResult()
  54. */
  55. bool openedOk() const noexcept { return status.wasOk(); }
  56. //==============================================================================
  57. int64 getTotalLength() override;
  58. int read (void*, int) override;
  59. bool isExhausted() override;
  60. int64 getPosition() override;
  61. bool setPosition (int64) override;
  62. private:
  63. //==============================================================================
  64. const File file;
  65. void* fileHandle;
  66. int64 currentPosition;
  67. Result status;
  68. void openHandle();
  69. size_t readInternal (void*, size_t);
  70. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileInputStream)
  71. };
  72. #endif // JUCE_FILEINPUTSTREAM_H_INCLUDED