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.
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
-
| number | Number of bytes to extract. |
| [out] | output | Pointer 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.
template<typename Type_ >
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
-
| number | Number of bytes to extract. This should be positive. |
| [out] | output | Pointer 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].