diff options
| author | robot-piglet <[email protected]> | 2023-12-02 01:45:21 +0300 | 
|---|---|---|
| committer | robot-piglet <[email protected]> | 2023-12-02 02:42:50 +0300 | 
| commit | 9c43d58f75cf086b744cf4fe2ae180e8f37e4a0c (patch) | |
| tree | 9f88a486917d371d099cd712efd91b4c122d209d /library/cpp/streams | |
| parent | 32fb6dda1feb24f9ab69ece5df0cb9ec238ca5e6 (diff) | |
Intermediate changes
Diffstat (limited to 'library/cpp/streams')
3 files changed, 74 insertions, 0 deletions
| diff --git a/library/cpp/streams/growing_file_input/growing_file_input.cpp b/library/cpp/streams/growing_file_input/growing_file_input.cpp new file mode 100644 index 00000000000..0bbfa5ade9e --- /dev/null +++ b/library/cpp/streams/growing_file_input/growing_file_input.cpp @@ -0,0 +1,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; +        } +    } +} diff --git a/library/cpp/streams/growing_file_input/growing_file_input.h b/library/cpp/streams/growing_file_input/growing_file_input.h new file mode 100644 index 00000000000..9054a5f3dac --- /dev/null +++ b/library/cpp/streams/growing_file_input/growing_file_input.h @@ -0,0 +1,23 @@ +#pragma once + +#include <util/stream/input.h> +#include <util/system/file.h> + +/** + * Growing file input stream. + * + * File descriptor offsets to the end of the file, when the object is created. + * + * Read function waites for reading at least one byte. + */ +class TGrowingFileInput: public IInputStream { +public: +    TGrowingFileInput(const TFile& file); +    TGrowingFileInput(const TString& path); + +private: +    size_t DoRead(void* buf, size_t len) override; + +private: +    TFile File_; +}; diff --git a/library/cpp/streams/growing_file_input/ya.make b/library/cpp/streams/growing_file_input/ya.make new file mode 100644 index 00000000000..69c56fea462 --- /dev/null +++ b/library/cpp/streams/growing_file_input/ya.make @@ -0,0 +1,11 @@ +LIBRARY() + +SRCS( +    growing_file_input.cpp +) + +END() + +RECURSE_FOR_TESTS( +    ut +) | 
