byteme
Read/write bytes from various sources
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 "utils.hpp"
11
18namespace byteme {
19
30template<class Pointer_>
31class IstreamReader final : public Reader {
32public:
36 IstreamReader(Pointer_ input) : my_input(std::move(input)) {}
37
38public:
39 std::size_t read(unsigned char* buffer, std::size_t n) {
40 return safe_read<std::streamsize>(buffer, n, [this](unsigned char* buffer, std::streamsize n) -> std::streamsize {
41 my_input->read(reinterpret_cast<char*>(buffer), n);
42 const auto my_read = my_input->gcount();
43 if (my_read < n) {
44 if (!(my_input->eof())) {
45 throw std::runtime_error("failed to finish reading the input stream");
46 }
47 }
48 return my_read;
49 });
50 }
51
52private:
53 Pointer_ my_input;
54};
55
56}
57
58#endif
Read an input source.
Read bytes from a std::istream.
Definition IstreamReader.hpp:31
IstreamReader(Pointer_ input)
Definition IstreamReader.hpp:36
std::size_t read(unsigned char *buffer, std::size_t n)
Definition IstreamReader.hpp:39
Virtual class for reading bytes from a source.
Definition Reader.hpp:17
Simple byte readers and writers.
Definition BufferedReader.hpp:21