@Test public void testCompressDecompress() { byte[] rawData = null; int rawDataSize = 0; rawDataSize = 1024 * 64; rawData = generate(rawDataSize); try { Bzip2Compressor compressor = new Bzip2Compressor(); Bzip2Decompressor decompressor = new Bzip2Decompressor(); assertFalse("testBzip2CompressDecompress finished error", compressor.finished()); compressor.setInput(rawData, 0, rawData.length); assertTrue("testBzip2CompressDecompress getBytesRead before error", compressor.getBytesRead() == 0); compressor.finish(); byte[] compressedResult = new byte[rawDataSize]; int cSize = compressor.compress(compressedResult, 0, rawDataSize); assertTrue("testBzip2CompressDecompress getBytesRead after error", compressor.getBytesRead() == rawDataSize); assertTrue( "testBzip2CompressDecompress compressed size no less than original size", cSize < rawDataSize); decompressor.setInput(compressedResult, 0, cSize); byte[] decompressedBytes = new byte[rawDataSize]; decompressor.decompress(decompressedBytes, 0, decompressedBytes.length); assertArrayEquals("testBzip2CompressDecompress arrays not equals ", rawData, decompressedBytes); compressor.reset(); decompressor.reset(); } catch (IOException ex) { fail("testBzip2CompressDecompress ex !!!" + ex); } }
/** * Check if native-bzip2 code is loaded & initialized correctly and * can be loaded for this job. * * @param conf configuration * @return <code>true</code> if native-bzip2 is loaded & initialized * and can be loaded for this job, else <code>false</code> */ public static boolean isNativeBzip2Loaded(Configuration conf) { String libname = conf.get("io.compression.codec.bzip2.library", "system-native"); if (!bzip2LibraryName.equals(libname)) { nativeBzip2Loaded = false; bzip2LibraryName = libname; if (libname.equals("java-builtin")) { LOG.info("Using pure-Java version of bzip2 library"); } else if (conf.getBoolean( CommonConfigurationKeys.IO_NATIVE_LIB_AVAILABLE_KEY, CommonConfigurationKeys.IO_NATIVE_LIB_AVAILABLE_DEFAULT) && NativeCodeLoader.isNativeCodeLoaded()) { try { // Initialize the native library. Bzip2Compressor.initSymbols(libname); Bzip2Decompressor.initSymbols(libname); nativeBzip2Loaded = true; LOG.info("Successfully loaded & initialized native-bzip2 library " + libname); } catch (Throwable t) { LOG.warn("Failed to load/initialize native-bzip2 library " + libname + ", will use pure-Java version"); } } } return nativeBzip2Loaded; }
/** * Check if native-bzip2 code is loaded & initialized correctly and * can be loaded for this job. * * @param conf configuration * @return <code>true</code> if native-bzip2 is loaded & initialized * and can be loaded for this job, else <code>false</code> */ public static synchronized boolean isNativeBzip2Loaded(Configuration conf) { String libname = conf.get("io.compression.codec.bzip2.library", "system-native"); if (!bzip2LibraryName.equals(libname)) { nativeBzip2Loaded = false; bzip2LibraryName = libname; if (libname.equals("java-builtin")) { LOG.info("Using pure-Java version of bzip2 library"); } else if (conf.getBoolean( CommonConfigurationKeys.IO_NATIVE_LIB_AVAILABLE_KEY, CommonConfigurationKeys.IO_NATIVE_LIB_AVAILABLE_DEFAULT) && NativeCodeLoader.isNativeCodeLoaded()) { try { // Initialize the native library. Bzip2Compressor.initSymbols(libname); Bzip2Decompressor.initSymbols(libname); nativeBzip2Loaded = true; LOG.info("Successfully loaded & initialized native-bzip2 library " + libname); } catch (Throwable t) { LOG.warn("Failed to load/initialize native-bzip2 library " + libname + ", will use pure-Java version"); } } } return nativeBzip2Loaded; }
/** * Return the appropriate type of the bzip2 decompressor. * * @param conf configuration * @return the appropriate type of the bzip2 decompressor. */ public static Class<? extends Decompressor> getBzip2DecompressorType(Configuration conf) { return isNativeBzip2Loaded(conf) ? Bzip2Decompressor.class : BZip2DummyDecompressor.class; }
/** * Return the appropriate implementation of the bzip2 decompressor. * * @param conf configuration * @return the appropriate implementation of the bzip2 decompressor. */ public static Decompressor getBzip2Decompressor(Configuration conf) { return isNativeBzip2Loaded(conf) ? new Bzip2Decompressor() : new BZip2DummyDecompressor(); }