byteme
C++ wrappers for buffered inputs
Loading...
Searching...
No Matches
IstreamReader.hpp
Go to the documentation of this file.
1#ifndef BYTEME_ISTREAM_READER_HPP
2#define BYTEME_ISTREAM_READER_HPP
3
4#include <istream>
5#include <vector>
6#include <stdexcept>
7#include "Reader.hpp"
8
15namespace byteme {
16
25template<class Pointer_ = std::istream*>
26class IstreamReader : public Reader {
27public:
32 IstreamReader(Pointer_ input, size_t buffer_size = 65536) : my_input(std::move(input)), my_buffer(buffer_size) {}
33
34 bool load() {
35 if (!my_okay) {
36 return false;
37 }
38
39 my_input->read(reinterpret_cast<char*>(my_buffer.data()), my_buffer.size());
40 my_read = my_input->gcount();
41
42 if (my_read < my_buffer.size()) {
43 if (my_input->eof()) {
44 my_okay = false;
45 } else {
46 throw std::runtime_error("failed to finish reading the input stream");
47 }
48 }
49
50 return true;
51 }
52
53 const unsigned char* buffer() const {
54 return my_buffer.data();
55 }
56
57 size_t available() const {
58 return my_read;
59 }
60
61private:
62 Pointer_ my_input;
63 std::vector<unsigned char> my_buffer;
64 size_t my_read = 0;
65 bool my_okay = true;
66};
67
68}
69
70#endif
Read an input source.
Read bytes from a std::istream.
Definition IstreamReader.hpp:26
const unsigned char * buffer() const
Definition IstreamReader.hpp:53
size_t available() const
Definition IstreamReader.hpp:57
bool load()
Definition IstreamReader.hpp:34
IstreamReader(Pointer_ input, size_t buffer_size=65536)
Definition IstreamReader.hpp:32
Virtual class for reading bytes from a source.
Definition Reader.hpp:15
Simple byte readers and writers.