1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-26 02:14:22 +00:00

Tweaked FileInputStream::setPosition to make sure it returns false when trying to seek beyond the limits of the file.

This commit is contained in:
jules 2014-05-24 18:36:17 +01:00
parent 4bd38dc619
commit d8180f7ba5
4 changed files with 23 additions and 37 deletions

View file

@ -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;
}

View file

@ -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

View file

@ -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)

View file

@ -234,7 +234,7 @@ void FileInputStream::openHandle()
status = WindowsFileHelpers::getResultForLastError();
}
void FileInputStream::closeHandle()
FileInputStream::~FileInputStream()
{
CloseHandle ((HANDLE) fileHandle);
}