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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#pragma once
#include <library/cpp/string_utils/base64/base64.h>
#include <util/generic/ptr.h>
#include <util/stream/input.h>
#include <util/stream/output.h>
class TStringDataInputStream: public IInputStream {
protected:
const char* (*GetLine)(size_t);
size_t BufferSize;
TArrayHolder<char> OutBuf;
size_t Line;
size_t LineLength;
size_t Cursor;
private:
bool NextLine() {
Cursor = 0;
const TStringBuf str(GetLine(Line));
if (str.empty()) {
LineLength = 0;
return false;
}
++Line;
const size_t size = Base64DecodeBufSize(str.size());
if (BufferSize < size) {
OutBuf.Reset(new char[size]);
BufferSize = size;
}
LineLength = Base64Decode(str, OutBuf.Get()).size();
Y_ASSERT(LineLength <= BufferSize);
return LineLength > 0;
}
size_t ReadDataFromLine(char* buf, size_t len) {
size_t n = Min(len, LineLength - Cursor);
memcpy(static_cast<void*>(buf), static_cast<void*>(OutBuf.Get() + Cursor), n);
Cursor += n;
return n;
}
protected:
size_t DoRead(void* buf, size_t len) override {
size_t readed = 0;
while (readed < len) {
readed += ReadDataFromLine(static_cast<char*>(buf) + readed, len - readed);
if (readed < len && !NextLine())
break;
}
return readed;
}
public:
TStringDataInputStream(const char* (*getLine)(size_t), size_t bufferSize = 0)
: GetLine(getLine)
, BufferSize(bufferSize)
, OutBuf(new char[BufferSize])
, Line(0)
, LineLength(0)
, Cursor(0)
{
}
};
class TStringDataOutputStream: public IOutputStream {
public:
TStringDataOutputStream(IOutputStream* out, const int maxOutStrLen)
: mStream(out)
, MaxReadLen(Base64DecodeBufSize(maxOutStrLen))
, BufRead(new unsigned char[MaxReadLen])
, BufReadOffset(0)
, BufOut(new char[maxOutStrLen + 1]){};
~TStringDataOutputStream() override {
try {
Finish();
} catch (...) {
}
}
private:
IOutputStream* mStream;
size_t MaxReadLen;
TArrayHolder<unsigned char> BufRead;
size_t BufReadOffset;
TArrayHolder<char> BufOut;
void WriteLine() {
if (BufReadOffset > 0) {
mStream->Write(" \"");
Y_ASSERT(BufReadOffset <= MaxReadLen);
char* b = BufOut.Get();
char* e = Base64Encode(b, BufRead.Get(), BufReadOffset);
mStream->Write(b, e - b);
mStream->Write("\",\n");
BufReadOffset = 0;
}
}
void DoWrite(const void* buf, size_t size) override {
size_t res = Min(MaxReadLen - BufReadOffset, size);
size_t buf_offset = 0;
while (res > 0) {
memcpy(BufRead.Get() + BufReadOffset, (const char*)buf + buf_offset, res);
BufReadOffset += res;
if (BufReadOffset < MaxReadLen)
return;
WriteLine();
buf_offset += res;
size -= res;
res = Min(MaxReadLen - BufReadOffset, size);
}
}
void DoFlush() override {
WriteLine();
}
void DoFinish() override {
DoFlush();
}
};
|