The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.3KB

  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 output stream that writes into a local file.
  22. @see OutputStream, FileInputStream, File::createOutputStream
  23. @tags{Core}
  24. */
  25. class JUCE_API FileOutputStream : public OutputStream
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a FileOutputStream.
  30. If the file doesn't exist, it will first be created. If the file can't be
  31. created or opened (for example, because the parent directory of the file
  32. does not exist), the failedToOpen() method will return true.
  33. If the file already exists when opened, the stream's write-position will
  34. be set to the end of the file. To overwrite an existing file,
  35. use File::deleteFile() before opening the stream, or use setPosition(0)
  36. after it's opened (although this won't truncate the file).
  37. Destroying a FileOutputStream object does not force the operating system
  38. to write the buffered data to disk immediately. If this is required you
  39. should call flush() before triggering the destructor.
  40. @see TemporaryFile
  41. */
  42. FileOutputStream (const File& fileToWriteTo,
  43. size_t bufferSizeToUse = 16384);
  44. /** Destructor. */
  45. ~FileOutputStream();
  46. //==============================================================================
  47. /** Returns the file that this stream is writing to.
  48. */
  49. const File& getFile() const { return file; }
  50. /** Returns the status of the file stream.
  51. The result will be ok if the file opened successfully. If an error occurs while
  52. opening or writing to the file, this will contain an error message.
  53. */
  54. const Result& getStatus() const noexcept { return status; }
  55. /** Returns true if the stream couldn't be opened for some reason.
  56. @see getResult()
  57. */
  58. bool failedToOpen() const noexcept { return status.failed(); }
  59. /** Returns true if the stream opened without problems.
  60. @see getResult()
  61. */
  62. bool openedOk() const noexcept { return status.wasOk(); }
  63. /** Attempts to truncate the file to the current write position.
  64. To truncate a file to a specific size, first use setPosition() to seek to the
  65. appropriate location, and then call this method.
  66. */
  67. Result truncate();
  68. //==============================================================================
  69. void flush() override;
  70. int64 getPosition() override;
  71. bool setPosition (int64) override;
  72. bool write (const void*, size_t) override;
  73. bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
  74. private:
  75. //==============================================================================
  76. File file;
  77. void* fileHandle = nullptr;
  78. Result status { Result::ok() };
  79. int64 currentPosition = 0;
  80. size_t bufferSize, bytesInBuffer = 0;
  81. HeapBlock<char> buffer;
  82. void openHandle();
  83. void closeHandle();
  84. void flushInternal();
  85. bool flushBuffer();
  86. int64 setPositionInternal (int64);
  87. ssize_t writeInternal (const void*, size_t);
  88. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileOutputStream)
  89. };
  90. } // namespace juce