mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Improved error handling in BufferedInputStream and tidied up a few classes in the streams folder
This commit is contained in:
parent
f4de59972a
commit
08a1b7bb6d
16 changed files with 95 additions and 112 deletions
|
|
@ -59,8 +59,10 @@ BufferedInputStream::~BufferedInputStream()
|
|||
//==============================================================================
|
||||
char BufferedInputStream::peekByte()
|
||||
{
|
||||
ensureBuffered();
|
||||
return position < lastReadPos ? *(buffer + (int) (position - bufferStart)) : 0;
|
||||
if (! ensureBuffered())
|
||||
return 0;
|
||||
|
||||
return position < lastReadPos ? buffer[(int) (position - bufferStart)] : 0;
|
||||
}
|
||||
|
||||
int64 BufferedInputStream::getTotalLength()
|
||||
|
|
@ -84,7 +86,7 @@ bool BufferedInputStream::isExhausted()
|
|||
return position >= lastReadPos && source->isExhausted();
|
||||
}
|
||||
|
||||
void BufferedInputStream::ensureBuffered()
|
||||
bool BufferedInputStream::ensureBuffered()
|
||||
{
|
||||
auto bufferEndOverlap = lastReadPos - bufferOverlap;
|
||||
|
||||
|
|
@ -100,24 +102,35 @@ void BufferedInputStream::ensureBuffered()
|
|||
memmove (buffer, buffer + (int) (position - bufferStart), (size_t) bytesToKeep);
|
||||
|
||||
bufferStart = position;
|
||||
|
||||
bytesRead = source->read (buffer + bytesToKeep,
|
||||
(int) (bufferSize - bytesToKeep));
|
||||
|
||||
if (bytesRead < 0)
|
||||
return false;
|
||||
|
||||
lastReadPos += bytesRead;
|
||||
bytesRead += bytesToKeep;
|
||||
}
|
||||
else
|
||||
{
|
||||
bufferStart = position;
|
||||
source->setPosition (bufferStart);
|
||||
|
||||
if (! source->setPosition (bufferStart))
|
||||
return false;
|
||||
|
||||
bytesRead = source->read (buffer, bufferSize);
|
||||
|
||||
if (bytesRead < 0)
|
||||
return false;
|
||||
|
||||
lastReadPos = bufferStart + bytesRead;
|
||||
}
|
||||
|
||||
while (bytesRead < bufferSize)
|
||||
buffer [bytesRead++] = 0;
|
||||
buffer[bytesRead++] = 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int BufferedInputStream::read (void* destBuffer, int maxBytesToRead)
|
||||
|
|
@ -133,7 +146,8 @@ int BufferedInputStream::read (void* destBuffer, int maxBytesToRead)
|
|||
}
|
||||
|
||||
if (position < bufferStart || position >= lastReadPos)
|
||||
ensureBuffered();
|
||||
if (! ensureBuffered())
|
||||
return 0;
|
||||
|
||||
int bytesRead = 0;
|
||||
|
||||
|
|
@ -151,12 +165,10 @@ int BufferedInputStream::read (void* destBuffer, int maxBytesToRead)
|
|||
}
|
||||
|
||||
auto oldLastReadPos = lastReadPos;
|
||||
ensureBuffered();
|
||||
|
||||
if (oldLastReadPos == lastReadPos)
|
||||
break; // if ensureBuffered() failed to read any more data, bail out
|
||||
|
||||
if (isExhausted())
|
||||
if (! ensureBuffered()
|
||||
|| oldLastReadPos == lastReadPos
|
||||
|| isExhausted())
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ private:
|
|||
int bufferSize;
|
||||
int64 position, lastReadPos = 0, bufferStart, bufferOverlap = 128;
|
||||
HeapBlock<char> buffer;
|
||||
void ensureBuffered();
|
||||
bool ensureBuffered();
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferedInputStream)
|
||||
};
|
||||
|
|
|
|||
|
|
@ -23,24 +23,19 @@
|
|||
namespace juce
|
||||
{
|
||||
|
||||
MemoryInputStream::MemoryInputStream (const void* const sourceData,
|
||||
const size_t sourceDataSize,
|
||||
const bool keepInternalCopy)
|
||||
MemoryInputStream::MemoryInputStream (const void* sourceData, size_t sourceDataSize, bool keepCopy)
|
||||
: data (sourceData),
|
||||
dataSize (sourceDataSize),
|
||||
position (0)
|
||||
dataSize (sourceDataSize)
|
||||
{
|
||||
if (keepInternalCopy)
|
||||
if (keepCopy)
|
||||
createInternalCopy();
|
||||
}
|
||||
|
||||
MemoryInputStream::MemoryInputStream (const MemoryBlock& sourceData,
|
||||
const bool keepInternalCopy)
|
||||
MemoryInputStream::MemoryInputStream (const MemoryBlock& sourceData, bool keepCopy)
|
||||
: data (sourceData.getData()),
|
||||
dataSize (sourceData.getSize()),
|
||||
position (0)
|
||||
dataSize (sourceData.getSize())
|
||||
{
|
||||
if (keepInternalCopy)
|
||||
if (keepCopy)
|
||||
createInternalCopy();
|
||||
}
|
||||
|
||||
|
|
@ -60,14 +55,14 @@ int64 MemoryInputStream::getTotalLength()
|
|||
return (int64) dataSize;
|
||||
}
|
||||
|
||||
int MemoryInputStream::read (void* const buffer, const int howMany)
|
||||
int MemoryInputStream::read (void* buffer, int howMany)
|
||||
{
|
||||
jassert (buffer != nullptr && howMany >= 0);
|
||||
|
||||
if (howMany <= 0 || position >= dataSize)
|
||||
return 0;
|
||||
|
||||
const size_t num = jmin ((size_t) howMany, dataSize - position);
|
||||
auto num = jmin ((size_t) howMany, dataSize - position);
|
||||
|
||||
if (num > 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ public:
|
|||
|
||||
//==============================================================================
|
||||
int64 getPosition() override;
|
||||
bool setPosition (int64 pos) override;
|
||||
bool setPosition (int64) override;
|
||||
int64 getTotalLength() override;
|
||||
bool isExhausted() override;
|
||||
int read (void* destBuffer, int maxBytesToRead) override;
|
||||
|
|
@ -79,7 +79,7 @@ public:
|
|||
private:
|
||||
//==============================================================================
|
||||
const void* data;
|
||||
size_t dataSize, position;
|
||||
size_t dataSize, position = 0;
|
||||
HeapBlock<char> internalCopy;
|
||||
|
||||
void createInternalCopy();
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@
|
|||
namespace juce
|
||||
{
|
||||
|
||||
SubregionStream::SubregionStream (InputStream* const sourceStream,
|
||||
const int64 start, const int64 length,
|
||||
const bool deleteSourceWhenDestroyed)
|
||||
SubregionStream::SubregionStream (InputStream* sourceStream,
|
||||
int64 start, int64 length,
|
||||
bool deleteSourceWhenDestroyed)
|
||||
: source (sourceStream, deleteSourceWhenDestroyed),
|
||||
startPositionInSourceStream (start),
|
||||
lengthOfSourceStream (length)
|
||||
|
|
@ -39,7 +39,7 @@ SubregionStream::~SubregionStream()
|
|||
|
||||
int64 SubregionStream::getTotalLength()
|
||||
{
|
||||
const int64 srcLen = source->getTotalLength() - startPositionInSourceStream;
|
||||
auto srcLen = source->getTotalLength() - startPositionInSourceStream;
|
||||
|
||||
return lengthOfSourceStream >= 0 ? jmin (lengthOfSourceStream, srcLen)
|
||||
: srcLen;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue