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.

137 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. jassert (src != nullptr && numBytes >= 0);
  69. if (bytesInBuffer + numBytes < bufferSize)
  70. {
  71. memcpy (buffer + bytesInBuffer, src, (size_t) numBytes);
  72. bytesInBuffer += numBytes;
  73. currentPosition += numBytes;
  74. }
  75. else
  76. {
  77. if (! flushBuffer())
  78. return false;
  79. if (numBytes < bufferSize)
  80. {
  81. memcpy (buffer + bytesInBuffer, src, (size_t) numBytes);
  82. bytesInBuffer += numBytes;
  83. currentPosition += numBytes;
  84. }
  85. else
  86. {
  87. const int bytesWritten = writeInternal (src, numBytes);
  88. if (bytesWritten < 0)
  89. return false;
  90. currentPosition += bytesWritten;
  91. return bytesWritten == numBytes;
  92. }
  93. }
  94. return true;
  95. }
  96. void FileOutputStream::writeRepeatedByte (uint8 byte, int numBytes)
  97. {
  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. }
  109. END_JUCE_NAMESPACE