1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#include "cru/base/io/Stream.h"
#include "cru/base/Exception.h"
#include "cru/base/Format.h"
#include <utility>
namespace cru::io {
StreamOperationNotSupportedException::StreamOperationNotSupportedException(
String operation)
: operation_(std::move(operation)) {
SetMessage(Format(u"Stream operation {} not supported.", operation_));
}
void StreamOperationNotSupportedException::CheckSeek(bool seekable) {
if (!seekable) throw StreamOperationNotSupportedException(u"seek");
}
void StreamOperationNotSupportedException::CheckRead(bool readable) {
if (!readable) throw StreamOperationNotSupportedException(u"read");
}
void StreamOperationNotSupportedException::CheckWrite(bool writable) {
if (!writable) throw StreamOperationNotSupportedException(u"write");
}
StreamClosedException::StreamClosedException()
: Exception(u"Stream is already closed.") {}
void StreamClosedException::Check(bool closed) {
if (closed) throw StreamClosedException();
}
Stream::Stream(SupportedOperations supported_operations)
: supported_operations_(std::move(supported_operations)), closed_(false) {}
Stream::Stream(std::optional<bool> can_seek, std::optional<bool> can_read,
std::optional<bool> can_write)
: Stream(SupportedOperations{can_seek, can_read, can_write}) {}
bool Stream::CanSeek() {
CheckClosed();
return DoCanSeek();
}
Index Stream::Seek(Index offset, SeekOrigin origin) {
CheckClosed();
StreamOperationNotSupportedException::CheckSeek(DoCanSeek());
return DoSeek(offset, origin);
}
Index Stream::Tell() {
CheckClosed();
return DoTell();
}
void Stream::Rewind() {
CheckClosed();
return DoRewind();
}
Index Stream::GetSize() {
CheckClosed();
return DoGetSize();
}
bool Stream::CanRead() {
CheckClosed();
return DoCanRead();
}
Index Stream::Read(std::byte* buffer, Index offset, Index size) {
CheckClosed();
StreamOperationNotSupportedException::CheckRead(DoCanRead());
return DoRead(buffer, offset, size);
}
Index Stream::Read(std::byte* buffer, Index size) {
return Read(buffer, 0, size);
}
Index Stream::Read(char* buffer, Index offset, Index size) {
return Read(reinterpret_cast<std::byte*>(buffer), offset, size);
}
Index Stream::Read(char* buffer, Index size) {
return Read(reinterpret_cast<std::byte*>(buffer), 0, size);
}
bool Stream::CanWrite() {
CheckClosed();
return DoCanWrite();
}
Index Stream::Write(const std::byte* buffer, Index offset, Index size) {
CheckClosed();
StreamOperationNotSupportedException::CheckWrite(DoCanWrite());
return DoWrite(buffer, offset, size);
}
Index Stream::Write(const std::byte* buffer, Index size) {
return Write(buffer, 0, size);
}
Index Stream::Write(const char* buffer, Index offset, Index size) {
return Write(reinterpret_cast<const std::byte*>(buffer), offset, size);
}
Index Stream::Write(const char* buffer, Index size) {
return Write(reinterpret_cast<const std::byte*>(buffer), size);
}
void Stream::Flush() {
CheckClosed();
DoFlush();
}
bool Stream::DoCanSeek() {
if (supported_operations_->can_seek) {
return *supported_operations_->can_seek;
} else {
throw Exception(
u"Can seek is neither set in supported_operations nor implemeted in "
u"virtual function.");
}
}
bool Stream::DoCanRead() {
if (supported_operations_->can_read) {
return *supported_operations_->can_read;
} else {
throw Exception(
u"Can read is neither set in supported_operations nor implemeted in "
u"virtual function.");
}
}
bool Stream::DoCanWrite() {
if (supported_operations_->can_write) {
return *supported_operations_->can_write;
} else {
throw Exception(
u"Can write is neither set in supported_operations nor implemeted in "
u"virtual function.");
}
}
Index Stream::DoSeek(Index offset, SeekOrigin origin) {
throw Exception(u"Stream is seekable but DoSeek is not implemented.");
}
Index Stream::DoTell() {
StreamOperationNotSupportedException::CheckSeek(DoCanSeek());
return DoSeek(0, SeekOrigin::Current);
}
void Stream::DoRewind() {
StreamOperationNotSupportedException::CheckSeek(DoCanSeek());
DoSeek(0, SeekOrigin::Begin);
}
Index Stream::DoGetSize() {
StreamOperationNotSupportedException::CheckSeek(DoCanSeek());
Index current_position = DoTell();
Seek(0, SeekOrigin::End);
Index size = DoTell();
Seek(current_position, SeekOrigin::Begin);
return size;
}
Index Stream::DoRead(std::byte* buffer, Index offset, Index size) {
throw Exception(u"Stream is readable but DoSeek is not implemented.");
}
Index Stream::DoWrite(const std::byte* buffer, Index offset, Index size) {
throw Exception(u"Stream is writable but DoSeek is not implemented.");
}
void Stream::DoFlush() {}
Buffer Stream::ReadToEnd(Index grow_size) {
Buffer buffer(grow_size);
while (true) {
auto read = Read(buffer.GetUsedEndPtr(), buffer.GetBackFree());
buffer.PushBackCount(read);
if (read == 0) {
break;
}
if (buffer.IsUsedReachEnd()) {
buffer.ResizeBuffer(buffer.GetBufferSize() + grow_size, true);
}
}
return buffer;
}
String Stream::ReadToEndAsUtf8String() {
auto buffer = ReadToEnd();
return String::FromUtf8(buffer);
}
} // namespace cru::io
|