juce_FileOutputStream.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_FILEOUTPUTSTREAM_H_INCLUDED
  22. #define JUCE_FILEOUTPUTSTREAM_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. An output stream that writes into a local file.
  26. @see OutputStream, FileInputStream, File::createOutputStream
  27. */
  28. class JUCE_API FileOutputStream : public OutputStream
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a FileOutputStream.
  33. If the file doesn't exist, it will first be created. If the file can't be
  34. created or opened, the failedToOpen() method will return
  35. true.
  36. If the file already exists when opened, the stream's write-postion will
  37. be set to the end of the file. To overwrite an existing file,
  38. use File::deleteFile() before opening the stream, or use setPosition(0)
  39. after it's opened (although this won't truncate the file).
  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;
  78. Result status;
  79. int64 currentPosition;
  80. size_t bufferSize, bytesInBuffer;
  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. #endif // JUCE_FILEOUTPUTSTREAM_H_INCLUDED