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.

117 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_FILEOUTPUTSTREAM_JUCEHEADER__
  19. #define __JUCE_FILEOUTPUTSTREAM_JUCEHEADER__
  20. #include "juce_File.h"
  21. #include "../streams/juce_OutputStream.h"
  22. //==============================================================================
  23. /**
  24. An output stream that writes into a local file.
  25. @see OutputStream, FileInputStream, File::createOutputStream
  26. */
  27. class JUCE_API FileOutputStream : public OutputStream
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a FileOutputStream.
  32. If the file doesn't exist, it will first be created. If the file can't be
  33. created or opened, the failedToOpen() method will return
  34. true.
  35. If the file already exists when opened, the stream's write-postion will
  36. be set to the end of the file. To overwrite an existing file,
  37. use File::deleteFile() before opening the stream, or use setPosition(0)
  38. after it's opened (although this won't truncate the file).
  39. @see TemporaryFile
  40. */
  41. FileOutputStream (const File& fileToWriteTo,
  42. int bufferSizeToUse = 16384);
  43. /** Destructor. */
  44. ~FileOutputStream();
  45. //==============================================================================
  46. /** Returns the file that this stream is writing to.
  47. */
  48. const File& getFile() const { return file; }
  49. /** Returns the status of the file stream.
  50. The result will be ok if the file opened successfully. If an error occurs while
  51. opening or writing to the file, this will contain an error message.
  52. */
  53. const Result& getStatus() const noexcept { return status; }
  54. /** Returns true if the stream couldn't be opened for some reason.
  55. @see getResult()
  56. */
  57. bool failedToOpen() const noexcept { return status.failed(); }
  58. /** Returns true if the stream opened without problems.
  59. @see getResult()
  60. */
  61. bool openedOk() const noexcept { return status.wasOk(); }
  62. /** Attempts to truncate the file to the current write position.
  63. To truncate a file to a specific size, first use setPosition() to seek to the
  64. appropriate location, and then call this method.
  65. */
  66. Result truncate();
  67. //==============================================================================
  68. void flush();
  69. int64 getPosition();
  70. bool setPosition (int64 pos);
  71. bool write (const void* data, int numBytes);
  72. void writeRepeatedByte (uint8 byte, int numTimesToRepeat);
  73. private:
  74. //==============================================================================
  75. File file;
  76. void* fileHandle;
  77. Result status;
  78. int64 currentPosition;
  79. int bufferSize, bytesInBuffer;
  80. HeapBlock <char> buffer;
  81. void openHandle();
  82. void closeHandle();
  83. void flushInternal();
  84. bool flushBuffer();
  85. int64 setPositionInternal (int64 newPosition);
  86. int writeInternal (const void* data, int numBytes);
  87. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileOutputStream);
  88. };
  89. #endif // __JUCE_FILEOUTPUTSTREAM_JUCEHEADER__