-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBZip2BlockCompressor.java
More file actions
310 lines (243 loc) · 8.87 KB
/
BZip2BlockCompressor.java
File metadata and controls
310 lines (243 loc) · 8.87 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (c) 2011 Matthew Francis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.itadaki.bzip2;
import java.io.IOException;
/*
* Block encoding consists of the following stages:
* 1. Run-Length Encoding[1] - write()
* 2. Burrows Wheeler Transform - close() (through BZip2DivSufSort)
* 3. Write block header - close()
* 4. Move To Front Transform - close() (through BZip2HuffmanStageEncoder)
* 5. Run-Length Encoding[2] - close() (through BZip2HuffmanStageEncoder)
* 6. Create and write Huffman tables - close() (through BZip2HuffmanStageEncoder)
* 7. Huffman encode and write data - close() (through BZip2HuffmanStageEncoder)
*/
/**
* Compresses and writes a single BZip2 block
*/
public class BZip2BlockCompressor {
/**
* The stream to which compressed BZip2 data is written
*/
private final BZip2BitOutputStream bitOutputStream;
/**
* CRC builder for the block
*/
private final CRC32 crc = new CRC32();
/**
* The RLE'd block data
*/
private final byte[] block;
/**
* Current length of the data within the {@link block} array
*/
private int blockLength = 0;
/**
* A limit beyond which new data will not be accepted into the block
*/
private final int blockLengthLimit;
/**
* The values that are present within the RLE'd block data. For each index, {@code true} if that
* value is present within the data, otherwise {@code false}
*/
private final boolean[] blockValuesPresent = new boolean[256];
/**
* The Burrows Wheeler Transformed block data
*/
private final int[] bwtBlock;
/**
* The current RLE value being accumulated (undefined when {@link #rleLength} is 0)
*/
private int rleCurrentValue = -1;
/**
* The repeat count of the current RLE value
*/
private int rleLength = 0;
/**
* Write the Huffman symbol to output byte map
* @throws IOException on any I/O error writing the data
*/
private void writeSymbolMap() throws IOException {
BZip2BitOutputStream bitOutputStream = this.bitOutputStream;
final boolean[] blockValuesPresent = this.blockValuesPresent;
final boolean[] condensedInUse = new boolean[16];
for (int i = 0; i < 16; i++) {
for (int j = 0, k = i << 4; j < 16; j++, k++) {
if (blockValuesPresent[k]) {
condensedInUse[i] = true;
}
}
}
for (int i = 0; i < 16; i++) {
bitOutputStream.writeBoolean (condensedInUse[i]);
}
for (int i = 0; i < 16; i++) {
if (condensedInUse[i]) {
for (int j = 0, k = i * 16; j < 16; j++, k++) {
bitOutputStream.writeBoolean (blockValuesPresent[k]);
}
}
}
}
/**
* Writes an RLE run to the block array, updating the block CRC and present values array as required
* @param value The value to write
* @param runLength The run length of the value to write
*/
private void writeRun (final int value, int runLength) {
final int blockLength = this.blockLength;
final byte[] block = this.block;
this.blockValuesPresent[value] = true;
this.crc.updateCRC (value, runLength);
final byte byteValue = (byte)value;
switch (runLength) {
case 1:
block[blockLength] = byteValue;
this.blockLength = blockLength + 1;
break;
case 2:
block[blockLength] = byteValue;
block[blockLength + 1] = byteValue;
this.blockLength = blockLength + 2;
break;
case 3:
block[blockLength] = byteValue;
block[blockLength + 1] = byteValue;
block[blockLength + 2] = byteValue;
this.blockLength = blockLength + 3;
break;
default:
runLength -= 4;
this.blockValuesPresent[runLength] = true;
block[blockLength] = byteValue;
block[blockLength + 1] = byteValue;
block[blockLength + 2] = byteValue;
block[blockLength + 3] = byteValue;
block[blockLength + 4] = (byte)runLength;
this.blockLength = blockLength + 5;
break;
}
}
/**
* Writes a byte to the block, accumulating to an RLE run where possible
* @param value The byte to write
* @return {@code true} if the byte was written, or {@code false} if the block is already full
*/
public boolean write (final int value) {
if (this.blockLength > this.blockLengthLimit) {
return false;
}
final int rleCurrentValue = this.rleCurrentValue;
final int rleLength = this.rleLength;
if (rleLength == 0) {
this.rleCurrentValue = value;
this.rleLength = 1;
} else if (rleCurrentValue != value) {
// This path commits us to write 6 bytes - one RLE run (5 bytes) plus one extra
writeRun (rleCurrentValue & 0xff, rleLength);
this.rleCurrentValue = value;
this.rleLength = 1;
} else {
if (rleLength == 254) {
writeRun (rleCurrentValue & 0xff, 255);
this.rleLength = 0;
} else {
this.rleLength = rleLength + 1;
}
}
return true;
}
/**
* Writes an array to the block
* @param data The array to write
* @param offset The offset within the input data to write from
* @param length The number of bytes of input data to write
* @return The actual number of input bytes written. May be less than the number requested, or
* zero if the block is already full
*/
public int write (final byte[] data, int offset, int length) {
int written = 0;
while (length-- > 0) {
if (!write (data[offset++])) {
break;
}
written++;
}
return written;
}
/**
* Compresses and writes out the block
* @throws IOException on any I/O error writing the data
*/
public void close() throws IOException {
// If an RLE run is in progress, write it out
if (this.rleLength > 0) {
writeRun (this.rleCurrentValue & 0xff, this.rleLength);
}
// Apply a one byte block wrap required by the BWT implementation
this.block[this.blockLength] = this.block[0];
// Perform the Burrows Wheeler Transform
BZip2DivSufSort divSufSort = new BZip2DivSufSort (this.block, this.bwtBlock, this.blockLength);
int bwtStartPointer = divSufSort.bwt();
// Write out the block header
this.bitOutputStream.writeBits (24, BZip2Constants.BLOCK_HEADER_MARKER_1);
this.bitOutputStream.writeBits (24, BZip2Constants.BLOCK_HEADER_MARKER_2);
this.bitOutputStream.writeInteger (this.crc.getCRC());
this.bitOutputStream.writeBoolean (false); // Randomised block flag. We never create randomised blocks
this.bitOutputStream.writeBits (24, bwtStartPointer);
// Write out the symbol map
writeSymbolMap();
// Perform the Move To Front Transform and Run-Length Encoding[2] stages
BZip2MTFAndRLE2StageEncoder mtfEncoder = new BZip2MTFAndRLE2StageEncoder (this.bwtBlock, this.blockLength, this.blockValuesPresent);
mtfEncoder.encode();
// Perform the Huffman Encoding stage and write out the encoded data
BZip2HuffmanStageEncoder huffmanEncoder = new BZip2HuffmanStageEncoder (this.bitOutputStream, mtfEncoder.getMtfBlock(), mtfEncoder.getMtfLength(), mtfEncoder.getMtfAlphabetSize(), mtfEncoder.getMtfSymbolFrequencies());
huffmanEncoder.encode();
}
/**
* Determines if any bytes have been written to the block
* @return {@code true} if one or more bytes has been written to the block, otherwise
* {@code false}
*/
public boolean isEmpty() {
return ((this.blockLength == 0) && (this.rleLength == 0));
}
/**
* Gets the CRC of the completed block. Only valid after calling {@link #close()}
* @return The block's CRC
*/
public int getCRC() {
return this.crc.getCRC();
}
/**
* @param bitOutputStream The BZip2BitOutputStream to which compressed BZip2 data is written
* @param blockSize The declared block size in bytes. Up to this many bytes will be accepted
* into the block after Run-Length Encoding is applied
*/
public BZip2BlockCompressor (final BZip2BitOutputStream bitOutputStream, final int blockSize) {
this.bitOutputStream = bitOutputStream;
// One extra byte is added to allow for the block wrap applied in close()
this.block = new byte[blockSize + 1];
this.bwtBlock = new int[blockSize + 1];
this.blockLengthLimit = blockSize - 6; // 5 bytes for one RLE run plus one byte - see {@link #write(int)}
}
}