From 80ff5d82bdbad51378a45d3a378213346ea22df0 Mon Sep 17 00:00:00 2001 From: Kristian Monsen Date: Fri, 24 Dec 2010 11:53:50 +0000 Subject: [PATCH] Adding a static method to get the size of a content url Part of fixes for bug 2862096 Change-Id: I86f1255c17efb367fac54b69b8de220d2874fc70 --- core/java/android/webkit/JniUtil.java | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/core/java/android/webkit/JniUtil.java b/core/java/android/webkit/JniUtil.java index 704d4dae81b66..cc1992920e8e0 100644 --- a/core/java/android/webkit/JniUtil.java +++ b/core/java/android/webkit/JniUtil.java @@ -17,8 +17,13 @@ package android.webkit; import android.content.Context; +import android.net.Uri; +import android.util.Log; + +import java.io.InputStream; class JniUtil { + private static final String LOGTAG = "webkit"; private JniUtil() {} // Utility class, do not instantiate. // Used by the Chromium HTTP stack. @@ -69,6 +74,46 @@ class JniUtil { return sCacheDirectory; } + /** + * Called by JNI. Calculates the size of an input stream by reading it. + * @return long The size of the stream + */ + private static synchronized long contentUrlSize(String url) { + final String ANDROID_CONTENT = "content:"; + + // content:// + if (url.startsWith(ANDROID_CONTENT)) { + try { + // Strip off mimetype, for compatibility with ContentLoader.java + // If we don't do this, we can fail to load Gmail attachments, + // because the URL being loaded doesn't exactly match the URL we + // have permission to read. + int mimeIndex = url.lastIndexOf('?'); + if (mimeIndex != -1) { + url = url.substring(0, mimeIndex); + } + Uri uri = Uri.parse(url); + InputStream is = sContext.getContentResolver().openInputStream(uri); + byte[] buffer = new byte[1024]; + int n; + long size = 0; + try { + while ((n = is.read(buffer)) != -1) { + size += n; + } + } finally { + is.close(); + } + return size; + } catch (Exception e) { + Log.e(LOGTAG, "Exception: " + url); + return 0; + } + } else { + return 0; + } + } + /** * Returns true if we're using the Chromium HTTP stack. *