Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 80ff5d82 authored by Kristian Monsen's avatar Kristian Monsen
Browse files

Adding a static method to get the size of a content url

Part of fixes for bug 2862096

Change-Id: I86f1255c17efb367fac54b69b8de220d2874fc70
parent 31d9d895
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -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.
     *