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.

134 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. int64 juce_fileSetPosition (void* handle, int64 pos);
  19. //==============================================================================
  20. FileOutputStream::FileOutputStream (const File& f, const int bufferSize_)
  21. : file (f),
  22. fileHandle (nullptr),
  23. status (Result::ok()),
  24. currentPosition (0),
  25. bufferSize (bufferSize_),
  26. bytesInBuffer (0),
  27. buffer ((size_t) jmax (bufferSize_, 16))
  28. {
  29. openHandle();
  30. }
  31. FileOutputStream::~FileOutputStream()
  32. {
  33. flushBuffer();
  34. flushInternal();
  35. closeHandle();
  36. }
  37. int64 FileOutputStream::getPosition()
  38. {
  39. return currentPosition;
  40. }
  41. bool FileOutputStream::setPosition (int64 newPosition)
  42. {
  43. if (newPosition != currentPosition)
  44. {
  45. flushBuffer();
  46. currentPosition = juce_fileSetPosition (fileHandle, newPosition);
  47. }
  48. return newPosition == currentPosition;
  49. }
  50. bool FileOutputStream::flushBuffer()
  51. {
  52. bool ok = true;
  53. if (bytesInBuffer > 0)
  54. {
  55. ok = (writeInternal (buffer, bytesInBuffer) == bytesInBuffer);
  56. bytesInBuffer = 0;
  57. }
  58. return ok;
  59. }
  60. void FileOutputStream::flush()
  61. {
  62. flushBuffer();
  63. flushInternal();
  64. }
  65. bool FileOutputStream::write (const void* const src, const int numBytes)
  66. {
  67. jassert (src != nullptr && numBytes >= 0);
  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. jassert (numBytes >= 0);
  98. if (bytesInBuffer + numBytes < bufferSize)
  99. {
  100. memset (buffer + bytesInBuffer, byte, (size_t) numBytes);
  101. bytesInBuffer += numBytes;
  102. currentPosition += numBytes;
  103. }
  104. else
  105. {
  106. OutputStream::writeRepeatedByte (byte, numBytes);
  107. }
  108. }