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.

178 lines
5.7KB

  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. int64 juce_fileSetPosition (void* handle, int64 pos);
  20. //==============================================================================
  21. FileInputStream::FileInputStream (const File& f) : file (f)
  22. {
  23. openHandle();
  24. }
  25. int64 FileInputStream::getTotalLength()
  26. {
  27. // You should always check that a stream opened successfully before using it!
  28. jassert (openedOk());
  29. return file.getSize();
  30. }
  31. int FileInputStream::read (void* buffer, int bytesToRead)
  32. {
  33. // You should always check that a stream opened successfully before using it!
  34. jassert (openedOk());
  35. // The buffer should never be null, and a negative size is probably a
  36. // sign that something is broken!
  37. jassert (buffer != nullptr && bytesToRead >= 0);
  38. auto num = readInternal (buffer, (size_t) bytesToRead);
  39. currentPosition += (int64) num;
  40. return (int) num;
  41. }
  42. bool FileInputStream::isExhausted()
  43. {
  44. return currentPosition >= getTotalLength();
  45. }
  46. int64 FileInputStream::getPosition()
  47. {
  48. return currentPosition;
  49. }
  50. bool FileInputStream::setPosition (int64 pos)
  51. {
  52. // You should always check that a stream opened successfully before using it!
  53. jassert (openedOk());
  54. if (pos != currentPosition)
  55. currentPosition = juce_fileSetPosition (fileHandle, pos);
  56. return currentPosition == pos;
  57. }
  58. //==============================================================================
  59. //==============================================================================
  60. #if JUCE_UNIT_TESTS
  61. struct FileInputStreamTests final : public UnitTest
  62. {
  63. FileInputStreamTests()
  64. : UnitTest ("FileInputStream", UnitTestCategories::streams)
  65. {}
  66. void runTest() override
  67. {
  68. beginTest ("Open stream non-existent file");
  69. {
  70. auto tempFile = File::createTempFile (".txt");
  71. expect (! tempFile.exists());
  72. FileInputStream stream (tempFile);
  73. expect (stream.failedToOpen());
  74. }
  75. beginTest ("Open stream existing file");
  76. {
  77. auto tempFile = File::createTempFile (".txt");
  78. tempFile.create();
  79. expect (tempFile.exists());
  80. FileInputStream stream (tempFile);
  81. expect (stream.openedOk());
  82. }
  83. const MemoryBlock data ("abcdefghijklmnopqrstuvwxyz", 26);
  84. File f (File::createTempFile (".txt"));
  85. f.appendData (data.getData(), data.getSize());
  86. FileInputStream stream (f);
  87. beginTest ("Read");
  88. {
  89. expectEquals (stream.getPosition(), (int64) 0);
  90. expectEquals (stream.getTotalLength(), (int64) data.getSize());
  91. expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
  92. expect (! stream.isExhausted());
  93. size_t numBytesRead = 0;
  94. MemoryBlock readBuffer (data.getSize());
  95. while (numBytesRead < data.getSize())
  96. {
  97. numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
  98. expectEquals (stream.getPosition(), (int64) numBytesRead);
  99. expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
  100. expect (stream.isExhausted() == (numBytesRead == data.getSize()));
  101. }
  102. expectEquals (stream.getPosition(), (int64) data.getSize());
  103. expectEquals (stream.getNumBytesRemaining(), (int64) 0);
  104. expect (stream.isExhausted());
  105. expect (readBuffer == data);
  106. }
  107. beginTest ("Skip");
  108. {
  109. stream.setPosition (0);
  110. expectEquals (stream.getPosition(), (int64) 0);
  111. expectEquals (stream.getTotalLength(), (int64) data.getSize());
  112. expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
  113. expect (! stream.isExhausted());
  114. size_t numBytesRead = 0;
  115. const int numBytesToSkip = 5;
  116. while (numBytesRead < data.getSize())
  117. {
  118. stream.skipNextBytes (numBytesToSkip);
  119. numBytesRead += numBytesToSkip;
  120. numBytesRead = std::min (numBytesRead, data.getSize());
  121. expectEquals (stream.getPosition(), (int64) numBytesRead);
  122. expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
  123. expect (stream.isExhausted() == (numBytesRead == data.getSize()));
  124. }
  125. expectEquals (stream.getPosition(), (int64) data.getSize());
  126. expectEquals (stream.getNumBytesRemaining(), (int64) 0);
  127. expect (stream.isExhausted());
  128. f.deleteFile();
  129. }
  130. }
  131. };
  132. static FileInputStreamTests fileInputStreamTests;
  133. #endif
  134. } // namespace juce