Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.storemate.shared.compress;
  
  import java.io.*;
  
 
 public class Compressors
 {
     // TODO: perhaps make pluggable?
     protected final static ChunkDecoder lzfDecoder;
     static {
          = ChunkDecoderFactory.optimalInstance();
     }
     
     /*
     /**********************************************************************
     /* Verification
     /**********************************************************************
      */
 
     public static boolean isCompressed(byte[] dataint offsetint len) {
         return findCompression(dataoffsetlen) != null;
     }
 
     public static boolean isCompressed(ByteContainer data) {
         return findCompression(data) != null;
     }
     
     public static Compression findCompression(byte[] dataint offsetint len)
     {
         if (len < 3) {
             return null;
         }
         byte b = data[offset];
         if (b == .) { // LZF: // starts with 'ZV' == 0x5A, 0x56
             if (data[offset+1] == .) {
                 byte third = data[offset+2];
                 if (third == . || third == .) {
                     return .;
                 }
             }
         } else if (b == 0x1F) { // GZIP: // starts with 0x1F, 0x8B (0x8B1F, little-endian)
             if ((data[offset+1] & 0xFF) == 0x8B) {
                 return .;
             }
         }
         return null;
     }
 
     public static Compression findCompression(ByteContainer data)
     {
         if (data.byteLength() < 3) {
             return null;
         }
         byte b = data.get(0);
         if (b == .) { // LZF: // starts with 'ZV' == 0x5A, 0x56
             if (data.get(1) == .) {
                 byte third = data.get(2);
                 if (third == . || third == .) {
                     return .;
                 }
             }
         } else if (b == 0x1F) { // GZIP: // starts with 0x1F, 0x8B (0x8B1F, little-endian)
             if ((data.get(1) & 0xFF) == 0x8B) {
                 return .;
             }
         }
         return null;
     }
     
     /*
     /**********************************************************************
     /* Compress
     /**********************************************************************
      */
 
     public static byte[] gzipCompress(byte[] datathrows IOException {
         return gzipCompress(data, 0, data.length);
     }
 
     public static byte[] gzipCompress(byte[] dataint offsetint lenthrows IOException
     {
         // assume 50% compression rate
         ByteArrayOutputStream bytes = new ByteArrayOutputStream(len>>1);
         OptimizedGZIPOutputStream out = new OptimizedGZIPOutputStream(bytes);
         out.write(dataoffsetlen);
         out.close();
         return bytes.toByteArray();
     }
 
    public static byte[] gzipCompress(ByteContainer datathrows IOException
    {
        // assume 50% compression rate
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(data.byteLength()>>1);
        OptimizedGZIPOutputStream out = new OptimizedGZIPOutputStream(bytes);
        data.writeBytes(out);
        out.close();
        return bytes.toByteArray();
    }
    
    public static byte[] lzfCompress(byte[] datathrows IOException {
        return lzfCompress(data, 0, data.length);
    }
    public static byte[] lzfCompress(byte[] dataint offsetint lenthrows IOException {
        return LZFEncoder.encode(dataoffsetlen);
    }
    public static byte[] lzfCompress(ByteContainer datathrows IOException {
        return data.withBytes(new WithBytesCallback<byte[]>() {
            @Override
            public byte[] withBytes(byte[] bufferint offsetint length) {
                try {
                    return LZFEncoder.encode(bufferoffsetlength);
                } catch (Exception e) {
                    throw new IllegalArgumentException(e);
                }
            }
        });
    }
    
    public static OutputStream compressingStream(OutputStream outCompression compthrows IOException
    {
        if (comp != null) {
            switch (comp) {
            case :
                break;
            case :
                return new LZFOutputStream(out);
            case :
                return new OptimizedGZIPOutputStream(out);
            default// sanity check
                throw new IllegalArgumentException("Unrecognized compression type: "+comp);
            }
        }
        return out;
    }
    
    /*
    /**********************************************************************
    /* Uncompress
    /**********************************************************************
     */
    
    public static InputStream uncompressingStream(InputStream inCompression comp)
        throws IOException
    {
        if (comp != null) {
            switch (comp) {
            case :
                break;
            case :
                return new LZFInputStream(in);
            case :
                return new OptimizedGZIPInputStream(in);
            default// sanity check
                throw new IllegalArgumentException("Unrecognized compression type: "+comp);
            }
        }
        return in;
    }
    public static ByteContainer uncompress(ByteContainer dataCompression compint expSize)
            throws IOException
    {
        if (comp != null) {
            switch (comp) {
            case :
                break;
            case :
                return lzfUncompress(data);
            case :
                return gzipUncompress(dataexpSize);
            default// sanity check
                throw new IllegalArgumentException("Unrecognized compression type: "+comp);
            }
        }
        return data;
    }
    public static ByteContainer gzipUncompress(ByteContainer compDataint expSize)
        throws IOException
    {
    	return ByteContainer.simple(gzipUncompress(compData.asBytes(), expSize));
    }
    
    public static byte[] gzipUncompress(byte[] compDataint expSize)
            throws IOException
    {
        if (expSize <= 0) {
            return gzipUncompress(compData);
        }
        byte[] buffer = new byte[expSize];
        OptimizedGZIPInputStream in = new OptimizedGZIPInputStream(new ByteArrayInputStream(compData));
        int offset = 0;
        int left = buffer.length;
        int count;
        while (left > 0 && (count = in.read(bufferoffsetleft)) > 0) {
            offset += count;
            left -= count;
        }
        // should have gotten exactly expected amount
        try {
            if (offset < expSize) {
                throw new IOException("Corrupt GZIP/Deflate data: expected "+expSize+" bytes, got "+offset);
            }
            // and no more
            if (in.read() != -1) {
                throw new IOException("Corrupt GZIP/Deflate data: expected "+expSize+" bytes, got at least one more");
            }
        } finally {
            try { in.close(); } catch (IOException e) { }
        }
        return buffer;
    }
    public static byte[] gzipUncompress(byte[] compData)
        throws IOException
    {
        OptimizedGZIPInputStream in = new OptimizedGZIPInputStream(new ByteArrayInputStream(compData));
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(16 + (compData.length << 1));
        byte[] buffer = new byte[500];
        int count;
        while ((count = in.read(buffer)) > 0) {
            bytes.write(buffer, 0, count);
        }
        in.close();
        bytes.close();
        return bytes.toByteArray();
    }
    
    public static byte[] lzfUncompress(byte[] datathrows IOException
    {
        return .decode(data);
    }
    public static ByteContainer lzfUncompress(ByteContainer datathrows IOException
    {
        return data.withBytes(new WithBytesCallback<ByteContainer>() {
			public ByteContainer withBytes(byte[] bufferint offsetint length)
			{
				try {
					return ByteContainer.simple(.decode(bufferoffsetlength));
catch (IOException e) {
					throw new IllegalArgumentException("Bad LZF data to uncompress ("+length+" bytes): "+e.getMessage(), e);
				}
			}
    	});
    }
New to GrepCode? Check out our FAQ X