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 <vector>
5#include <stdexcept>
6#include <memory>
7#include <cstddef>
8
9#include "Reader.hpp"
10#include "check_buffer_size.hpp"
11
18namespace byteme {
19
28 std::size_t buffer_size = 65536;
29};
30
41template<class Pointer_>
42class IstreamReader final : public Reader {
43public:
48 IstreamReader(Pointer_ input, const IstreamReaderOptions& options) :
49 my_input(std::move(input)),
50 my_buffer(
51 check_buffer_size<std::streamsize>( // for istream::read().
52 check_buffer_size(options.buffer_size)
53 )
54 )
55 {}
56
57public:
58 bool load() {
59 if (!my_okay) {
60 return false;
61 }
62
63 my_input->read(reinterpret_cast<char*>(my_buffer.data()), my_buffer.size());
64 my_read = my_input->gcount();
65
66 if (my_read < my_buffer.size()) {
67 if (my_input->eof()) {
68 my_okay = false;
69 } else {
70 throw std::runtime_error("failed to finish reading the input stream");
71 }
72 }
73
74 return true;
75 }
76
77 const unsigned char* buffer() const {
78 return my_buffer.data();
79 }
80
81 std::size_t available() const {
82 return my_read;
83 }
84
85private:
86 Pointer_ my_input;
87 std::vector<unsigned char> my_buffer;
88 std::size_t my_read = 0;
89 bool my_okay = true;
90};
91
92}
93
94#endif
Read an input source.
Read bytes from a std::istream.
Definition IstreamReader.hpp:42
IstreamReader(Pointer_ input, const IstreamReaderOptions &options)
Definition IstreamReader.hpp:48
const unsigned char * buffer() const
Definition IstreamReader.hpp:77
bool load()
Definition IstreamReader.hpp:58
std::size_t available() const
Definition IstreamReader.hpp:81
Virtual class for reading bytes from a source.
Definition Reader.hpp:17
Simple byte readers and writers.
Options for IstreamReader construction.
Definition IstreamReader.hpp:23
std::size_t buffer_size
Definition IstreamReader.hpp:28