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);
95 MemoryInputStream mi (data,
true);
97 const int offset = getRandom().nextInt ((
int) data.getSize());
98 const size_t subregionSize = data.getSize() - (size_t) offset;
100 SubregionStream stream (&mi, offset, (
int) subregionSize,
false);
104 expectEquals (stream.getPosition(), (int64) 0);
105 expectEquals (stream.getTotalLength(), (int64) subregionSize);
106 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
107 expect (! stream.isExhausted());
109 size_t numBytesRead = 0;
110 MemoryBlock readBuffer (subregionSize);
112 while (numBytesRead < subregionSize)
114 numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
116 expectEquals (stream.getPosition(), (int64) numBytesRead);
117 expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
118 expect (stream.isExhausted() == (numBytesRead == subregionSize));
121 expectEquals (stream.getPosition(), (int64) subregionSize);
122 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
123 expect (stream.isExhausted());
125 const MemoryBlock memoryBlockToCheck (data.begin() + (
size_t) offset, data.getSize() - (
size_t) offset);
126 expect (readBuffer == memoryBlockToCheck);
130 stream.setPosition (0);
131 expectEquals (stream.getPosition(), (int64) 0);
132 expectEquals (stream.getTotalLength(), (int64) subregionSize);
133 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
134 expect (! stream.isExhausted());
137 const int64 numBytesToSkip = 5;
139 while (numBytesRead < subregionSize)
141 stream.skipNextBytes (numBytesToSkip);
142 numBytesRead += numBytesToSkip;
143 numBytesRead = std::min (numBytesRead, subregionSize);
145 expectEquals (stream.getPosition(), (int64) numBytesRead);
146 expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
147 expect (stream.isExhausted() == (numBytesRead == subregionSize));
150 expectEquals (stream.getPosition(), (int64) subregionSize);
151 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
152 expect (stream.isExhausted());
156 static SubregionInputStreamTests subregionInputStreamTests;
SubregionStream(InputStream *sourceStream, int64 startPositionInSourceStream, int64 lengthOfSourceStream, bool deleteSourceWhenDestroyed)
Creates a SubregionStream from an input source.
int64 getPosition() override
Returns the offset of the next byte that will be read from the stream.
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.
int read(void *destBuffer, int maxBytesToRead) override
Reads some data from the stream into a memory buffer.
int64 getTotalLength() override
Returns the total number of bytes available for reading in this stream.
~SubregionStream() override
Destructor.
This is a base class for classes that perform a unit test.