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.snappy;
 
 
A com.indeed.util.compress.Compressor based on the snappy compression algorithm. http://code.google.com/p/snappy/
 
 public class SnappyCompressor implements Compressor {
   private static final Logger LOG = Logger.getLogger(SnappyCompressor.class);
   private static final int DEFAULT_DIRECT_BUFFER_SIZE = 64 * 1024;
 
   // HACK - Use this as a global lock in the JNI layer
   @SuppressWarnings({"unchecked""unused"})
   private static Class clazz = SnappyCompressor.class;
 
   private int directBufferSize;
   private Buffer compressedDirectBuf = null;
   private int uncompressedDirectBufLen;
   private Buffer uncompressedDirectBuf = null;
   private byte[] userBuf = null;
   private int userBufOff = 0, userBufLen = 0;
   private boolean finishfinished;
 
   private long bytesRead = 0L;
   private long bytesWritten = 0L;
 
   private static boolean nativeSnappyLoaded = false;
   
   static {
     if (NativeCodeLoader.isNativeCodeLoaded()) {
       try {
         initIDs();
          = true;
       } catch (Throwable t) {
         .error("failed to load SnappyCompressor"t);
       }
     }
   }
   
   public static boolean isNativeCodeLoaded() {
     return ;
   }
  
  
Creates a new compressor.

Parameters:
directBufferSize size of the direct buffer to be used.
 
   public SnappyCompressor(int directBufferSize) {
     this. = directBufferSize;
 
      = ByteBuffer.allocateDirect(directBufferSize);
      = ByteBuffer.allocateDirect(directBufferSize);
     .position(directBufferSize);
   }

  
Creates a new compressor with the default buffer size.
 
   public SnappyCompressor() {
   }

  
Sets input data for compression. This should be called whenever #needsInput() returns true indicating that more input data is required.

Parameters:
b Input data
off Start offset
len Length
 
   @Override
   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();
    }
     = false;
    if (len > .remaining()) {
      // save data; now !needsInput
      this. = b;
      this. = off;
      this. = len;
    } else {
      ((ByteBuffer).put(bofflen);
    }
     += len;
  }

  
If a write would exceed the capacity of the direct buffers, it is set aside to be loaded by this function while the compressed data are consumed.
  synchronized void setInputFromSavedData() {
    if (0 >= ) {
      return;
    }
     = false;
        );
    // Note how much data is being fed to snappy
  }

  
Does nothing.
  public synchronized void setDictionary(byte[] bint offint len) {
    // do nothing
  }

  
Returns true if the input data buffer is empty and #setInput() should be called to provide more input.

Returns:
true if the input data buffer is empty and #setInput() should be called in order to provide more input.
  public synchronized boolean needsInput() {
    return !(.remaining() > 0
        || .remaining() == 0 ||  > 0);
  }

  
When called, indicates that compression should end with the current contents of the input buffer.
  public synchronized void finish() {
     = true;
  }

  
Returns true if the end of the compressed data output stream has been reached.

Returns:
true if the end of the compressed data output stream has been reached.
  public synchronized boolean finished() {
    // Check if all uncompressed data has been consumed
    return ( &&  && .remaining() == 0);
  }

  
Fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput() should be called in order to determine if more input data is required.

Parameters:
b Buffer for the compressed data
off Start offset of the data
len Size of the buffer
Returns:
The actual number of bytes of compressed data.
  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();
    }
    // Check if there is compressed data
    int n = .remaining();
    if (n > 0) {
      n = Math.min(nlen);
      ((ByteBuffer).get(boffn);
       += n;
      return n;
    }
    // Re-initialize the snappy's output direct-buffer
    if (0 == .position()) {
      // No compressed data, so we should have !needsInput or !finished
      if (0 == .position()) {
        // Called without data; write nothing
         = true;
        return 0;
      }
    }
    // Compress data
    n = compressBytesDirect();
    .clear(); // snappy consumes all buffer input
    // Set 'finished' if snapy has consumed all user-data
    if (0 == ) {
       = true;
    }
    // Get atmost 'len' bytes
    n = Math.min(nlen);
     += n;
    ((ByteBuffer).get(boffn);
    return n;
  }

  
Resets compressor so that a new set of input data can be processed.
  public synchronized void reset() {
     = false;
     = false;
     =  = 0;
     =  = 0L;
  }

  
Return number of bytes given to this compressor since last reset.
  public synchronized long getBytesRead() {
    return ;
  }

  
Return number of bytes consumed by callers of compress since last reset.
  public synchronized long getBytesWritten() {
    return ;
  }

  
Closes the compressor and discards any unprocessed input.
  public synchronized void end() {
  }
  private native static void initIDs();
  private native int compressBytesDirect();
New to GrepCode? Check out our FAQ X