juce_FileOutputStream.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, you can truncate
  35. it like this:
  36. @code
  37. FileOutputStream stream (file);
  38. if (stream.openedOk())
  39. {
  40. stream.setPosition (0);
  41. stream.truncate();
  42. ...
  43. }
  44. @endcode
  45. Destroying a FileOutputStream object does not force the operating system
  46. to write the buffered data to disk immediately. If this is required you
  47. should call flush() before triggering the destructor.
  48. @see TemporaryFile
  49. */
  50. FileOutputStream (const File& fileToWriteTo,
  51. size_t bufferSizeToUse = 16384);
  52. /** Destructor. */
  53. ~FileOutputStream() override;
  54. //==============================================================================
  55. /** Returns the file that this stream is writing to.
  56. */
  57. const File& getFile() const { return file; }
  58. /** Returns the status of the file stream.
  59. The result will be ok if the file opened successfully. If an error occurs while
  60. opening or writing to the file, this will contain an error message.
  61. */
  62. const Result& getStatus() const noexcept { return status; }
  63. /** Returns true if the stream couldn't be opened for some reason.
  64. @see getResult()
  65. */
  66. bool failedToOpen() const noexcept { return status.failed(); }
  67. /** Returns true if the stream opened without problems.
  68. @see getResult()
  69. */
  70. bool openedOk() const noexcept { return status.wasOk(); }
  71. /** Attempts to truncate the file to the current write position.
  72. To truncate a file to a specific size, first use setPosition() to seek to the
  73. appropriate location, and then call this method.
  74. */
  75. Result truncate();
  76. //==============================================================================
  77. void flush() override;
  78. int64 getPosition() override;
  79. bool setPosition (int64) override;
  80. bool write (const void*, size_t) override;
  81. bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
  82. private:
  83. //==============================================================================
  84. File file;
  85. void* fileHandle = nullptr;
  86. Result status { Result::ok() };
  87. int64 currentPosition = 0;
  88. size_t bufferSize, bytesInBuffer = 0;
  89. HeapBlock<char> buffer;
  90. void openHandle();
  91. void closeHandle();
  92. void flushInternal();
  93. bool flushBuffer();
  94. int64 setPositionInternal (int64);
  95. ssize_t writeInternal (const void*, size_t);
  96. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileOutputStream)
  97. };
  98. } // namespace juce