roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
input_stream.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <inttypes.h>
4
5
#include "
roo_io/base/byte.h
"
6
#include "
roo_io/status.h
"
7
8
namespace
roo_io
{
9
10
/// Virtualizes access to files, memory, and other readable sources.
11
///
12
/// Represents an open resource with a read cursor.
13
///
14
/// For iterator-style usage, prefer `BufferedInputStreamIterator` to avoid
15
/// virtual-call overhead per byte.
16
class
InputStream
{
17
public
:
18
virtual
~InputStream
() {
close
(); }
19
20
/// Returns whether stream is considered open.
21
///
22
/// @return `true` when `status()` is `kOk` or `kEndOfStream`.
23
virtual
bool
isOpen
()
const
{
24
Status
s
=
status
();
25
return
s
==
kOk
||
s
==
kEndOfStream
;
26
}
27
28
/// Closes this stream.
29
///
30
/// Updates status.
31
///
32
/// If previous status was `kOk` or `kEndOfStream`, status transitions to
33
/// `kClosed`.
34
///
35
/// After close, read operations should return zero bytes.
36
virtual
void
close
() {}
37
38
/// Attempts to read up to `count` bytes into `result`.
39
///
40
/// Updates status.
41
///
42
/// Contract:
43
/// - On success (`status() == kOk`), returns at least one byte.
44
/// - On end-of-stream (`status() == kEndOfStream`), returns zero.
45
/// - On error, may return zero or number of bytes read before failure.
46
///
47
/// If status before call is not `kOk`, leaves it unchanged and returns zero.
48
///
49
/// Implementations may return fewer than `count` bytes even when more data is
50
/// available. Use `readFully()` if that is not the desired behavior.
51
/// @return Number of bytes read.
52
virtual
size_t
read
(
byte
*
result
,
size_t
count
) = 0;
53
54
/// Attempts to read up to `count` bytes into `result` without indefinitely
55
/// blocking.
56
///
57
/// Updates status.
58
///
59
/// Similar to `read()`, but may return zero on success when no data is
60
/// currently available without blocking.
61
///
62
/// The implementation has some leeway for deciding what constitutes
63
/// unacceptable blocking, as long as it is guaranteed that callers can always
64
/// make progress by calling tryRead() repeatedly, and never calling read().
65
///
66
/// @return Number of bytes read.
67
virtual
size_t
tryRead
(
byte
*
result
,
size_t
count
) {
68
return
read
(
result
,
count
);
69
}
70
71
/// Attempts to read `count` bytes into `buf`; blocks as needed.
72
///
73
/// Updates status.
74
///
75
/// Unlike `read()`, this method keeps reading until one of the following:
76
/// - `count` bytes are read,
77
/// - end-of-stream is reached,
78
/// - an error occurs.
79
///
80
/// If pre-call status is not `kOk`, this method returns zero and leaves
81
/// status unchanged.
82
///
83
/// @return Total bytes read.
84
virtual
size_t
readFully
(
byte
*
buf
,
size_t
count
) {
85
size_t
read_total
= 0;
86
while
(
count
> 0) {
87
int
read_now
=
read
(
buf
,
count
);
88
if
(
read_now
== 0)
break
;
89
buf
+=
read_now
;
90
read_total
+=
read_now
;
91
count
-=
read_now
;
92
}
93
return
read_total
;
94
}
95
96
/// Skips over `count` bytes, updating `status()`.
97
///
98
/// Conceptually equivalent to `readFully(tmp, count)` and discarding data.
99
///
100
/// If pre-call status is not `kOk`, leaves status unchanged and returns.
101
///
102
/// If skip ends exactly at stream end, status remains `kOk`.
103
/// If skip ends before stream end, status remains `kOk`.
104
/// If skip crosses stream end, status becomes `kEndOfStream`.
105
///
106
/// Any I/O error updates status accordingly.
107
virtual
void
skip
(
uint64_t
count
) {
108
byte
buf
[64];
109
while
(
count
>= 64) {
110
if
(
status
() !=
kOk
)
return
;
111
count
-=
read
(
buf
, 64);
112
}
113
while
(
count
> 0) {
114
if
(
status
() !=
kOk
)
return
;
115
count
-=
read
(
buf
,
count
);
116
}
117
}
118
119
/// Returns status of the most recent I/O operation.
120
virtual
Status
status
()
const
= 0;
121
};
122
123
}
// namespace roo_io
byte.h
roo_io::InputStream
Virtualizes access to files, memory, and other readable sources.
Definition
input_stream.h:16
roo_io::InputStream::isOpen
virtual bool isOpen() const
Returns whether stream is considered open.
Definition
input_stream.h:23
roo_io::InputStream::read
virtual size_t read(byte *result, size_t count)=0
Attempts to read up to count bytes into result.
roo_io::InputStream::skip
virtual void skip(uint64_t count)
Skips over count bytes, updating status().
Definition
input_stream.h:107
roo_io::InputStream::close
virtual void close()
Closes this stream.
Definition
input_stream.h:36
roo_io::InputStream::status
virtual Status status() const =0
Returns status of the most recent I/O operation.
roo_io::InputStream::~InputStream
virtual ~InputStream()
Definition
input_stream.h:18
roo_io::InputStream::readFully
virtual size_t readFully(byte *buf, size_t count)
Attempts to read count bytes into buf; blocks as needed.
Definition
input_stream.h:84
roo_io::InputStream::tryRead
virtual size_t tryRead(byte *result, size_t count)
Attempts to read up to count bytes into result without indefinitely blocking.
Definition
input_stream.h:67
roo_io
Definition
byte.h:6
roo_io::basic_string_view
roo::basic_string_view< CharT, Traits > basic_string_view
Definition
string_view.h:8
roo_io::count
size_t count
Definition
compare.h:45
roo_io::Status
Status
Definition
status.h:7
roo_io::kOk
@ kOk
Definition
status.h:8
roo_io::kEndOfStream
@ kEndOfStream
Definition
status.h:9
status.h
temp_repos
roo_io
src
roo_io
core
input_stream.h
Generated by
1.9.8