Repository navigation
Expand file tree
/
Copy pathBzip2WriteBuffer.cpp
More file actions
94 lines (72 loc) · 1.96 KB
/
Copy pathBzip2WriteBuffer.cpp
File metadata and controls
94 lines (72 loc) · 1.96 KB
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
#include "config.h"
#if USE_BZIP2
# include <IO/Bzip2WriteBuffer.h>
# include <bzlib.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BZIP2_STREAM_ENCODER_FAILED;
}
Bzip2WriteBuffer::~Bzip2WriteBuffer()
{
BZ2_bzCompressEnd(&stream);
}
void Bzip2WriteBuffer::nextImpl()
{
if (!offset())
{
return;
}
stream.next_in = working_buffer.begin();
stream.avail_in = static_cast<unsigned>(offset());
try
{
do
{
out->nextIfAtEnd();
stream.next_out = out->position();
stream.avail_out = static_cast<unsigned>(out->buffer().end() - out->position());
int ret = BZ2_bzCompress(&stream, BZ_RUN);
out->position() = out->buffer().end() - stream.avail_out;
if (ret != BZ_RUN_OK)
throw Exception(
ErrorCodes::BZIP2_STREAM_ENCODER_FAILED,
"bzip2 stream encoder failed: error code: {}",
ret);
}
while (stream.avail_in > 0);
total_in += offset();
}
catch (...)
{
/// Do not try to write next time after exception.
out->position() = out->buffer().begin();
throw;
}
}
void Bzip2WriteBuffer::finalFlushBefore()
{
next();
/// Don't write out if no data was ever compressed
if (!compress_empty && total_in == 0)
return;
do
{
out->nextIfAtEnd();
stream.next_out = out->position();
stream.avail_out = static_cast<unsigned>(out->buffer().end() - out->position());
int ret = BZ2_bzCompress(&stream, BZ_FINISH);
out->position() = out->buffer().end() - stream.avail_out;
if (ret == BZ_STREAM_END)
break;
if (ret != BZ_FINISH_OK)
throw Exception(
ErrorCodes::BZIP2_STREAM_ENCODER_FAILED,
"bzip2 stream encoder failed: error code: {}",
ret);
}
while (true);
}
}
#endif