forked from pjmikkol/bwtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreams.hpp
176 lines (147 loc) · 4.37 KB
/
Streams.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @file Streams.hpp
* @author Pekka Mikkola <[email protected]>
* @author Dominik Kempa <[email protected]>
*
* @section LICENSE
*
* This file is part of bwtc.
*
* bwtc is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* bwtc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bwtc. If not, see <http://www.gnu.org/licenses/>.
*
* @section DESCRIPTION
*
* Header for OutStream and InStream.
*/
#ifndef BWTC_STREAMS_HPP_
#define BWTC_STREAMS_HPP_
#include <cstdio>
#include <cassert>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include "globaldefs.hpp"
namespace bwtc {
class OutStream {
public:
virtual ~OutStream() {}
virtual void writeByte(byte b) = 0;
virtual void writeBlock(const byte *begin, const byte *end) = 0;
virtual long int getPos() = 0;
virtual void write48bits(uint64 to_written, long int position) = 0;
virtual void flush() = 0;
};
class InStream {
public:
virtual ~InStream() {}
virtual size_t readBlock(byte *to, size_t max_block_size) = 0;
virtual bool readBit() = 0;
virtual byte readByte() = 0;
virtual void flushBuffer() = 0;
virtual uint64 read48bits() = 0;
virtual bool compressedDataEnding() = 0;
int pos;
};
/**
* OutStream writes data into file or std::cout.
*
* On the compression pipeline OutStream-object is located at the end of
* the pipeline. It is owned by Compressor-object which control the states of
* stream. The actual writes are done by EntropyEncoder.
*
* OutStream is also at the end of the decompression pipeline right after
* the postprocessors.
*
* @see Compressor
*/
class RawOutStream : public OutStream {
public:
explicit RawOutStream(const std::string& file_name);
virtual ~RawOutStream();
/**
* Writes given byte into target.
*
*@param b byte to be written
*/
virtual void writeByte(byte b);
/**
* Writes bytes from range [begin, end) to stream
*/
virtual void writeBlock(const byte *begin, const byte *end);
bool isStdout() const { return m_fileptr == stdout; }
virtual long int getPos();
virtual void write48bits(uint64 to_written, long int position);
virtual void flush();
private:
static const uint32 kBufferSize = 1 << 16; // 64KB
std::string m_name;
FILE *m_fileptr;
uint32 m_filled;
byte *m_buffer;
RawOutStream& operator=(const RawOutStream& os);
RawOutStream(const RawOutStream& os);
};
class RawInStream : public InStream {
public:
explicit RawInStream(const std::string &file_name);
virtual ~RawInStream();
/* Copies block from stream to given byte array.
* Returns the number of read bytes. */
virtual size_t readBlock(byte *to, size_t max_block_size);
virtual inline bool readBit() {
if (m_bitsInBuffer == 0) {
m_buffer = static_cast<byte>(fetchByte());
m_bitsInBuffer = 8;
}
return (m_buffer >> --m_bitsInBuffer) & 1;
}
virtual inline byte readByte() {
assert(m_bitsInBuffer < 8);
byte nextByte = static_cast<byte>(fetchByte());
m_buffer = (m_buffer << 8) | nextByte;
return (m_buffer >> m_bitsInBuffer) & 0xff;
}
virtual inline void flushBuffer() {
m_bitsInBuffer = 0;
}
bool isStdin() const { return m_fileptr == stdin; }
virtual uint64 read48bits();
virtual bool compressedDataEnding() {
/* Quick workaround. For some mysterious reason there is single
* additional byte in the end of compressed file. It seems that
* BitEncoder is responsible for this. */
if (m_bigbuf_left > 1) return false;
if (m_bigbuf_left == 0) {
return (peekByte() == EOF);
} else {
return feof(m_fileptr);
}
}
private:
static const uint32 kBigbufSize = 1 << 16; // 64KB
std::string m_name;
FILE *m_fileptr;
uint16 m_buffer;
byte m_bitsInBuffer;
byte *m_bigbuf;
int32 m_bigbuf_left;
uint32 m_bigbuf_pos;
int fetchByte();
int peekByte();
RawInStream& operator=(const RawInStream& os);
RawInStream(const RawInStream& os);
};
} //namespace bwtc
#endif