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
|
#include "reader.h"
#include "common.h"
#include <library/cpp/blockcodecs/codecs.h>
#include <library/cpp/json/json_reader.h>
#include <util/system/byteorder.h>
using namespace NUCompress;
TDecodedInput::TDecodedInput(IInputStream* in)
: S_(in)
{
Y_ENSURE_EX(S_, TBadArgumentException() << "Null output stream");
}
TDecodedInput::~TDecodedInput() = default;
size_t TDecodedInput::DoUnboundedNext(const void** ptr) {
if (!C_) {
TBlockLen blockLen = 0;
S_->LoadOrFail(&blockLen, sizeof(blockLen));
blockLen = LittleToHost(blockLen);
Y_ENSURE(blockLen <= MaxCompressedLen, "broken stream");
TString buf = TString::Uninitialized(blockLen);
S_->LoadOrFail(buf.Detach(), blockLen);
NJson::TJsonValue hdr;
Y_ENSURE(NJson::ReadJsonTree(buf, &hdr), "cannot parse header, suspect old format");
auto& codecName = hdr["codec"].GetString();
Y_ENSURE(codecName, "header does not have codec info");
// Throws TNotFound
C_ = NBlockCodecs::Codec(codecName);
Y_ASSERT(C_);
}
TBlockLen blockLen = 0;
size_t actualRead = S_->Load(&blockLen, sizeof(blockLen));
if (!actualRead) {
// End of stream
return 0;
}
Y_ENSURE(actualRead == sizeof(blockLen), "broken stream: cannot read block length");
blockLen = LittleToHost(blockLen);
Y_ENSURE(blockLen <= MaxCompressedLen, "broken stream");
TBuffer block;
block.Resize(blockLen);
S_->LoadOrFail(block.Data(), blockLen);
C_->Decode(block, D_);
*ptr = D_.Data();
return D_.Size();
}
|