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.

131 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
  21. : file (f),
  22. bufferSize (bufferSizeToUse),
  23. buffer (jmax (bufferSizeToUse, (size_t) 16))
  24. {
  25. openHandle();
  26. }
  27. FileOutputStream::~FileOutputStream()
  28. {
  29. flushBuffer();
  30. closeHandle();
  31. }
  32. int64 FileOutputStream::getPosition()
  33. {
  34. return currentPosition;
  35. }
  36. bool FileOutputStream::setPosition (int64 newPosition)
  37. {
  38. if (newPosition != currentPosition)
  39. {
  40. flushBuffer();
  41. currentPosition = juce_fileSetPosition (fileHandle, newPosition);
  42. }
  43. return newPosition == currentPosition;
  44. }
  45. bool FileOutputStream::flushBuffer()
  46. {
  47. bool ok = true;
  48. if (bytesInBuffer > 0)
  49. {
  50. ok = (writeInternal (buffer, bytesInBuffer) == (ssize_t) bytesInBuffer);
  51. bytesInBuffer = 0;
  52. }
  53. return ok;
  54. }
  55. void FileOutputStream::flush()
  56. {
  57. flushBuffer();
  58. flushInternal();
  59. }
  60. bool FileOutputStream::write (const void* const src, const size_t numBytes)
  61. {
  62. jassert (src != nullptr && ((ssize_t) numBytes) >= 0);
  63. if (! openedOk())
  64. return false;
  65. if (bytesInBuffer + numBytes < bufferSize)
  66. {
  67. memcpy (buffer + bytesInBuffer, src, numBytes);
  68. bytesInBuffer += numBytes;
  69. currentPosition += (int64) numBytes;
  70. }
  71. else
  72. {
  73. if (! flushBuffer())
  74. return false;
  75. if (numBytes < bufferSize)
  76. {
  77. memcpy (buffer + bytesInBuffer, src, numBytes);
  78. bytesInBuffer += numBytes;
  79. currentPosition += (int64) numBytes;
  80. }
  81. else
  82. {
  83. auto bytesWritten = writeInternal (src, numBytes);
  84. if (bytesWritten < 0)
  85. return false;
  86. currentPosition += (int64) bytesWritten;
  87. return bytesWritten == (ssize_t) numBytes;
  88. }
  89. }
  90. return true;
  91. }
  92. bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
  93. {
  94. jassert (((ssize_t) numBytes) >= 0);
  95. if (bytesInBuffer + numBytes < bufferSize)
  96. {
  97. memset (buffer + bytesInBuffer, byte, numBytes);
  98. bytesInBuffer += numBytes;
  99. currentPosition += (int64) numBytes;
  100. return true;
  101. }
  102. return OutputStream::writeRepeatedByte (byte, numBytes);
  103. }
  104. } // namespace juce