aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/skiff/zerocopy_output_writer-inl.h
blob: 6da5e38f69a2f08b7444bdf45b9fbaf51ea81469 (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
#pragma once
#ifndef ZEROCOPY_OUTPUT_WRITER_INL_H_
#error "Direct inclusion of this file is not allowed, include zerocopy_output_writer.h"
// For the sake of sane code completion.
#include "zerocopy_output_writer.h"
#endif

#include <util/system/yassert.h>

namespace NSkiff {

////////////////////////////////////////////////////////////////////////////////

char* TZeroCopyOutputStreamWriter::Current() const
{
    return Current_;
}

ui64 TZeroCopyOutputStreamWriter::RemainingBytes() const
{
    return RemainingBytes_;
}

void TZeroCopyOutputStreamWriter::Advance(size_t bytes)
{
    Y_ABORT_UNLESS(bytes <= RemainingBytes_);
    Current_ += bytes;
    RemainingBytes_ -= bytes;
}

void TZeroCopyOutputStreamWriter::Write(const void* buffer, size_t length)
{
    if (length > RemainingBytes_) {
        UndoRemaining();
        Output_->Write(buffer, length);
        TotalWrittenBlockSize_ += length;
        ObtainNextBlock();
    } else {
        memcpy(Current_, buffer, length);
        Advance(length);
    }
}

ui64 TZeroCopyOutputStreamWriter::GetTotalWrittenSize() const
{
    return TotalWrittenBlockSize_ - RemainingBytes_;
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NSkiff