Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.indeed.util.compress.zlib;
 
 
A com.indeed.util.compress.Compressor based on the popular zlib compression algorithm. http://www.zlib.net/
 
 public class ZlibCompressor implements Compressor {
 
   private static final Logger LOG = Logger.getLogger(ZlibCompressor.class);
 
   private static final int DEFAULT_DIRECT_BUFFER_SIZE = 64*1024;
 
   // HACK - Use this as a global lock in the JNI layer
   private static Class clazz = ZlibCompressor.class;
 
   private long stream;
   private CompressionLevel level;
   private final CompressionHeader windowBits;
   private int directBufferSize;
   private byte[] userBuf = null;
   private int userBufOff = 0, userBufLen = 0;
   private Buffer uncompressedDirectBuf = null;
   private int uncompressedDirectBufOff = 0, uncompressedDirectBufLen = 0;
   private boolean keepUncompressedBuf = false;
   private Buffer compressedDirectBuf = null;
   private boolean finishfinished;

  
The compression level for zlib library.
 
   public static enum CompressionLevel {
    
Compression level for no compression.
 
     NO_COMPRESSION (0),
    
    
Compression level for fastest compression.
 
     BEST_SPEED (1),
    
    
Compression level for best compression.
 
     BEST_COMPRESSION (9),
    
    
Default compression level.
 
     DEFAULT_COMPRESSION (-1);
     
     
     private final int compressionLevel;
     
     CompressionLevel(int level) {
        = level;
     }
     
     int compressionLevel() {
       return ;
     }
   };
  
  
The compression level for zlib library.
 
   public static enum CompressionStrategy {
    
Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. Forces more Huffman coding and less string matching.
    FILTERED (1),
    
    
Compression strategy for Huffman coding only.
    HUFFMAN_ONLY (2),
    
    
Compression strategy to limit match distances to one (run-length encoding).
    RLE (3),

    
Compression strategy to prevent the use of dynamic Huffman codes, allowing for a simpler decoder for special applications.
    FIXED (4),

    
Default compression strategy.
    DEFAULT_STRATEGY (0);
    
    
    private final int compressionStrategy;
    
    CompressionStrategy(int strategy) {
       = strategy;
    }
    
    int compressionStrategy() {
      return ;
    }
  };

  
The type of header for compressed data.
  public static enum CompressionHeader {
    
No headers/trailers/checksums.
    NO_HEADER (-15),
    
    
Default headers/trailers/checksums.
    DEFAULT_HEADER (15),
    
    
Simple gzip headers/trailers.
    GZIP_FORMAT (31);
    private final int windowBits;
    
    CompressionHeader(int windowBits) {
      this. = windowBits;
    }
    
    public int windowBits() {
      return ;
    }
  }
  
  private static boolean nativeZlibLoaded = false;
  
  static {
    if (NativeCodeLoader.isNativeCodeLoaded()) {
      try {
        // Initialize the native library
        initIDs();
         = true;
      } catch (Throwable t) {
        // Ignore failure to load/initialize native-zlib
      }
    }
  }
  
  static boolean isNativeZlibLoaded() {
    return ;
  }
  protected final void construct(CompressionLevel levelCompressionStrategy strategy,
      CompressionHeader headerint directBufferSize) {
  }

  
Creates a new compressor with the default compression level. Compressed data will be generated in ZLIB format.
  public ZlibCompressor() {
            .,
            .,
            );
  }

  
Creates a new compressor using the specified compression level. Compressed data will be generated in ZLIB format.

Parameters:
level Compression level #CompressionLevel
strategy Compression strategy #CompressionStrategy
header Compression header #CompressionHeader
directBufferSize Size of the direct buffer to be used.
  public ZlibCompressor(CompressionLevel levelCompressionStrategy strategy
                        CompressionHeader headerint directBufferSize) {
    this. = level;
    this. = strategy;
    this. = header;
     = init(this..compressionLevel(),
            this..compressionStrategy(),
            this..windowBits());
    this. = directBufferSize;
     = ByteBuffer.allocateDirect(directBufferSize);
     = ByteBuffer.allocateDirect(directBufferSize);
    .position(directBufferSize);
  }
  public synchronized void setInput(byte[] bint offint len) {
    if (b== null) {
      throw new NullPointerException();
    }
    if (off < 0 || len < 0 || off > b.length - len) {
      throw new ArrayIndexOutOfBoundsException();
    }
    
    this. = b;
    this. = off;
    this. = len;
    
    // Reinitialize zlib's output direct buffer 
  }
  
  //copy enough data from userBuf to uncompressedDirectBuf
  synchronized void setInputFromSavedData() {
    int len = Math.min(.remaining());
     -= len;
     += len;
  }
  public synchronized void setDictionary(byte[] bint offint len) {
    if ( == 0 || b == null) {
      throw new NullPointerException();
    }
    if (off < 0 || len < 0 || off > b.length - len) {
      throw new ArrayIndexOutOfBoundsException();
    }
    setDictionary(bofflen);
  }
  public synchronized boolean needsInput() {
    // Consume remaining compressed data?
    if (.remaining() > 0) {
      return false;
    }
    // Check if zlib has consumed all input
    // compress should be invoked if keepUncompressedBuf true
      return false;
    
    if (.remaining() > 0) {
      // Check if we have consumed all user-input
      if ( <= 0) {
        return true;
      } else {
        // copy enough data from userBuf to uncompressedDirectBuf
        setInputFromSavedData();
        if (.remaining() > 0) // uncompressedDirectBuf is not full
          return true;
        else 
          return false;
      }
    }
    
    return false;
  }
  
  public synchronized void finish() {
     = true;
  }
  
  public synchronized boolean finished() {
    // Check if 'zlib' says its 'finished' and
    // all compressed data has been consumed
    return ( && .remaining() == 0);
  }
  public synchronized int compress(byte[] bint offint len
    throws IOException {
    if (b == null) {
      throw new NullPointerException();
    }
    if (off < 0 || len < 0 || off > b.length - len) {
      throw new ArrayIndexOutOfBoundsException();
    }
    
    int n = 0;
    
    // Check if there is compressed data
    if (n > 0) {
      n = Math.min(nlen);
      ((ByteBuffer)).get(boffn);
      return n;
    }
    // Re-initialize the zlib's output direct buffer
    // Compress data
    n = deflateBytesDirect();
    
    // Check if zlib consumed all input buffer
    // set keepUncompressedBuf properly
    if ( <= 0) { // zlib consumed all input buffer
       = false;
       = 0;
       = 0;
    } else { // zlib did not consume all input buffer
       = true;
    }
    
    // Get atmost 'len' bytes
    n = Math.min(nlen);
    ((ByteBuffer)).get(boffn);
    return n;
  }

  
Returns the total number of compressed bytes output so far.

Returns:
the total (non-negative) number of compressed bytes output so far
  public synchronized long getBytesWritten() {
    checkStream();
    return getBytesWritten();
  }

  
Returns the total number of uncompressed bytes input so far.

Returns:
the total (non-negative) number of uncompressed bytes input so far
  public synchronized long getBytesRead() {
    checkStream();
    return getBytesRead();
  }
  public synchronized void reset() {
    checkStream();
    reset();
     = false;
     = false;
     = false;
     =  = 0;
  }
  
  public synchronized void end() {
    if ( != 0) {
      end();
       = 0;
    }
  }
  
  private void checkStream() {
    if ( == 0)
      throw new NullPointerException();
  }
  
  private native static void initIDs();
  private native static long init(int levelint strategyint windowBits);
  private native static void setDictionary(long strmbyte[] bint off,
                                           int len);
  private native int deflateBytesDirect();
  private native static long getBytesRead(long strm);
  private native static long getBytesWritten(long strm);
  private native static void reset(long strm);
  private native static void end(long strm);
New to GrepCode? Check out our FAQ X