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.

135 lines
3.6KB

  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. BEGIN_JUCE_NAMESPACE
  19. int64 juce_fileSetPosition (void* handle, int64 pos);
  20. //==============================================================================
  21. FileOutputStream::FileOutputStream (const File& f, const int bufferSize_)
  22. : file (f),
  23. fileHandle (nullptr),
  24. status (Result::ok()),
  25. currentPosition (0),
  26. bufferSize (bufferSize_),
  27. bytesInBuffer (0),
  28. buffer ((size_t) jmax (bufferSize_, 16))
  29. {
  30. openHandle();
  31. }
  32. FileOutputStream::~FileOutputStream()
  33. {
  34. flushBuffer();
  35. flushInternal();
  36. closeHandle();
  37. }
  38. int64 FileOutputStream::getPosition()
  39. {
  40. return currentPosition;
  41. }
  42. bool FileOutputStream::setPosition (int64 newPosition)
  43. {
  44. if (newPosition != currentPosition)
  45. {
  46. flushBuffer();
  47. currentPosition = juce_fileSetPosition (fileHandle, newPosition);
  48. }
  49. return newPosition == currentPosition;
  50. }
  51. bool FileOutputStream::flushBuffer()
  52. {
  53. bool ok = true;
  54. if (bytesInBuffer > 0)
  55. {
  56. ok = (writeInternal (buffer, bytesInBuffer) == bytesInBuffer);
  57. bytesInBuffer = 0;
  58. }
  59. return ok;
  60. }
  61. void FileOutputStream::flush()
  62. {
  63. flushBuffer();
  64. flushInternal();
  65. }
  66. bool FileOutputStream::write (const void* const src, const int numBytes)
  67. {
  68. if (bytesInBuffer + numBytes < bufferSize)
  69. {
  70. memcpy (buffer + bytesInBuffer, src, (size_t) numBytes);
  71. bytesInBuffer += numBytes;
  72. currentPosition += numBytes;
  73. }
  74. else
  75. {
  76. if (! flushBuffer())
  77. return false;
  78. if (numBytes < bufferSize)
  79. {
  80. memcpy (buffer + bytesInBuffer, src, (size_t) numBytes);
  81. bytesInBuffer += numBytes;
  82. currentPosition += numBytes;
  83. }
  84. else
  85. {
  86. const int bytesWritten = writeInternal (src, numBytes);
  87. if (bytesWritten < 0)
  88. return false;
  89. currentPosition += bytesWritten;
  90. return bytesWritten == numBytes;
  91. }
  92. }
  93. return true;
  94. }
  95. void FileOutputStream::writeRepeatedByte (uint8 byte, int numBytes)
  96. {
  97. if (bytesInBuffer + numBytes < bufferSize)
  98. {
  99. memset (buffer + bytesInBuffer, byte, (size_t) numBytes);
  100. bytesInBuffer += numBytes;
  101. currentPosition += numBytes;
  102. }
  103. else
  104. {
  105. OutputStream::writeRepeatedByte (byte, numBytes);
  106. }
  107. }
  108. END_JUCE_NAMESPACE