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.

FileOutputStream.h 4.7KB

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