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();
84 struct SubregionInputStreamTests :
public UnitTest 86 SubregionInputStreamTests()
87 :
UnitTest (
"SubregionInputStream",
"Streams")
90 void runTest()
override 92 const MemoryBlock data (
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52);
95 const int offset = getRandom().nextInt ((
int) data.
getSize());
96 const size_t subregionSize = data.
getSize() - (size_t) offset;
107 size_t numBytesRead = 0;
110 while (numBytesRead < subregionSize)
112 numBytesRead += (size_t) stream.
read (&readBuffer[numBytesRead], 3);
114 expectEquals (stream.
getPosition(), (int64) numBytesRead);
116 expect (stream.
isExhausted() == (numBytesRead == subregionSize));
119 expectEquals (stream.
getPosition(), (int64) subregionSize);
124 expect (readBuffer == memoryBlockToCheck);
135 const int64 numBytesToSkip = 5;
137 while (numBytesRead < subregionSize)
140 numBytesRead += numBytesToSkip;
141 numBytesRead = std::min (numBytesRead, subregionSize);
143 expectEquals (stream.
getPosition(), (int64) numBytesRead);
145 expect (stream.
isExhausted() == (numBytesRead == subregionSize));
148 expectEquals (stream.
getPosition(), (int64) subregionSize);
154 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.
char * begin() const noexcept
Returns an iterator for the data.
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.