aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/data/input_reader.cpp
blob: a2280116872107310da6b689e5b762b03e58de32 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "input_reader.h"
#include "input.h"

namespace vespalib {

size_t
InputReader::obtain_slow()
{
    _data = _input.evict(_pos).obtain();
    _bytes_evicted += _pos;
    _pos = 0;
    if (_data.size == 0) {
        _eof = true;
    }
    return size();
}

char
InputReader::read_slow()
{
    if (!failed()) {
        fail("input underflow");
    }
    return 0;
}

Memory
InputReader::read_slow(size_t bytes)
{
    _space.clear();
    while ((_space.size() < bytes) && (obtain() > 0)) {
        size_t copy_now = std::min(size(), (bytes - _space.size()));
        _space.insert(_space.end(), data(), data() + copy_now);
        _pos += copy_now;
    }
    if (_space.size() == bytes) {
        return Memory(&_space[0], _space.size());
    }
    if (!failed()) {
        fail("input underflow");
    }
    return Memory();
}

InputReader::~InputReader()
{
    _input.evict(_pos);
}

void
InputReader::fail(const vespalib::string &msg) {
    if (!failed()) {
        _error = msg;
        _input.evict(_pos);
        _data = Memory();
        _bytes_evicted += _pos;
        _pos = 0;
        _eof = true;
    }
}

} // namespace vespalib