diff --git a/modules/juce_core/files/juce_FileInputStream.cpp b/modules/juce_core/files/juce_FileInputStream.cpp index 426f3adc70..801ccfaaf2 100644 --- a/modules/juce_core/files/juce_FileInputStream.cpp +++ b/modules/juce_core/files/juce_FileInputStream.cpp @@ -28,41 +28,34 @@ int64 juce_fileSetPosition (void* handle, int64 pos); + //============================================================================== FileInputStream::FileInputStream (const File& f) : file (f), fileHandle (nullptr), currentPosition (0), - status (Result::ok()), - needToSeek (true) + status (Result::ok()) { openHandle(); } -FileInputStream::~FileInputStream() -{ - closeHandle(); -} - -//============================================================================== int64 FileInputStream::getTotalLength() { + // You should always check that a stream opened successfully before using it! + jassert (openedOk()); + return file.getSize(); } int FileInputStream::read (void* buffer, int bytesToRead) { + // You should always check that a stream opened successfully before using it! jassert (openedOk()); + + // The buffer should never be null, and a negative size is probably a + // sign that something is broken! jassert (buffer != nullptr && bytesToRead >= 0); - if (needToSeek) - { - if (juce_fileSetPosition (fileHandle, currentPosition) < 0) - return 0; - - needToSeek = false; - } - const size_t num = readInternal (buffer, (size_t) bytesToRead); currentPosition += num; @@ -81,15 +74,11 @@ int64 FileInputStream::getPosition() bool FileInputStream::setPosition (int64 pos) { + // You should always check that a stream opened successfully before using it! jassert (openedOk()); if (pos != currentPosition) - { - pos = jlimit ((int64) 0, getTotalLength(), pos); + currentPosition = juce_fileSetPosition (fileHandle, pos); - needToSeek |= (currentPosition != pos); - currentPosition = pos; - } - - return true; + return currentPosition == pos; } diff --git a/modules/juce_core/files/juce_FileInputStream.h b/modules/juce_core/files/juce_FileInputStream.h index fd5a5c6131..35963baaee 100644 --- a/modules/juce_core/files/juce_FileInputStream.h +++ b/modules/juce_core/files/juce_FileInputStream.h @@ -40,10 +40,11 @@ class JUCE_API FileInputStream : public InputStream { public: //============================================================================== - /** Creates a FileInputStream. + /** Creates a FileInputStream to read from the given file. - @param fileToRead the file to read from - if the file can't be accessed for some - reason, then the stream will just contain no data + After creating a FileInputStream, you should use openedOk() or failedToOpen() + to make sure that it's OK before trying to read from it! If it failed, you + can call getStatus() to get more error information. */ explicit FileInputStream (const File& fileToRead); @@ -73,24 +74,23 @@ public: //============================================================================== int64 getTotalLength() override; - int read (void* destBuffer, int maxBytesToRead) override; + int read (void*, int) override; bool isExhausted() override; int64 getPosition() override; - bool setPosition (int64 pos) override; + bool setPosition (int64) override; private: //============================================================================== - File file; + const File file; void* fileHandle; int64 currentPosition; Result status; - bool needToSeek; void openHandle(); - void closeHandle(); - size_t readInternal (void* buffer, size_t numBytes); + size_t readInternal (void*, size_t); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileInputStream) }; + #endif // JUCE_FILEINPUTSTREAM_H_INCLUDED diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index 88def315a5..1be83e5804 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -397,13 +397,10 @@ void FileInputStream::openHandle() status = getResultForErrno(); } -void FileInputStream::closeHandle() +FileInputStream::~FileInputStream() { if (fileHandle != 0) - { close (getFD (fileHandle)); - fileHandle = 0; - } } size_t FileInputStream::readInternal (void* const buffer, const size_t numBytes) diff --git a/modules/juce_core/native/juce_win32_Files.cpp b/modules/juce_core/native/juce_win32_Files.cpp index 2ef738331e..80ad084ee3 100644 --- a/modules/juce_core/native/juce_win32_Files.cpp +++ b/modules/juce_core/native/juce_win32_Files.cpp @@ -234,7 +234,7 @@ void FileInputStream::openHandle() status = WindowsFileHelpers::getResultForLastError(); } -void FileInputStream::closeHandle() +FileInputStream::~FileInputStream() { CloseHandle ((HANDLE) fileHandle); }