juce_FileOutputStream.cpp 3.4 KB

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