Bug 726001 - Use BufferedHttpEntity for fetching favicons. r=mfinkle
authorBrian Nicholson <bnicholson@mozilla.com>
Wed, 28 Mar 2012 11:14:19 -0700
changeset 90546 2f0536c9c49790c8c79a9f0c68c4b844f36ece9e
parent 90545 baf59f769aed7a02b4cae352147bd3b68487575b
child 90547 97377dc127550fe8c0a3dff0843d4de4bbb02ad6
push idunknown
push userunknown
push dateunknown
reviewersmfinkle
bugs726001
milestone14.0a1
Bug 726001 - Use BufferedHttpEntity for fetching favicons. r=mfinkle
mobile/android/base/Favicons.java
--- a/mobile/android/base/Favicons.java
+++ b/mobile/android/base/Favicons.java
@@ -44,31 +44,32 @@ import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.database.sqlite.SQLiteQueryBuilder;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.AsyncTask;
 import android.util.Log;
 
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.io.IOException;
-import java.net.URLConnection;
-import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
 import org.mozilla.gecko.db.BrowserDB;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.entity.BufferedHttpEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+
 public class Favicons {
     private static final String LOGTAG = "GeckoFavicons";
 
     public static final long NOT_LOADING = 0;
 
     private Context mContext;
     private DatabaseHelper mDbHelper;
 
@@ -273,51 +274,34 @@ public class Favicons {
         private BitmapDrawable downloadFavicon(URL faviconUrl) {
             Log.d(LOGTAG, "Downloading favicon for URL = " + mPageUrl +
                           " with favicon URL = " + mFaviconUrl);
 
             if (mFaviconUrl.startsWith("jar:jar:")) {
                 return GeckoJarReader.getBitmapDrawable(mFaviconUrl);
             }
 
-            // due to android bug 6066, we must download the entire image before using it
-            // http://code.google.com/p/android/issues/detail?id=6066
-            URLConnection urlConnection = null;
-            BufferedInputStream contentStream = null;
-            ByteArrayInputStream byteStream = null;
+            // skia decoder sometimes returns null; workaround is to use BufferedHttpEntity
+            // http://groups.google.com/group/android-developers/browse_thread/thread/171b8bf35dbbed96/c3ec5f45436ceec8?lnk=raot 
             BitmapDrawable image = null;
-
+            InputStream contentStream = null;
             try {
-                urlConnection = faviconUrl.openConnection();
-                int length = urlConnection.getContentLength();
-                contentStream = new BufferedInputStream(urlConnection.getInputStream(), length);
-                byte[] bytes = new byte[length];
-                int pos = 0;
-                int offset = 0;
-                while ((pos = contentStream.read(bytes, offset, length - offset)) > 0)
-                    offset += pos;
-                if (length == offset) {
-                    byteStream = new ByteArrayInputStream(bytes);
-                    image = (BitmapDrawable) Drawable.createFromStream(byteStream, "src");
-                }
+                HttpGet request = new HttpGet(faviconUrl.toURI());
+                HttpEntity entity = new DefaultHttpClient().execute(request).getEntity();
+                BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
+                contentStream = bufferedEntity.getContent();
+                image = (BitmapDrawable) Drawable.createFromStream(contentStream, "src");
             } catch (IOException e) {
                 // just close up and return null
             } catch (Exception e) {
                 Log.e(LOGTAG, "Error reading favicon", e);
             } finally {
-                if (urlConnection != null && urlConnection instanceof HttpURLConnection) {
-                    HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
-                    httpConnection.disconnect();
-                }
-
                 try {
                     if (contentStream != null)
                         contentStream.close();
-                    if (byteStream != null)
-                        byteStream.close();
                 } catch (IOException e) {
                     Log.d(LOGTAG, "error closing favicon stream");
                 }
             }
 
             return image;
         }