Merge "Adding a static method to get the size of a content url" into honeycomb

This commit is contained in:
Kristian Monsen
2011-01-12 11:15:09 -08:00
committed by Android (Google) Code Review

View File

@@ -17,8 +17,13 @@
package android.webkit; package android.webkit;
import android.content.Context; import android.content.Context;
import android.net.Uri;
import android.util.Log;
import java.io.InputStream;
class JniUtil { class JniUtil {
private static final String LOGTAG = "webkit";
private JniUtil() {} // Utility class, do not instantiate. private JniUtil() {} // Utility class, do not instantiate.
// Used by the Chromium HTTP stack. // Used by the Chromium HTTP stack.
@@ -69,6 +74,46 @@ class JniUtil {
return sCacheDirectory; 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. * Returns true if we're using the Chromium HTTP stack.
* *