Audio plugin host https://kx.studio/carla
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.

123 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_FILEOUTPUTSTREAM_H_INCLUDED
  24. #define JUCE_FILEOUTPUTSTREAM_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. An output stream that writes into a local file.
  28. @see OutputStream, FileInputStream, File::createOutputStream
  29. */
  30. class JUCE_API FileOutputStream : public OutputStream
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates a FileOutputStream.
  35. If the file doesn't exist, it will first be created. If the file can't be
  36. created or opened (for example, because the parent directory of the file
  37. does not exist), the failedToOpen() method will return true.
  38. If the file already exists when opened, the stream's write-position will
  39. be set to the end of the file. To overwrite an existing file,
  40. use File::deleteFile() before opening the stream, or use setPosition(0)
  41. after it's opened (although this won't truncate the file).
  42. Destroying a FileOutputStream object does not force the operating system
  43. to write the buffered data to disk immediately. If this is required you
  44. should call flush() before triggering the destructor.
  45. @see TemporaryFile
  46. */
  47. FileOutputStream (const File& fileToWriteTo,
  48. size_t bufferSizeToUse = 16384);
  49. /** Destructor. */
  50. ~FileOutputStream();
  51. //==============================================================================
  52. /** Returns the file that this stream is writing to.
  53. */
  54. const File& getFile() const { return file; }
  55. /** Returns the status of the file stream.
  56. The result will be ok if the file opened successfully. If an error occurs while
  57. opening or writing to the file, this will contain an error message.
  58. */
  59. const Result& getStatus() const noexcept { return status; }
  60. /** Returns true if the stream couldn't be opened for some reason.
  61. @see getResult()
  62. */
  63. bool failedToOpen() const noexcept { return status.failed(); }
  64. /** Returns true if the stream opened without problems.
  65. @see getResult()
  66. */
  67. bool openedOk() const noexcept { return status.wasOk(); }
  68. /** Attempts to truncate the file to the current write position.
  69. To truncate a file to a specific size, first use setPosition() to seek to the
  70. appropriate location, and then call this method.
  71. */
  72. Result truncate();
  73. //==============================================================================
  74. void flush() override;
  75. int64 getPosition() override;
  76. bool setPosition (int64) override;
  77. bool write (const void*, size_t) override;
  78. bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
  79. private:
  80. //==============================================================================
  81. File file;
  82. void* fileHandle;
  83. Result status;
  84. int64 currentPosition;
  85. size_t bufferSize, bytesInBuffer;
  86. HeapBlock<char> buffer;
  87. void openHandle();
  88. void closeHandle();
  89. void flushInternal();
  90. bool flushBuffer();
  91. int64 setPositionInternal (int64);
  92. ssize_t writeInternal (const void*, size_t);
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileOutputStream)
  94. };
  95. #endif // JUCE_FILEOUTPUTSTREAM_H_INCLUDED