Exemple #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;
 }
Exemple #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;
 }
  /**
   * 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();
    }
  }