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
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef avro_BufferReader_hh__
#define avro_BufferReader_hh__
#include "Buffer.hh"
#include <type_traits>
#ifdef min
#undef min
#endif
/**
* \file BufferReader.hh
*
* \brief Helper class for reading bytes from buffer in a streaming manner,
* without the overhead of istreams.
*
**/
namespace avro {
/**
* Helper class for reading bytes from buffer without worrying about
* chunk boundaries. May read from an InputBuffer or OutputBuffer.
*
**/
class AVRO_DECL BufferReader : private boost::noncopyable {
public:
typedef detail::data_type data_type;
typedef detail::size_type size_type;
private:
size_type chunkRemaining() const {
return iter_->dataSize() - chunkPos_;
}
void incrementChunk(size_type howMuch) {
bytesRemaining_ -= howMuch;
chunkPos_ += howMuch;
if (chunkPos_ == iter_->dataSize()) {
chunkPos_ = 0;
++iter_;
}
}
void rewind() {
iter_ = bufferImpl_->beginRead();
bytesRemaining_ = bytes_;
chunkPos_ = 0;
}
const data_type *addr() const {
return iter_->tellReadPos() + chunkPos_;
}
public:
explicit BufferReader(const InputBuffer &buf) : bufferImpl_(buf.pimpl_),
iter_(bufferImpl_->beginRead()),
bytes_(bufferImpl_->size()),
bytesRemaining_(bytes_),
chunkPos_(0) {}
explicit BufferReader(const OutputBuffer &buf) : bufferImpl_(buf.pimpl_),
iter_(bufferImpl_->beginRead()),
bytes_(bufferImpl_->size()),
bytesRemaining_(bytes_),
chunkPos_(0) {}
/**
* How many bytes are still not read from this buffer.
**/
size_type bytesRemaining() const {
return bytesRemaining_;
}
/**
* Read a block of data from the front of the buffer.
**/
size_type bytesRead() const {
return bytes_ - bytesRemaining_;
}
/**
* Read a block of data from the buffer.
**/
size_type read(data_type *data, size_type size) {
if (size > bytesRemaining_) {
size = bytesRemaining_;
}
size_type sizeToRead = size;
while (sizeToRead) {
const size_type toRead = std::min(sizeToRead, chunkRemaining());
memcpy(data, addr(), toRead);
sizeToRead -= toRead;
data += toRead;
incrementChunk(toRead);
}
return size;
}
/**
* Read a block of data from the buffer.
**/
bool read(std::string &str, size_type size) {
if (size > bytesRemaining_) {
return false;
}
if (size <= chunkRemaining()) {
fastStringRead(str, size);
} else {
slowStringRead(str, size);
}
return true;
}
/**
* Read a single value from the buffer. The value must be a "fundamental"
* type, e.g. int, float, etc. (otherwise use the other writeTo tests).
*
**/
template<typename T>
bool read(T &val) {
return read(val, std::is_fundamental<T>());
}
/**
* Skips a block of data from the buffer.
**/
bool skip(size_type bytes) {
bool skipped = false;
if (bytes <= bytesRemaining_) {
doSkip(bytes);
skipped = true;
}
return skipped;
}
/**
* Seek to a position in the buffer.
**/
bool seek(size_type pos) {
if (pos > bytes_) {
return false;
}
size_type toSkip = pos;
size_type curPos = bytesRead();
// if the seek position is ahead, we can use skip to get there
if (pos >= curPos) {
toSkip -= curPos;
}
// if the seek position is ahead of the start of the chunk we can back up to
// start of the chunk
else if (pos >= (curPos - chunkPos_)) {
curPos -= chunkPos_;
bytesRemaining_ += chunkPos_;
chunkPos_ = 0;
toSkip -= curPos;
} else {
rewind();
}
doSkip(toSkip);
return true;
}
bool peek(char &val) {
bool ret = (bytesRemaining_ > 0);
if (ret) {
val = *(addr());
}
return ret;
}
InputBuffer copyData(size_type bytes) {
if (bytes > bytesRemaining_) {
// force no copy
bytes = 0;
}
detail::BufferImpl::SharedPtr newImpl(new detail::BufferImpl);
if (bytes) {
bufferImpl_->copyData(*newImpl, iter_, chunkPos_, bytes);
doSkip(bytes);
}
return InputBuffer(newImpl);
}
private:
void doSkip(size_type sizeToSkip) {
while (sizeToSkip) {
const size_type toSkip = std::min(sizeToSkip, chunkRemaining());
sizeToSkip -= toSkip;
incrementChunk(toSkip);
}
}
template<typename T>
bool read(T &val, const std::true_type &) {
if (sizeof(T) > bytesRemaining_) {
return false;
}
if (sizeof(T) <= chunkRemaining()) {
val = *(reinterpret_cast<const T *>(addr()));
incrementChunk(sizeof(T));
} else {
read(reinterpret_cast<data_type *>(&val), sizeof(T));
}
return true;
}
/// An uninstantiable function, that is if boost::is_fundamental check fails
template<typename T>
bool read(T &val, const std::false_type &) {
static_assert(sizeof(T) == 0, "Not a valid type to read");
return false;
}
void fastStringRead(std::string &str, size_type sizeToCopy) {
str.assign(addr(), sizeToCopy);
incrementChunk(sizeToCopy);
}
void slowStringRead(std::string &str, size_type sizeToCopy) {
str.clear();
str.reserve(sizeToCopy);
while (sizeToCopy) {
const size_type toCopy = std::min(sizeToCopy, chunkRemaining());
str.append(addr(), toCopy);
sizeToCopy -= toCopy;
incrementChunk(toCopy);
}
}
detail::BufferImpl::ConstSharedPtr bufferImpl_;
detail::BufferImpl::ChunkList::const_iterator iter_;
size_type bytes_;
size_type bytesRemaining_;
size_type chunkPos_;
};
} // namespace avro
#endif
|