aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/length.h
blob: 049b91c05b0c3a435aa5668a8b2bee63ef5b1ab6 (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
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
#pragma once

#include "input.h"
#include "output.h" 

#include <util/generic/utility.h>

/** 
 * Proxy input stream that can read a limited number of characters from a slave 
 * stream. 
 * 
 * This can be useful for breaking up the slave stream into small chunks and 
 * treat these as separate streams. 
 */ 
class TLengthLimitedInput: public IInputStream {
public:
    inline TLengthLimitedInput(IInputStream* slave, ui64 length) noexcept
        : Slave_(slave)
        , Length_(length)
    {
    }

    ~TLengthLimitedInput() override = default;

    inline ui64 Left() const noexcept {
        return Length_;
    }

private:
    size_t DoRead(void* buf, size_t len) override;
    size_t DoSkip(size_t len) override;

private:
    IInputStream* Slave_;
    ui64 Length_;
};

/** 
 * Proxy input stream that counts the number of characters read. 
 */ 
class TCountingInput: public IInputStream {
public:
    inline TCountingInput(IInputStream* slave) noexcept
        : Slave_(slave)
        , Count_()
    {
    }

    ~TCountingInput() override = default;

    /** 
     * \returns                         The total number of characters read from 
     *                                  this stream. 
     */ 
    inline ui64 Counter() const noexcept {
        return Count_;
    }

private:
    size_t DoRead(void* buf, size_t len) override;
    size_t DoSkip(size_t len) override;
    size_t DoReadTo(TString& st, char ch) override;
    ui64 DoReadAll(IOutputStream& out) override;

private:
    IInputStream* Slave_;
    ui64 Count_;
};
 
/** 
 * Proxy output stream that counts the number of characters written. 
 */ 
class TCountingOutput: public IOutputStream {
public: 
    inline TCountingOutput(IOutputStream* slave) noexcept
        : Slave_(slave)
        , Count_()
    {
    } 
 
    ~TCountingOutput() override = default;
 
    TCountingOutput(TCountingOutput&&) noexcept = default;
    TCountingOutput& operator=(TCountingOutput&&) noexcept = default;

    /** 
     * \returns                         The total number of characters written 
     *                                  into this stream. 
     */ 
    inline ui64 Counter() const noexcept {
        return Count_; 
    } 
 
private: 
    void DoWrite(const void* buf, size_t len) override;
 
private: 
    IOutputStream* Slave_;
    ui64 Count_; 
};