mobile/android/base/favicons/decoders/LoadFaviconResult.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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.favicons.decoders;
michael@0 6
michael@0 7 import android.graphics.Bitmap;
michael@0 8 import android.util.Log;
michael@0 9
michael@0 10 import java.io.ByteArrayOutputStream;
michael@0 11 import java.util.Iterator;
michael@0 12
michael@0 13 /**
michael@0 14 * Class representing the result of loading a favicon.
michael@0 15 * This operation will produce either a collection of favicons, a single favicon, or no favicon.
michael@0 16 * It is necessary to model single favicons differently to a collection of one favicon (An entity
michael@0 17 * that may not exist with this scheme) since the in-database representation of these things differ.
michael@0 18 * (In particular, collections of favicons are stored in encoded ICO format, whereas single icons are
michael@0 19 * stored as decoded bitmap blobs.)
michael@0 20 */
michael@0 21 public class LoadFaviconResult {
michael@0 22 private static final String LOGTAG = "LoadFaviconResult";
michael@0 23
michael@0 24 byte[] faviconBytes;
michael@0 25 int offset;
michael@0 26 int length;
michael@0 27
michael@0 28 boolean isICO;
michael@0 29 Iterator<Bitmap> bitmapsDecoded;
michael@0 30
michael@0 31 public Iterator<Bitmap> getBitmaps() {
michael@0 32 return bitmapsDecoded;
michael@0 33 }
michael@0 34
michael@0 35 /**
michael@0 36 * Return a representation of this result suitable for storing in the database.
michael@0 37 *
michael@0 38 * @return A byte array containing the bytes from which this result was decoded,
michael@0 39 * or null if re-encoding failed.
michael@0 40 */
michael@0 41 public byte[] getBytesForDatabaseStorage() {
michael@0 42 // Begin by normalising the buffer.
michael@0 43 if (offset != 0 || length != faviconBytes.length) {
michael@0 44 final byte[] normalised = new byte[length];
michael@0 45 System.arraycopy(faviconBytes, offset, normalised, 0, length);
michael@0 46 offset = 0;
michael@0 47 faviconBytes = normalised;
michael@0 48 }
michael@0 49
michael@0 50 // For results containing multiple images, we store the result verbatim. (But cutting the
michael@0 51 // buffer to size first).
michael@0 52 // We may instead want to consider re-encoding the entire ICO as a collection of efficiently
michael@0 53 // encoded PNGs. This may not be worth the CPU time (Indeed, the encoding of single-image
michael@0 54 // favicons may also not be worth the time/space tradeoff.).
michael@0 55 if (isICO) {
michael@0 56 return faviconBytes;
michael@0 57 }
michael@0 58
michael@0 59 // For results containing a single image, we re-encode the
michael@0 60 // result as a PNG in an effort to save space.
michael@0 61 final Bitmap favicon = ((FaviconDecoder.SingleBitmapIterator) bitmapsDecoded).peek();
michael@0 62 final ByteArrayOutputStream stream = new ByteArrayOutputStream();
michael@0 63
michael@0 64 try {
michael@0 65 if (favicon.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
michael@0 66 return stream.toByteArray();
michael@0 67 }
michael@0 68 } catch (OutOfMemoryError e) {
michael@0 69 Log.w(LOGTAG, "Out of memory re-compressing favicon.");
michael@0 70 }
michael@0 71
michael@0 72 Log.w(LOGTAG, "Favicon re-compression failed.");
michael@0 73 return null;
michael@0 74 }
michael@0 75
michael@0 76 }

mercurial