/
LzoDecompressor.java
376 lines (321 loc) · 10.2 KB
/
LzoDecompressor.java
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* This file is part of Hadoop-Gpl-Compression.
*
* Hadoop-Gpl-Compression 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.
*
* Hadoop-Gpl-Compression 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 Hadoop-Gpl-Compression. If not, see
* <http://www.gnu.org/licenses/>.
*/
package com.hadoop.compression.lzo;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.compress.Decompressor;
/**
* A {@link Decompressor} based on the lzo algorithm.
* http://www.oberhumer.com/opensource/lzo/
*
*/
class LzoDecompressor implements Decompressor {
private static final Log LOG =
LogFactory.getLog(LzoDecompressor.class.getName());
// HACK - Use this as a global lock in the JNI layer
@SuppressWarnings({ "unchecked", "unused" })
private static Class clazz = LzoDecompressor.class;
/**
* The minimum version of LZO that we can read.
* Set to 1.0 since there were a couple header
* size changes prior to that.
* See read_header() in lzop.c
*/
public static int MINIMUM_LZO_VERSION = 0x0100;
private int directBufferSize;
private Buffer compressedDirectBuf = null;
private int compressedDirectBufLen;
private Buffer uncompressedDirectBuf = null;
private byte[] userBuf = null;
private int userBufOff = 0, userBufLen = 0;
private boolean finished;
// Whether or not the current block being is actually stored uncompressed.
// This happens when compressing a block would increase it size.
private boolean isCurrentBlockUncompressed;
private CompressionStrategy strategy;
@SuppressWarnings("unused")
private long lzoDecompressor = 0; // The actual lzo decompression function.
public static enum CompressionStrategy {
/**
* lzo1 algorithms.
*/
LZO1 (0),
/**
* lzo1a algorithms.
*/
LZO1A (1),
/**
* lzo1b algorithms.
*/
LZO1B (2),
LZO1B_SAFE(3),
/**
* lzo1c algorithms.
*/
LZO1C (4),
LZO1C_SAFE(5),
LZO1C_ASM (6),
LZO1C_ASM_SAFE (7),
/**
* lzo1f algorithms.
*/
LZO1F (8),
LZO1F_SAFE (9),
LZO1F_ASM_FAST (10),
LZO1F_ASM_FAST_SAFE (11),
/**
* lzo1x algorithms.
*/
LZO1X (12),
LZO1X_SAFE (13),
LZO1X_ASM (14),
LZO1X_ASM_SAFE (15),
LZO1X_ASM_FAST (16),
LZO1X_ASM_FAST_SAFE (17),
/**
* lzo1y algorithms.
*/
LZO1Y (18),
LZO1Y_SAFE (19),
LZO1Y_ASM (20),
LZO1Y_ASM_SAFE (21),
LZO1Y_ASM_FAST (22),
LZO1Y_ASM_FAST_SAFE (23),
/**
* lzo1z algorithms.
*/
LZO1Z (24),
LZO1Z_SAFE (25),
/**
* lzo2a algorithms.
*/
LZO2A (26),
LZO2A_SAFE (27);
private final int decompressor;
private CompressionStrategy(int decompressor) {
this.decompressor = decompressor;
}
int getDecompressor() {
return decompressor;
}
}; // CompressionStrategy
private static boolean nativeLzoLoaded;
public static final int LZO_LIBRARY_VERSION;
static {
if (GPLNativeCodeLoader.isNativeCodeLoaded()) {
// Initialize the native library
try {
initIDs();
nativeLzoLoaded = true;
} catch (Throwable t) {
// Ignore failure to load/initialize native-lzo
LOG.warn(t.toString());
nativeLzoLoaded = false;
}
LZO_LIBRARY_VERSION = (nativeLzoLoaded) ? 0xFFFF & getLzoLibraryVersion()
: -1;
} else {
LOG.error("Cannot load " + LzoDecompressor.class.getName() +
" without native-hadoop library!");
nativeLzoLoaded = false;
LZO_LIBRARY_VERSION = -1;
}
}
/**
* Check if lzo decompressors are loaded and initialized.
*
* @return <code>true</code> if lzo decompressors are loaded & initialized,
* else <code>false</code>
*/
public static boolean isNativeLzoLoaded() {
return nativeLzoLoaded;
}
/**
* Creates a new lzo decompressor.
*
* @param strategy lzo decompression algorithm
* @param directBufferSize size of the direct-buffer
*/
public LzoDecompressor(CompressionStrategy strategy, int directBufferSize) {
this.directBufferSize = directBufferSize;
this.strategy = strategy;
compressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
uncompressedDirectBuf.position(directBufferSize);
/**
* Initialize {@link #lzoDecompress}
*/
init(this.strategy.getDecompressor());
}
/**
* Creates a new lzo decompressor.
*/
public LzoDecompressor() {
this(CompressionStrategy.LZO1X, 64*1024);
}
public synchronized void setInput(byte[] b, int off, int len) {
if (!isCurrentBlockUncompressed()) {
if (len > directBufferSize) {
LOG.warn("Decompression will fail because compressed buffer size :" +
len + " is greater than this decompressor's directBufferSize: " +
directBufferSize + ". To fix this, You can increase the value of your " +
"configuration's io.compression.codec.lzo.buffersize to be larger " +
"than: " + len + " and restart your job.");
}
}
if (b == null) {
throw new NullPointerException();
}
if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
this.userBuf = b;
this.userBufOff = off;
this.userBufLen = len;
setInputFromSavedData();
// Reinitialize lzo's output direct-buffer
uncompressedDirectBuf.limit(directBufferSize);
uncompressedDirectBuf.position(directBufferSize);
}
synchronized void setInputFromSavedData() {
// If the current block is stored uncompressed, no need
// to ready all the lzo machinery, because it will be bypassed.
if (!isCurrentBlockUncompressed()) {
compressedDirectBufLen = Math.min(userBufLen, directBufferSize);
// Reinitialize lzo's input direct-buffer
compressedDirectBuf.rewind();
((ByteBuffer)compressedDirectBuf).put(userBuf, userBufOff,
compressedDirectBufLen);
// Note how much data is being fed to lzo
userBufOff += compressedDirectBufLen;
userBufLen -= compressedDirectBufLen;
}
}
public synchronized void setDictionary(byte[] b, int off, int len) {
// nop
}
public synchronized boolean needsInput() {
// Consume remaining compressed data?
if (uncompressedDirectBuf.remaining() > 0) {
return false;
}
// Check if lzo has consumed all input
if (compressedDirectBufLen <= 0) {
// Check if we have consumed all user-input
if (userBufLen <= 0) {
return true;
} else {
setInputFromSavedData();
}
}
return false;
}
public synchronized boolean needsDictionary() {
return false;
}
public synchronized boolean finished() {
// Check if 'lzo' says its 'finished' and
// all uncompressed data has been consumed
return (finished && uncompressedDirectBuf.remaining() == 0);
}
public synchronized int decompress(byte[] b, int off, int len)
throws IOException {
if (b == null) {
throw new NullPointerException();
}
if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException();
}
int numBytes = 0;
if (isCurrentBlockUncompressed()) {
// The current block has been stored uncompressed, so just
// copy directly from the input buffer.
numBytes = Math.min(userBufLen, len);
System.arraycopy(userBuf, userBufOff, b, off, numBytes);
userBufOff += numBytes;
userBufLen -= numBytes;
} else {
// Check if there is uncompressed data
numBytes = uncompressedDirectBuf.remaining();
if (numBytes > 0) {
numBytes = Math.min(numBytes, len);
((ByteBuffer)uncompressedDirectBuf).get(b, off, numBytes);
return numBytes;
}
// Check if there is data to decompress
if (compressedDirectBufLen > 0) {
// Re-initialize the lzo's output direct-buffer
uncompressedDirectBuf.rewind();
uncompressedDirectBuf.limit(directBufferSize);
// Decompress data
numBytes = decompressBytesDirect(strategy.getDecompressor());
uncompressedDirectBuf.limit(numBytes);
// Return atmost 'len' bytes
numBytes = Math.min(numBytes, len);
((ByteBuffer)uncompressedDirectBuf).get(b, off, numBytes);
}
}
// Set 'finished' if lzo has consumed all user-data
if (userBufLen <= 0) {
finished = true;
}
return numBytes;
}
public synchronized void reset() {
finished = false;
compressedDirectBufLen = 0;
uncompressedDirectBuf.limit(directBufferSize);
uncompressedDirectBuf.position(directBufferSize);
userBufOff = userBufLen = 0;
}
public synchronized void end() {
// nop
}
@Override
protected void finalize() {
end();
}
/**
* Note whether the current block being decompressed is actually
* stored as uncompressed data. If it is, there is no need to
* use the lzo decompressor, and no need to update compressed
* checksums.
*
* @param uncompressed
* Whether the current block of data is uncompressed already.
*/
public synchronized void setCurrentBlockUncompressed(boolean uncompressed) {
isCurrentBlockUncompressed = uncompressed;
}
/**
* Query the compression status of the current block as it exists
* in the file.
*
* @return true if the current block of data was stored as uncompressed.
*/
protected synchronized boolean isCurrentBlockUncompressed() {
return isCurrentBlockUncompressed;
}
private native static void initIDs();
private native static int getLzoLibraryVersion();
private native void init(int decompressor);
private native int decompressBytesDirect(int decompressor);
}