26static inline int calcBufferStreamBufferSize (
int requestedSize, InputStream* source)
noexcept
29 jassert (source !=
nullptr);
31 requestedSize = jmax (256, requestedSize);
32 auto sourceSize = source->getTotalLength();
34 if (sourceSize >= 0 && sourceSize < requestedSize)
35 return jmax (32, (
int) sourceSize);
43 bufferSize (calcBufferStreamBufferSize (size, sourceStream)),
44 position (sourceStream->getPosition()),
45 bufferStart (position)
47 buffer.
malloc (bufferSize);
62 if (! ensureBuffered())
65 return position < lastReadPos ? buffer[(
int) (position - bufferStart)] : 0;
70 return source->getTotalLength();
86 return position >= lastReadPos && source->isExhausted();
89bool BufferedInputStream::ensureBuffered()
97 if (position < lastReadPos
99 && position >= bufferStart)
104 bufferStart = position;
116 bufferStart = position;
118 if (! source->setPosition (bufferStart))
121 bytesRead = source->read (buffer, bufferSize);
126 lastReadPos = bufferStart + bytesRead;
129 while (bytesRead < bufferSize)
130 buffer[bytesRead++] = 0;
140 if (position >= bufferStart
149 if (! ensureBuffered())
169 if (! ensureBuffered()
180 if (position >= bufferStart
181 && position < lastReadPos)
184 auto*
src = buffer + (
int) (position - bufferStart);
205 :
UnitTest (
"BufferedInputStream",
"Streams")
208 void runTest()
override
210 const MemoryBlock
data (
"abcdefghijklmnopqrstuvwxyz", 26);
211 MemoryInputStream
mi (
data,
true);
213 BufferedInputStream stream (
mi, (
int)
data.getSize());
217 expectEquals (stream.getPosition(), (int64) 0);
218 expectEquals (stream.getTotalLength(), (int64)
data.getSize());
219 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
220 expect (! stream.isExhausted());
231 expectEquals (stream.getPosition(), (int64)
numBytesRead);
232 expectEquals (stream.getNumBytesRemaining(), (int64) (
data.getSize() -
numBytesRead));
236 expectEquals (stream.getPosition(), (int64)
data.getSize());
237 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
238 expect (stream.isExhausted());
244 stream.setPosition (0);
245 expectEquals (stream.getPosition(), (int64) 0);
246 expectEquals (stream.getTotalLength(), (int64)
data.getSize());
247 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
248 expect (! stream.isExhausted());
261 expectEquals (stream.getPosition(), (int64)
numBytesRead);
262 expectEquals (stream.getNumBytesRemaining(), (int64) (
data.getSize() -
numBytesRead));
266 expectEquals (stream.getPosition(), (int64)
data.getSize());
267 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
268 expect (stream.isExhausted());
272static BufferedInputStreamTests bufferedInputStreamTests;
Holds a resizable array of primitive or copy-by-value objects.
ElementType * begin() const noexcept
Returns a pointer to the first element in the array.
Array()=default
Creates an empty array.
ElementType * data() const noexcept
Returns a pointer to the first element in the array.
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory.
static String fromUTF8(const char *utf8buffer, int bufferSizeBytes=-1)
Creates a String from a UTF-8 encoded buffer.
This is a base class for classes that perform a unit test.