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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
#pragma once
#include "zerocopy.h"
#include "zerocopy_output.h"
#include <util/generic/strbuf.h>
/**
* @addtogroup Streams_Memory
* @{
*/
/**
* Input stream that reads data from a memory block.
*/
class TMemoryInput: public IZeroCopyInputFastReadTo {
public:
TMemoryInput() noexcept;
/**
* Constructs a stream that reads from the provided memory block. It's up
* to the user to make sure that the memory block doesn't get freed while
* this stream is in use.
*
* @param buf Memory block to use.
* @param len Size of the memory block.
*/
TMemoryInput(const void* buf Y_LIFETIME_BOUND, size_t len) noexcept;
TMemoryInput(TString&&) = delete;
explicit TMemoryInput(const TStringBuf buf Y_LIFETIME_BOUND) noexcept;
explicit TMemoryInput(const char* zstr Y_LIFETIME_BOUND)
: TMemoryInput(TStringBuf(zstr))
{
}
~TMemoryInput() override;
TMemoryInput(const TMemoryInput& other) noexcept
: IZeroCopyInputFastReadTo()
, Buf_(other.Buf_)
, Len_(other.Len_)
{
}
TMemoryInput& operator=(const TMemoryInput& other) noexcept {
if (this != &other) {
Buf_ = other.Buf_;
Len_ = other.Len_;
}
return *this;
}
TMemoryInput(TMemoryInput&&) noexcept = default;
TMemoryInput& operator=(TMemoryInput&&) noexcept = default;
/**
* Initializes this stream with a new memory block. It's up to the
* user to make sure that the memory block doesn't get freed while this
* stream is in use.
*
* @param buf New memory block to use.
* @param len Size of the new memory block.
*/
void Reset(const void* buf, size_t len) noexcept {
Buf_ = (const char*)buf;
Len_ = len;
}
/**
* @returns Whether there is more data in the stream.
*/
bool Exhausted() const noexcept {
return !Avail();
}
/**
* @returns Number of bytes available in the stream.
*/
size_t Avail() const noexcept {
return Len_;
}
/**
* @returns Current read position in the memory block
* used by this stream.
*/
const char* Buf() const noexcept {
return Buf_;
}
/**
* Initializes this stream with a next chunk extracted from the given zero
* copy stream.
*
* @param stream Zero copy stream to initialize from.
*/
void Fill(IZeroCopyInput* stream) {
Len_ = stream->Next(&Buf_);
if (!Len_) {
Reset(nullptr, 0);
}
}
private:
size_t DoNext(const void** ptr, size_t len) override;
void DoUndo(size_t len) override;
private:
const char* Buf_;
size_t Len_;
};
/**
* Output stream that writes data to a memory block.
*/
class TMemoryOutput: public IZeroCopyOutput {
public:
/**
* Constructs a stream that writes to the provided memory block. It's up
* to the user to make sure that the memory block doesn't get freed while
* this stream is in use.
*
* @param buf Memory block to use.
* @param len Size of the memory block.
*/
TMemoryOutput(void* buf, size_t len) noexcept
: Buf_(static_cast<char*>(buf))
, End_(Buf_ + len)
{
}
~TMemoryOutput() override;
TMemoryOutput(TMemoryOutput&&) noexcept = default;
TMemoryOutput& operator=(TMemoryOutput&&) noexcept = default;
/**
* Initializes this stream with a new memory block. It's up to the
* user to make sure that the memory block doesn't get freed while this
* stream is in use.
*
* @param buf New memory block to use.
* @param len Size of the new memory block.
*/
inline void Reset(void* buf, size_t len) noexcept {
Buf_ = static_cast<char*>(buf);
End_ = Buf_ + len;
}
/**
* @returns Whether there is more space in the
* stream for writing.
*/
inline bool Exhausted() const noexcept {
return !Avail();
}
/**
* @returns Number of bytes available for writing
* in the stream.
*/
inline size_t Avail() const noexcept {
return End_ - Buf_;
}
/**
* @returns Current write position in the memory block
* used by this stream.
*/
inline char* Buf() const noexcept {
return Buf_;
}
/**
* @returns Pointer to the end of the memory block
* used by this stream.
*/
char* End() const {
return End_;
}
private:
size_t DoNext(void** ptr) override;
void DoUndo(size_t len) override;
void DoWrite(const void* buf, size_t len) override;
void DoWriteC(char c) override;
protected:
char* Buf_;
char* End_;
};
/**
* Memory output stream that supports changing the position of the
* write pointer.
*
* @see TMemoryOutput
*/
class TMemoryWriteBuffer: public TMemoryOutput {
public:
TMemoryWriteBuffer(void* buf, size_t len)
: TMemoryOutput(buf, len)
, Beg_(Buf_)
{
}
void Reset(void* buf, size_t len) {
TMemoryOutput::Reset(buf, len);
Beg_ = Buf_;
}
size_t Len() const {
return Buf() - Beg();
}
size_t Empty() const {
return Buf() == Beg();
}
/**
* @returns Data that has been written into this
* stream as a string.
*/
TStringBuf Str() const {
return TStringBuf(Beg(), Buf());
}
char* Beg() const {
return Beg_;
}
/**
* @param ptr New write position for this stream.
* Must be inside the memory block that
* this stream uses.
*/
void SetPos(char* ptr) {
Y_ASSERT(Beg_ <= ptr);
SetPosImpl(ptr);
}
/**
* @param pos New write position for this stream,
* relative to the beginning of the memory
* block that this stream uses.
*/
void SetPos(size_t pos) {
SetPosImpl(Beg_ + pos);
}
protected:
void SetPosImpl(char* ptr) {
Y_ASSERT(End_ >= ptr);
Buf_ = ptr;
}
protected:
char* Beg_;
};
/** @} */
|