aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/skiff/zerocopy_output_writer.cpp
blob: 49492b55a4a7e6c6d39f1c74373a1a7b3f68e521 (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
#include "zerocopy_output_writer.h"

namespace NSkiff {

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

TZeroCopyOutputStreamWriter::TZeroCopyOutputStreamWriter(IZeroCopyOutput* output)
    : Output_(output)
{
    ObtainNextBlock();
}

TZeroCopyOutputStreamWriter::~TZeroCopyOutputStreamWriter()
{
    if (RemainingBytes_ > 0) {
        UndoRemaining();
    }
}

void TZeroCopyOutputStreamWriter::ObtainNextBlock()
{
    if (RemainingBytes_ > 0) {
        UndoRemaining();
    }
    RemainingBytes_ = Output_->Next(&Current_);
    TotalWrittenBlockSize_ += RemainingBytes_;
}

void TZeroCopyOutputStreamWriter::UndoRemaining()
{
    Output_->Undo(RemainingBytes_);
    TotalWrittenBlockSize_ -= RemainingBytes_;
    RemainingBytes_ = 0;
}

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

} // namespace NSkiff