Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.iq80.leveldb.util;
  

A Snappy abstraction which attempts uses the iq80 implementation and falls back to the xerial Snappy implementation it cannot be loaded. You can change the load order by setting the 'leveldb.snappy' system property. Example: -Dleveldb.snappy=xerial,iq80 The system property can also be configured with the name of a class which implements the Snappy.SPI interface.

Author(s):
Hiram Chirino
 
 public class Snappy {
     
     public static interface SPI {
         public int uncompress(ByteBuffer compressedByteBuffer uncompressedthrows IOException;
         public int uncompress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException;
         public int compress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException;
         public byte[] compress(String textthrows IOException;
         public int maxCompressedLength(int length);
     }
 
     public static class XerialSnappy implements SPI {
         static {
             // Make sure that the JNI libs are fully loaded.
             try {
                 org.xerial.snappy.Snappy.compress("test");
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
         }
         
         public int uncompress(ByteBuffer compressedByteBuffer uncompressedthrows IOException {
             return org.xerial.snappy.Snappy.uncompress(compresseduncompressed);
         }
 
         public int uncompress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException {
             return org.xerial.snappy.Snappy.uncompress(inputinputOffsetlengthoutputoutputOffset);
         }
 
         public int compress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException {
             return org.xerial.snappy.Snappy.compress(inputinputOffsetlengthoutputoutputOffset);
         }
 
         public byte[] compress(String textthrows IOException {
             return org.xerial.snappy.Snappy.compress(text);
         }
 
         public int maxCompressedLength(int length) {
             return org.xerial.snappy.Snappy.maxCompressedLength(length);
         }
     }
     
     public static class IQ80Snappy implements SPI {
         static {
             // Make sure that the library can fully load.
             try {
                 new IQ80Snappy().compress("test");
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
         }
 
         public int uncompress(ByteBuffer compressedByteBuffer uncompressedthrows IOException {
             byte[] input;
             int inputOffset;
             int length;
             byte[] output;
             int outputOffset;
             ifcompressed.hasArray() ) {
                 input = compressed.array();
                 inputOffset = compressed.arrayOffset() + compressed.position();
                 length = compressed.remaining();
             } else {
                 input = new byte[compressed.remaining()];
                 inputOffset = 0;
                 length = input.length;
                 compressed.mark();
                 compressed.get(input);
                 compressed.reset();
             }
             ifuncompressed.hasArray() ) {
                 output = uncompressed.array();
                 outputOffset = uncompressed.arrayOffset() + uncompressed.position();
             } else {
                 int t = org.iq80.snappy.Snappy.getUncompressedLength(inputinputOffset);
                 output = new byte[t];
                 outputOffset = 0;
             }
 
            int count = org.iq80.snappy.Snappy.uncompress(inputinputOffsetlengthoutputoutputOffset);
            ifuncompressed.hasArray() ) {
                uncompressed.limit(uncompressed.position()+count);
            } else {
                int p = uncompressed.position();
                uncompressed.limit(uncompressed.capacity());
                uncompressed.put(output, 0, count);
                uncompressed.flip().position(p);
            }
            return count;
        }
        public int uncompress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException {
            return  org.iq80.snappy.Snappy.uncompress(inputinputOffsetlengthoutputoutputOffset);
        }
        public int compress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException {
            return org.iq80.snappy.Snappy.compress(inputinputOffsetlengthoutputoutputOffset);
        }
        public byte[] compress(String textthrows IOException {
            byte[] uncomressed = text.getBytes("UTF-8");
            byte[] compressedOut = new byte[maxCompressedLength(uncomressed.length)];
            int compressedSize = compress(uncomressed, 0, uncomressed.lengthcompressedOut, 0);
            byte[] trimmedBuffer = new byte[compressedSize];
            System.arraycopy(compressedOut, 0, trimmedBuffer, 0, compressedSize);
            return trimmedBuffer;
        }
        public int maxCompressedLength(int length) {
            return org.iq80.snappy.Snappy.maxCompressedLength(length);
        }
    }
    static final private SPI SNAPPY;
    static {
        SPI attempt = null;
        String[] factories = System.getProperty("leveldb.snappy""iq80,xerial").split(",");
        for (int i = 0; i < factories.length && attempt==nulli++) {
            String name = factories[i];
            try {
                name = name.trim();
                if("xerial".equals(name.toLowerCase())) {
                    name = "org.iq80.leveldb.util.Snappy$XerialSnappy";
                } else if("iq80".equals(name.toLowerCase())) {
                    name = "org.iq80.leveldb.util.Snappy$IQ80Snappy";
                }
                attempt = (SPI) Thread.currentThread().getContextClassLoader().loadClass(name).newInstance();
            } catch (Throwable e) {
            }
        }
         = attempt;
    }
    public static boolean available() {
        return  !=null;
    }
    public static void uncompress(ByteBuffer compressedByteBuffer uncompressedthrows IOException {
        .uncompress(compresseduncompressed);
    }
    public static void uncompress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException {
        .uncompress(inputinputOffsetlengthoutputoutputOffset);
    }
    public static int compress(byte[] inputint inputOffsetint lengthbyte[] outputint outputOffsetthrows IOException {
        return .compress(inputinputOffsetlengthoutputoutputOffset);
    }
    public static byte[] compress(String textthrows IOException {
        return .compress(text);
    }
    public static int maxCompressedLength(int length) {
        return .maxCompressedLength(length);
    }
New to GrepCode? Check out our FAQ X