byteme
Read/write bytes from various sources
Loading...
Searching...
No Matches
byteme::BufferedReader< Type_ > Class Template Reference

Buffered wrapper around a Reader. More...

#include <BufferedReader.hpp>

Inheritance diagram for byteme::BufferedReader< Type_ >:

Public Member Functions

bool advance ()
 
Type_ get () const
 
unsigned long long position () const
 
bool valid () const
 
std::pair< std::size_t, bool > extract (std::size_t number, Type_ *output)
 
std::size_t extract_until (std::size_t number, Type_ *output)
 

Detailed Description

template<typename Type_>
class byteme::BufferedReader< Type_ >

Buffered wrapper around a Reader.

Template Parameters
Type_Type of the output bytes, usually char for text or unsigned char for binary.

In some applications, we may need to iterate over many small chunks of bytes or even individual bytes. Naively calling Reader::read() for each request may be inefficient if the underlying implementation attempts to read from some storage device for each call. Instead, we can wrap our Reader in a BufferedReader instance, which calls Reader::read() every now and then to fill a large intermediate buffer. Users can then iterate that buffer to obtain the next byte or chunk of bytes, reducing the number of separate calls to Reader::read().

Check out SerialBufferedReader and ParallelBufferedReader for subclasses.

Member Function Documentation

◆ advance()

template<typename Type_ >
bool byteme::BufferedReader< Type_ >::advance ( )
inline

Advance to the next byte, possibly refilling the buffer using bytes from the supplied Reader. This should only be called if valid() is true.

Returns
Whether the buffer still has one or more bytes that can be read, i.e., the output of valid() after advancing to the next byte.

◆ extract()

template<typename Type_ >
std::pair< std::size_t, bool > byteme::BufferedReader< Type_ >::extract ( std::size_t number,
Type_ * output )
inline

Extract up to number bytes from the buffer and store them in the output. This is equivalent to (but more efficient than) calling get() and then advance() up to number times, only iterating while the return value of the latter advance() is still true. Users should only call this method if valid() is true.

Parameters
numberNumber of bytes to extract.
[out]outputPointer to an output buffer of length number. This is filled with up to number bytes from the source.
Returns
Pair containing:
  • The number of bytes that were successfully read into output. This can also be interpreted as the number of successful get()/advance() iterations.
  • Whether there are any more bytes available in the source for future get() or extract() calls. This can also be interpreted as the result of the final advance(), i.e., the result of the valid() after extract() returns.

If the first element is less than number, the second element must be false, i.e., no more bytes are available in the source. Note that converse may not be true, i.e., the second element can be false even if the first element is equal to number.

◆ extract_until()

template<typename Type_ >
std::size_t byteme::BufferedReader< Type_ >::extract_until ( std::size_t number,
Type_ * output )
inline

Extract up to number bytes from the buffer and store them in the output, stopping on the last byte. This is equivalent to calling extract(X - 1, output) and then setting output[X - 1] = get() without any additional advance(), where X is the return value of this method, i.e., the smaller of number and the number of remaining bytes in the Reader. Users should only call this method if valid() is true.

To be clear, extract_until() differs from extract() in that the former does not advance past the final extracted byte. This is occasionally useful in loops where advance() is called before get(). Calling advance() and then extract_until() is equivalent to X iterations of a advance() + get() loop.

Parameters
numberNumber of bytes to extract. This should be positive.
[out]outputPointer to an output buffer of length number. This is filled with up to number bytes from the source.
Returns
The number of bytes that were successfully read into output, i.e., X. This is less than number iff no more bytes are available in the source. On return, the value of get() will be equal to output[X - 1].

◆ get()

template<typename Type_ >
Type_ byteme::BufferedReader< Type_ >::get ( ) const
inline
Returns
The current byte.

This should only be called if valid() is true.

◆ position()

template<typename Type_ >
unsigned long long byteme::BufferedReader< Type_ >::position ( ) const
inline
Returns
The position of the current byte since the start of the input.

◆ valid()

template<typename Type_ >
bool byteme::BufferedReader< Type_ >::valid ( ) const
inline
Returns
Whether the buffer still has one or more bytes that can be read.

The documentation for this class was generated from the following file: