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();
84struct SubregionInputStreamTests :
public UnitTest
86 SubregionInputStreamTests()
87 :
UnitTest (
"SubregionInputStream",
"Streams")
90 void runTest()
override
92 const MemoryBlock data (
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52);
93 MemoryInputStream mi (data,
true);
95 const int offset = getRandom().nextInt ((
int) data.getSize());
96 const size_t subregionSize = data.getSize() - (size_t) offset;
98 SubregionStream stream (&mi, offset, (
int) subregionSize,
false);
102 expectEquals (stream.getPosition(), (int64) 0);
103 expectEquals (stream.getTotalLength(), (int64) subregionSize);
104 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
105 expect (! stream.isExhausted());
107 size_t numBytesRead = 0;
108 MemoryBlock readBuffer (subregionSize);
110 while (numBytesRead < subregionSize)
112 numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
114 expectEquals (stream.getPosition(), (int64) numBytesRead);
115 expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
116 expect (stream.isExhausted() == (numBytesRead == subregionSize));
119 expectEquals (stream.getPosition(), (int64) subregionSize);
120 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
121 expect (stream.isExhausted());
123 const MemoryBlock memoryBlockToCheck (data.begin() + (
size_t) offset, data.getSize() - (
size_t) offset);
124 expect (readBuffer == memoryBlockToCheck);
128 stream.setPosition (0);
129 expectEquals (stream.getPosition(), (int64) 0);
130 expectEquals (stream.getTotalLength(), (int64) subregionSize);
131 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
132 expect (! stream.isExhausted());
135 const int64 numBytesToSkip = 5;
137 while (numBytesRead < subregionSize)
139 stream.skipNextBytes (numBytesToSkip);
140 numBytesRead += numBytesToSkip;
141 numBytesRead = std::min (numBytesRead, subregionSize);
143 expectEquals (stream.getPosition(), (int64) numBytesRead);
144 expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
145 expect (stream.isExhausted() == (numBytesRead == subregionSize));
148 expectEquals (stream.getPosition(), (int64) subregionSize);
149 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
150 expect (stream.isExhausted());
154static 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.