-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock_wrappers.h
128 lines (89 loc) · 3.75 KB
/
block_wrappers.h
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
// Author: [email protected] (Peter Waller)
// Author: [email protected] (Johannes Ebke)
#ifndef PROTOBUF_ZEROCOPY_COMPRESSION_BLOCK_WRAPPER_H_
#define PROTOBUF_ZEROCOPY_COMPRESSION_BLOCK_WRAPPER_H_
#include <google/protobuf/io/zero_copy_stream.h>
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
#include <google/protobuf/io/coded_stream.h>
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::CodedOutputStream;
#include <compressed_stream.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
namespace zerocc {
// This class provides scaffolding for implementing compression streams based
// on compression algorithms that do not support streaming operations but must
// be operated blockwise.
class BlockCompressionInputStream : public AbstractCompressedInputStream {
public:
explicit BlockCompressionInputStream(ZeroCopyInputStream* sub_stream);
virtual ~BlockCompressionInputStream();
bool Next(const void** data, int* size);
void BackUp(int count);
bool Skip(int count);
int64_t ByteCount() const { return _byte_count; }
bool ExpectAtEnd() { return true; }
protected:
virtual void RawUncompress(char* input_buffer, uint32_t compressed_size) = 0;
char* _output_buffer;
size_t _output_buffer_size;
private:
CodedInputStream* _sub_stream;
ZeroCopyInputStream* _raw_stream;
int _backed_up_bytes;
size_t _byte_count;
void reset_input_stream();
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(BlockCompressionInputStream);
};
class BlockCompressionOutputStream : public AbstractCompressedOutputStream {
public:
// Create a BlockCompressionOutputStream with default options.
explicit BlockCompressionOutputStream(ZeroCopyOutputStream* sub_stream);
virtual ~BlockCompressionOutputStream();
bool Next(void** data, int* size);
void BackUp(int count);
int64_t ByteCount() const { return _byte_count; };
bool Flush();
bool Close() { return Flush(); }
protected:
virtual uint32_t MaxCompressedLength(size_t input_size) = 0;
virtual uint32_t RawCompress(char* input_buffer, size_t input_size, char* output_buffer) = 0;
char* _input_buffer;
private:
CodedOutputStream* _sub_stream;
int _backed_up_bytes;
size_t _byte_count;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(BlockCompressionOutputStream);
};
#ifdef HAVE_SNAPPY
class SnappyInputStream : public BlockCompressionInputStream {
public:
explicit SnappyInputStream(ZeroCopyInputStream* sub_stream) : BlockCompressionInputStream(sub_stream) {};
virtual void RawUncompress(char* input_buffer, uint32_t compressed_size);
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SnappyInputStream);
};
class SnappyOutputStream : public BlockCompressionOutputStream {
public:
explicit SnappyOutputStream(ZeroCopyOutputStream* sub_stream) : BlockCompressionOutputStream(sub_stream) {};
virtual uint32_t MaxCompressedLength(size_t input_size);
virtual uint32_t RawCompress(char* input_buffer, size_t input_size, char* output_buffer);
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SnappyOutputStream);
};
#endif
class LZ4InputStream : public BlockCompressionInputStream {
public:
explicit LZ4InputStream(ZeroCopyInputStream* sub_stream) : BlockCompressionInputStream(sub_stream) {};
virtual void RawUncompress(char* input_buffer, uint32_t compressed_size);
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LZ4InputStream);
};
class LZ4OutputStream : public BlockCompressionOutputStream {
public:
explicit LZ4OutputStream(ZeroCopyOutputStream* sub_stream) : BlockCompressionOutputStream(sub_stream) {};
virtual uint32_t MaxCompressedLength(size_t input_size);
virtual uint32_t RawCompress(char* input_buffer, size_t input_size, char* output_buffer);
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LZ4OutputStream);
};
}; //namespace zerocc
#endif