aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/streams/growing_file_input/growing_file_input.cpp
blob: 0bbfa5ade9e406eb6691ec82cadab5932eec9531 (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
#include "growing_file_input.h"

#include <util/datetime/base.h>
#include <util/generic/yexception.h>

TGrowingFileInput::TGrowingFileInput(const TString& path)
    : File_(path, OpenExisting | RdOnly | Seq)
{
    if (!File_.IsOpen()) {
        ythrow TIoException() << "file " << path << " not open";
    }

    File_.Seek(0, sEnd);
}

TGrowingFileInput::TGrowingFileInput(const TFile& file)
    : File_(file)
{
    if (!File_.IsOpen()) {
        ythrow TIoException() << "file (" << file.GetName() << ") not open";
    }

    File_.Seek(0, sEnd);
}

size_t TGrowingFileInput::DoRead(void* buf, size_t len) {
    for (int sleepTime = 1;;) {
        size_t rr = File_.Read(buf, len);

        if (rr != 0) {
            return rr;
        }

        NanoSleep((ui64)sleepTime * 1000000);

        if (sleepTime < 2000) {
            sleepTime <<= 1;
        }
    }
}