Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.background.bagheera; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.UnsupportedEncodingException; |
michael@0 | 8 | import java.util.zip.Deflater; |
michael@0 | 9 | |
michael@0 | 10 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 11 | |
michael@0 | 12 | public class DeflateHelper { |
michael@0 | 13 | /** |
michael@0 | 14 | * Conservative upper bound for zlib size, equivalent to the first few lines |
michael@0 | 15 | * in zlib's deflateBound function. |
michael@0 | 16 | * |
michael@0 | 17 | * Includes zlib header. |
michael@0 | 18 | * |
michael@0 | 19 | * @param sourceLen |
michael@0 | 20 | * the number of bytes to compress. |
michael@0 | 21 | * @return the number of bytes to allocate for the compressed output. |
michael@0 | 22 | */ |
michael@0 | 23 | public static int deflateBound(final int sourceLen) { |
michael@0 | 24 | return sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5 + 6; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * Deflate the input into the output array, returning the number of bytes |
michael@0 | 29 | * written to output. |
michael@0 | 30 | */ |
michael@0 | 31 | public static int deflate(byte[] input, byte[] output) { |
michael@0 | 32 | final Deflater deflater = new Deflater(); |
michael@0 | 33 | deflater.setInput(input); |
michael@0 | 34 | deflater.finish(); |
michael@0 | 35 | |
michael@0 | 36 | final int length = deflater.deflate(output); |
michael@0 | 37 | deflater.end(); |
michael@0 | 38 | return length; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Deflate the input, returning an HttpEntity that offers an accurate window |
michael@0 | 43 | * on the output. |
michael@0 | 44 | * |
michael@0 | 45 | * Note that this method does not trim the output array. (Test code can use |
michael@0 | 46 | * TestDeflation#deflateTrimmed(byte[]).) |
michael@0 | 47 | * |
michael@0 | 48 | * Trimming would be more efficient for long-term space use, but we expect this |
michael@0 | 49 | * entity to be transient. |
michael@0 | 50 | * |
michael@0 | 51 | * Note also that deflate can require <b>more</b> space than the input. |
michael@0 | 52 | * {@link #deflateBound(int)} tells us the most it will use. |
michael@0 | 53 | * |
michael@0 | 54 | * @param bytes the input to deflate. |
michael@0 | 55 | * @return the deflated input as an entity. |
michael@0 | 56 | */ |
michael@0 | 57 | public static HttpEntity deflateBytes(final byte[] bytes) { |
michael@0 | 58 | // We would like to use DeflaterInputStream here, but it's minSDK=9, and we |
michael@0 | 59 | // still target 8. It would also force us to use chunked Transfer-Encoding, |
michael@0 | 60 | // so perhaps it's for the best! |
michael@0 | 61 | |
michael@0 | 62 | final byte[] out = new byte[deflateBound(bytes.length)]; |
michael@0 | 63 | final int outLength = deflate(bytes, out); |
michael@0 | 64 | return new BoundedByteArrayEntity(out, 0, outLength); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | public static HttpEntity deflateBody(final String payload) { |
michael@0 | 68 | final byte[] bytes; |
michael@0 | 69 | try { |
michael@0 | 70 | bytes = payload.getBytes("UTF-8"); |
michael@0 | 71 | } catch (UnsupportedEncodingException ex) { |
michael@0 | 72 | // This will never happen. Thanks, Java! |
michael@0 | 73 | throw new RuntimeException(ex); |
michael@0 | 74 | } |
michael@0 | 75 | return deflateBytes(bytes); |
michael@0 | 76 | } |
michael@0 | 77 | } |