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)
 

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 advance() is still true. Users should only call this method if valid() is still 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.
  • Wether 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, it can be assumed that no more bytes are available in the source (i.e., the second element must be false). Note that the converse is not true as it is possible to read number bytes and finish the source at the same time.

◆ 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: