blob: 34468a2ffebe1c0cdd550e2420eeca8aac3df758 (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include "input.h"
namespace vbench {
/**
* Concrete utility class used to read input data one byte at a time
* by wrapping a generic Input implementation.
**/
class ByteInput
{
private:
Input &_input;
Memory _data;
size_t _pos;
size_t _chunkSize;
public:
/**
* Wrap an Input to read one byte at a time.
*
* @param input the underlying Input
* @param chunkSize how much data to request from the input per transaction
**/
ByteInput(Input &input, size_t chunkSize)
: _input(input), _data(), _pos(0), _chunkSize(chunkSize) {}
~ByteInput() { _input.evict(_pos); }
/**
* Read the next byte of input.
*
* @return next byte of input, or -1 if no more input is available
**/
int get() {
if (_pos < _data.size) {
return (_data.data[_pos++] & 0xff);
} else {
_data = _input.evict(_pos).obtain(_chunkSize, 1);
if ((_pos = 0) < _data.size) {
return (_data.data[_pos++] & 0xff);
}
}
return -1;
}
};
} // namespace vbench
|