aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/file.cpp
blob: dcf89a48ab78a8503df23a6b06e095c1abf80409 (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
101
102
103
104
105
106
107
108
109
110
111
#include "file.h"

#include <util/memory/blob.h>
#include <util/generic/yexception.h>

TUnbufferedFileInput::TUnbufferedFileInput(const char* path)
    : TUnbufferedFileInput(TFile(path, OPEN_MODE))
{
}

TUnbufferedFileInput::TUnbufferedFileInput(const TString& path)
    : TUnbufferedFileInput(TFile(path, OPEN_MODE))
{
}

TUnbufferedFileInput::TUnbufferedFileInput(const std::filesystem::path& path)
    : TUnbufferedFileInput(TFile(path, OPEN_MODE))
{
}

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

size_t TUnbufferedFileInput::DoRead(void* buf, size_t len) {
    return File_.ReadOrFail(buf, len);
}

size_t TUnbufferedFileInput::DoSkip(size_t len) {
    if (len < 384) {
        /* Base implementation calls DoRead, which results in one system call
         * instead of three as in fair skip implementation. For small sizes
         * actually doing one read is cheaper. Experiments show that the
         * border that separates two implementations performance-wise lies
         * in the range of 384-512 bytes (assuming that the file is in OS cache). */
        return IInputStream::DoSkip(len);
    }

    /* TFile::Seek can seek beyond the end of file, so we need to do
     * size check here. */
    i64 size = File_.GetLength();
    i64 oldPos = File_.GetPosition();
    i64 newPos = File_.Seek(Min<i64>(size, oldPos + len), sSet);

    return newPos - oldPos;
}

TUnbufferedFileOutput::TUnbufferedFileOutput(const char* path)
    : TUnbufferedFileOutput(TFile(path, OPEN_MODE))
{
}

TUnbufferedFileOutput::TUnbufferedFileOutput(const TString& path)
    : TUnbufferedFileOutput(TFile(path, OPEN_MODE))
{
}

TUnbufferedFileOutput::TUnbufferedFileOutput(const std::filesystem::path& path)
    : TUnbufferedFileOutput(TFile(path, OPEN_MODE))
{
}

TUnbufferedFileOutput::TUnbufferedFileOutput(const TFile& file)
    : File_(file)
{
    if (!File_.IsOpen()) {
        ythrow TIoException() << "closed file(" << file.GetName() << ") passed";
    }
}

TUnbufferedFileOutput::~TUnbufferedFileOutput() = default;

void TUnbufferedFileOutput::DoWrite(const void* buf, size_t len) {
    File_.Write(buf, len);
}

void TUnbufferedFileOutput::DoFlush() {
    if (File_.IsOpen()) {
        File_.Flush();
    }
}

class TMappedFileInput::TImpl: public TBlob {
public:
    inline TImpl(const TFile& file)
        : TBlob(TBlob::FromFile(file))
    {
    }

    inline ~TImpl() = default;
};

TMappedFileInput::TMappedFileInput(const TFile& file)
    : TMemoryInput(nullptr, 0)
    , Impl_(new TImpl(file))
{
    Reset(Impl_->Data(), Impl_->Size());
}

TMappedFileInput::TMappedFileInput(const TString& path)
    : TMemoryInput(nullptr, 0)
    , Impl_(new TImpl(TFile(path, OpenExisting | RdOnly)))
{
    Reset(Impl_->Data(), Impl_->Size());
}

TMappedFileInput::~TMappedFileInput() = default;