1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/bagheera/DeflateHelper.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,77 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.bagheera; 1.9 + 1.10 +import java.io.UnsupportedEncodingException; 1.11 +import java.util.zip.Deflater; 1.12 + 1.13 +import ch.boye.httpclientandroidlib.HttpEntity; 1.14 + 1.15 +public class DeflateHelper { 1.16 + /** 1.17 + * Conservative upper bound for zlib size, equivalent to the first few lines 1.18 + * in zlib's deflateBound function. 1.19 + * 1.20 + * Includes zlib header. 1.21 + * 1.22 + * @param sourceLen 1.23 + * the number of bytes to compress. 1.24 + * @return the number of bytes to allocate for the compressed output. 1.25 + */ 1.26 + public static int deflateBound(final int sourceLen) { 1.27 + return sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5 + 6; 1.28 + } 1.29 + 1.30 + /** 1.31 + * Deflate the input into the output array, returning the number of bytes 1.32 + * written to output. 1.33 + */ 1.34 + public static int deflate(byte[] input, byte[] output) { 1.35 + final Deflater deflater = new Deflater(); 1.36 + deflater.setInput(input); 1.37 + deflater.finish(); 1.38 + 1.39 + final int length = deflater.deflate(output); 1.40 + deflater.end(); 1.41 + return length; 1.42 + } 1.43 + 1.44 + /** 1.45 + * Deflate the input, returning an HttpEntity that offers an accurate window 1.46 + * on the output. 1.47 + * 1.48 + * Note that this method does not trim the output array. (Test code can use 1.49 + * TestDeflation#deflateTrimmed(byte[]).) 1.50 + * 1.51 + * Trimming would be more efficient for long-term space use, but we expect this 1.52 + * entity to be transient. 1.53 + * 1.54 + * Note also that deflate can require <b>more</b> space than the input. 1.55 + * {@link #deflateBound(int)} tells us the most it will use. 1.56 + * 1.57 + * @param bytes the input to deflate. 1.58 + * @return the deflated input as an entity. 1.59 + */ 1.60 + public static HttpEntity deflateBytes(final byte[] bytes) { 1.61 + // We would like to use DeflaterInputStream here, but it's minSDK=9, and we 1.62 + // still target 8. It would also force us to use chunked Transfer-Encoding, 1.63 + // so perhaps it's for the best! 1.64 + 1.65 + final byte[] out = new byte[deflateBound(bytes.length)]; 1.66 + final int outLength = deflate(bytes, out); 1.67 + return new BoundedByteArrayEntity(out, 0, outLength); 1.68 + } 1.69 + 1.70 + public static HttpEntity deflateBody(final String payload) { 1.71 + final byte[] bytes; 1.72 + try { 1.73 + bytes = payload.getBytes("UTF-8"); 1.74 + } catch (UnsupportedEncodingException ex) { 1.75 + // This will never happen. Thanks, Java! 1.76 + throw new RuntimeException(ex); 1.77 + } 1.78 + return deflateBytes(bytes); 1.79 + } 1.80 +} 1.81 \ No newline at end of file