Add stream avail() methods

Makes it more readable to write code that needs to know how much
data/space is available in a stream.
This commit is contained in:
Pierre Ossman 2020-05-19 20:45:22 +02:00 committed by Lauri Kasanen
parent 910fd8fa3e
commit 7f90205cf2
13 changed files with 38 additions and 22 deletions

View File

@ -126,7 +126,7 @@ size_t FdInStream::overrun(size_t itemSize, size_t nItems, bool wait)
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -129,7 +129,7 @@ size_t FdOutStream::overrun(size_t itemSize, size_t nItems)
flush();
// Still not enough space?
if (itemSize > (size_t)(end - ptr)) {
if (itemSize > avail()) {
// Can we shuffle things around?
// (don't do this if it gains us less than 25%)
if (((size_t)(sentUpTo - start) > bufSize / 4) &&
@ -150,7 +150,7 @@ size_t FdOutStream::overrun(size_t itemSize, size_t nItems)
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -81,7 +81,7 @@ size_t FileInStream::overrun(size_t itemSize, size_t nItems, bool wait)
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -91,7 +91,7 @@ size_t HexInStream::overrun(size_t itemSize, size_t nItems, bool wait) {
offset += ptr - start;
ptr = start;
while ((size_t)(end - ptr) < itemSize) {
while (avail() < itemSize) {
size_t n = in_stream.check(2, 1, wait);
if (n == 0) return 0;
const U8* iptr = in_stream.getptr();
@ -111,7 +111,7 @@ size_t HexInStream::overrun(size_t itemSize, size_t nItems, bool wait) {
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -103,7 +103,7 @@ HexOutStream::overrun(size_t itemSize, size_t nItems) {
writeBuffer();
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -35,6 +35,14 @@ namespace rdr {
virtual ~InStream() {}
// avail() returns the number of bytes that are currenctly directly
// available from the stream.
inline size_t avail()
{
return end - ptr;
}
// check() ensures there is buffer data for at least one item of size
// itemSize bytes. Returns the number of items in the buffer (up to a
// maximum of nItems). If wait is false, then instead of blocking to wait
@ -48,11 +56,11 @@ namespace rdr {
if (itemSize == 0 || nItems == 0)
return 0;
if (itemSize > (size_t)(end - ptr))
if (itemSize > avail())
return overrun(itemSize, nItems, wait);
// itemSize cannot be zero at this point
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -40,6 +40,14 @@ namespace rdr {
virtual ~OutStream() {}
// avail() returns the number of bytes that currently be written to the
// stream without any risk of blocking.
inline size_t avail()
{
return end - ptr;
}
// check() ensures there is buffer space for at least one item of size
// itemSize bytes. Returns the number of items which fit (up to a maximum
// of nItems).
@ -48,10 +56,10 @@ namespace rdr {
{
size_t nAvail;
if (itemSize > (size_t)(end - ptr))
if (itemSize > avail())
return overrun(itemSize, nItems);
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -124,7 +124,7 @@ size_t RandomStream::overrun(size_t itemSize, size_t nItems, bool wait) {
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -100,7 +100,7 @@ size_t TLSInStream::overrun(size_t itemSize, size_t nItems, bool wait)
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -101,7 +101,7 @@ size_t TLSOutStream::overrun(size_t itemSize, size_t nItems)
flush();
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;

View File

@ -108,13 +108,13 @@ size_t ZlibInStream::overrun(size_t itemSize, size_t nItems, bool wait)
end -= ptr - start;
ptr = start;
while ((size_t)(end - ptr) < itemSize) {
while (avail() < itemSize) {
if (!decompress(wait))
return 0;
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;
@ -136,7 +136,7 @@ bool ZlibInStream::decompress(bool wait)
size_t n = underlying->check(1, 1, wait);
if (n == 0) return false;
zs->next_in = (U8*)underlying->getptr();
zs->avail_in = underlying->getend() - underlying->getptr();
zs->avail_in = underlying->avail();
if (zs->avail_in > bytesIn)
zs->avail_in = bytesIn;

View File

@ -106,7 +106,7 @@ size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems)
checkCompressionLevel();
while ((size_t)(end - ptr) < itemSize) {
while (avail() < itemSize) {
zs->next_in = start;
zs->avail_in = ptr - start;
@ -128,7 +128,7 @@ size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems)
}
size_t nAvail;
nAvail = (end - ptr) / itemSize;
nAvail = avail() / itemSize;
if (nAvail < nItems)
return nAvail;
@ -148,7 +148,7 @@ void ZlibOutStream::deflate(int flush)
do {
underlying->check(1);
zs->next_out = underlying->getptr();
zs->avail_out = underlying->getend() - underlying->getptr();
zs->avail_out = underlying->avail();
#ifdef ZLIBOUT_DEBUG
fprintf(stderr,"zos: calling deflate, avail_in %d, avail_out %d\n",

View File

@ -85,7 +85,7 @@ JpegInitDestination(j_compress_ptr cinfo)
jc->clear();
dest->pub.next_output_byte = jc->getptr();
dest->pub.free_in_buffer = jc->getend() - jc->getptr();
dest->pub.free_in_buffer = jc->avail();
}
static boolean
@ -97,7 +97,7 @@ JpegEmptyOutputBuffer(j_compress_ptr cinfo)
jc->setptr(jc->getend());
jc->overrun(jc->getend() - jc->getstart(), 1);
dest->pub.next_output_byte = jc->getptr();
dest->pub.free_in_buffer = jc->getend() - jc->getptr();
dest->pub.free_in_buffer = jc->avail();
return TRUE;
}