Beispiel #1
0
 public Compressor getCompressor() {
   CompressionCodec codec = getCodec(conf);
   if (codec != null) {
     Compressor compressor = CodecPool.getCompressor(codec);
     if (LOG.isTraceEnabled()) LOG.trace("Retrieved compressor " + compressor + " from pool.");
     if (compressor != null) {
       if (compressor.finished()) {
         // Somebody returns the compressor to CodecPool but is still using it.
         LOG.warn("Compressor obtained from CodecPool is already finished()");
       }
       compressor.reset();
     }
     return compressor;
   }
   return null;
 }
Beispiel #2
0
 public Compressor getCompressor() {
   CompressionCodec codec = getCodec(conf);
   if (codec != null) {
     Compressor compressor = CodecPool.getCompressor(codec);
     if (compressor != null) {
       if (compressor.finished()) {
         // Somebody returns the compressor to CodecPool but is still using
         // it.
         LOG.warn("Compressor obtained from CodecPool is already finished()");
         // throw new AssertionError(
         // "Compressor obtained from CodecPool is already finished()");
       }
       compressor.reset();
     }
     return compressor;
   }
   return null;
 }
 private boolean checkSetDictionaryArrayIndexOutOfBoundsException(Compressor compressor) {
   try {
     compressor.setDictionary(new byte[] {(byte) 0}, 0, -1);
   } catch (ArrayIndexOutOfBoundsException e) {
     return true;
   } catch (Exception e) {
   }
   return false;
 }
 private boolean checkSetDictionaryNullPointerException(Compressor compressor) {
   try {
     compressor.setDictionary(null, 0, 1);
   } catch (NullPointerException ex) {
     return true;
   } catch (Exception ex) {
   }
   return false;
 }
 @Test
 public void testZlibCompressorDecompressorWithConfiguration() {
   Configuration conf = new Configuration();
   conf.setBoolean(CommonConfigurationKeys.IO_NATIVE_LIB_AVAILABLE_KEY, true);
   if (ZlibFactory.isNativeZlibLoaded(conf)) {
     byte[] rawData;
     int tryNumber = 5;
     int BYTE_SIZE = 10 * 1024;
     Compressor zlibCompressor = ZlibFactory.getZlibCompressor(conf);
     Decompressor zlibDecompressor = ZlibFactory.getZlibDecompressor(conf);
     rawData = generate(BYTE_SIZE);
     try {
       for (int i = 0; i < tryNumber; i++)
         compressDecompressZlib(
             rawData, (ZlibCompressor) zlibCompressor, (ZlibDecompressor) zlibDecompressor);
       zlibCompressor.reinit(conf);
     } catch (Exception ex) {
       fail("testZlibCompressorDecompressorWithConfiguration ex error " + ex);
     }
   } else {
     assertTrue(
         "ZlibFactory is using native libs against request", ZlibFactory.isNativeZlibLoaded(conf));
   }
 }
  /**
   * Find the size of compressed data assuming that buffer will be compressed using given algorithm.
   *
   * @param algo compression algorithm
   * @param compressor compressor already requested from codec
   * @param inputBuffer Array to be compressed.
   * @param offset Offset to beginning of the data.
   * @param length Length to be compressed.
   * @return Size of compressed data in bytes.
   * @throws IOException
   */
  public static int getCompressedSize(
      Algorithm algo, Compressor compressor, byte[] inputBuffer, int offset, int length)
      throws IOException {
    DataOutputStream compressedStream = new DataOutputStream(new IOUtils.NullOutputStream());
    if (compressor != null) {
      compressor.reset();
    }
    OutputStream compressingStream = null;

    try {
      compressingStream = algo.createCompressionStream(compressedStream, compressor, 0);

      compressingStream.write(inputBuffer, offset, length);
      compressingStream.flush();

      return compressedStream.size();
    } finally {
      if (compressingStream != null) compressingStream.close();
    }
  }