27 int64 start, int64 length,
28 bool deleteSourceWhenDestroyed)
29 : source (sourceStream, deleteSourceWhenDestroyed),
30 startPositionInSourceStream (start),
31 lengthOfSourceStream (length)
42 auto srcLen = source->getTotalLength() - startPositionInSourceStream;
44 return lengthOfSourceStream >= 0 ? jmin (lengthOfSourceStream, srcLen)
50 return source->getPosition() - startPositionInSourceStream;
55 return source->setPosition (jmax ((int64) 0, newPosition + startPositionInSourceStream));
60 jassert (destBuffer !=
nullptr && maxBytesToRead >= 0);
62 if (lengthOfSourceStream < 0)
63 return source->read (destBuffer, maxBytesToRead);
65 maxBytesToRead = (int) jmin ((int64) maxBytesToRead, lengthOfSourceStream -
getPosition());
67 if (maxBytesToRead <= 0)
70 return source->read (destBuffer, maxBytesToRead);
75 if (lengthOfSourceStream >= 0 &&
getPosition() >= lengthOfSourceStream)
78 return source->isExhausted();
86 struct SubregionInputStreamTests :
public UnitTest 88 SubregionInputStreamTests()
89 :
UnitTest (
"SubregionInputStream", UnitTestCategories::streams)
92 void runTest()
override 94 const MemoryBlock data (
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52);
97 const int offset = getRandom().nextInt ((
int) data.
getSize());
98 const size_t subregionSize = data.
getSize() - (size_t) offset;
109 size_t numBytesRead = 0;
112 while (numBytesRead < subregionSize)
114 numBytesRead += (size_t) stream.
read (&readBuffer[numBytesRead], 3);
116 expectEquals (stream.
getPosition(), (int64) numBytesRead);
118 expect (stream.
isExhausted() == (numBytesRead == subregionSize));
121 expectEquals (stream.
getPosition(), (int64) subregionSize);
126 expect (readBuffer == memoryBlockToCheck);
137 const int64 numBytesToSkip = 5;
139 while (numBytesRead < subregionSize)
142 numBytesRead += numBytesToSkip;
143 numBytesRead = std::min (numBytesRead, subregionSize);
145 expectEquals (stream.
getPosition(), (int64) numBytesRead);
147 expect (stream.
isExhausted() == (numBytesRead == subregionSize));
150 expectEquals (stream.
getPosition(), (int64) subregionSize);
156 static SubregionInputStreamTests subregionInputStreamTests;
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
~SubregionStream() override
Destructor.
int64 getPosition() override
Returns the offset of the next byte that will be read from the stream.
int64 getTotalLength() override
Returns the total number of bytes available for reading in this stream.
Wraps another input stream, and reads from a specific part of it.
This is a base class for classes that perform a unit test.
bool setPosition(int64 newPosition) override
Tries to move the current read position of the stream.
bool isExhausted() override
Returns true if the stream has no more data to read.
SubregionStream(InputStream *sourceStream, int64 startPositionInSourceStream, int64 lengthOfSourceStream, bool deleteSourceWhenDestroyed)
Creates a SubregionStream from an input source.
int read(void *destBuffer, int maxBytesToRead) override
Reads some data from the stream into a memory buffer.
A class to hold a resizable block of raw data.
char * begin() noexcept
Returns an iterator for the data.