Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry - Common Java Functionality Copyright (c) 2012-2015, Sandeep Gupta http://sangupta.com/projects/jerry Licensed 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.sangupta.jerry.util;
 
 
Utility function to compress given data into a byte-array and vice-versa. Useful for situation where data needs to be stored in database etc. The default compression happens using DEFLATE algorithm.

Author(s):
sangupta
 
 public class CompressionUtils {

The logger
 
 	private static final Logger LOGGER = LoggerFactory.getLogger(CompressionUtils.class);

Compress the given string data into byte-array. The method is null safe. The incoming string is translated to bytes using the platform-encoding.

Parameters:
string the string to be compressed
Returns:
the bytes of the compressed representation, or null if incoming string is null.
 
 	public static byte[] compress(String string) {
 		if(string == null) {
 			return null;
 		}
 		
 		return compress(string.getBytes());
 	}

Compress the given string data into byte-array. The method is null safe. The incoming string is translated to bytes using the given charset-name.

Parameters:
string the string to be compressed
Returns:
the bytes of the compressed representation, or null if incoming string is null.
Throws:
java.io.UnsupportedEncodingException if the incoming string cannot be converted into bytes using the given charset name.
 
 	public static byte[] compress(String stringString charsetNamethrows UnsupportedEncodingException {
 		if(string == null) {
 			return null;
 		}
 		
 		return compress(string.getBytes(charsetName));
 	}

Compress the given string data into byte-array. The method is null safe. The incoming string is translated to bytes using the given java.nio.charset.Charset.

Parameters:
string the string to be compressed
Returns:
the bytes of the compressed representation, or null if incoming string is null.
Throws:
java.io.UnsupportedEncodingException if the incoming string cannot be converted into bytes using the given java.nio.charset.Charset.
	public static byte[] compress(String stringCharset charsetthrows UnsupportedEncodingException {
		if(string == null) {
			return null;
		}
		return compress(string.getBytes(charset));
	}

Compress the given bytes using DEFLATE algorithm. The method is null safe.

Parameters:
inputBytes the bytes to be compressed
Returns:
the bytes of the compressed representation, or null if incoming bytes is null.
	public static byte[] compress(byte[] inputBytes) {
		if(AssertUtils.isEmpty(inputBytes)) {
			return null;
		}
		byte[] output = new byte[inputBytes.length];
		Deflater deflater = new Deflater(.);
		deflater.setInput(inputBytes);
		deflater.finish();
		int compressedDataLength = deflater.deflate(output);
		deflater.end();
		if(compressedDataLength != 0) {
			byte[] finalOutput = new byte[compressedDataLength];
			System.arraycopy(output, 0, finalOutput, 0, compressedDataLength);
			return finalOutput;
		}
		return null;
	}

Uncompress the given input byte-array assuming that the max uncompressed size is not known. The method will try and allocate a buffer of 3 times the size of input array. If the uncompressed size is known, use the method uncompress(byte[],int).

Parameters:
inputBytes the bytes to uncompress
Returns:
the uncompressed byte stream
	public static byte[] uncompress(byte[] inputBytes) {
		if(inputBytes == null) {
			return null;
		}
		return uncompress(inputBytesinputBytes.length * 3);
	}

Uncompress the byte-array when the uncompressed size is known.

Parameters:
inputBytes the compressed bytes
uncompressedSize the uncompressed size of the stream
Returns:
the uncompressed byte stream
	public static byte[] uncompress(byte[] inputBytesint uncompressedSize) {
		if(AssertUtils.isEmpty(inputBytes)) {
			return null;
		}
		Inflater inflater = new Inflater();
		inflater.setInput(inputBytes);
		byte[] output = new byte[uncompressedSize + 1];
		try {
			int total = inflater.inflate(output);
			inflater.end();
			return Arrays.copyOf(outputtotal);
catch (DataFormatException e) {
			.error("Unable to uncompress data"e);
		}
		return null;
	}

Uncompress the given byte-array and convert it into a string.

Parameters:
inputBytes the compressed bytes
Returns:
string representation of the bytes in the default platform encoding
	public static String uncompressToString(byte[] inputBytes) {
		return new String(uncompress(inputBytes));
	}

Uncompress the given byte-array and convert it into a string where uncompressed size is known.

Parameters:
inputBytes the compressed bytes
uncompressedSize the uncompressed size
Returns:
string representation of the bytes in the default platform encoding
	public static String uncompressToString(byte[] inputBytesint uncompressedSize) {
		return new String(uncompress(inputBytesuncompressedSize));
	}

GZip a given byte-array in-memory.

Parameters:
bytes the uncompressed bytes
Returns:
the GZipped byte stream
Throws:
java.io.IOException if something fails
	public static byte[] gzipByteArray(byte[] bytesthrows IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
		GZIPOutputStream gzip = new GZIPOutputStream(baos);
		gzip.write(bytes);
		gzip.close();
		return baos.toByteArray();
	}
New to GrepCode? Check out our FAQ X