Index: net/android/java/src/org/chromium/net/ContentUriUtils.java |
diff --git a/net/android/java/src/org/chromium/net/ContentUriUtils.java b/net/android/java/src/org/chromium/net/ContentUriUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..294528942dfb1af2c4480aac9b4704657f31a8a3 |
--- /dev/null |
+++ b/net/android/java/src/org/chromium/net/ContentUriUtils.java |
@@ -0,0 +1,83 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.net; |
+ |
+import android.content.ContentResolver; |
+import android.content.Context; |
+import android.net.Uri; |
+import android.os.ParcelFileDescriptor; |
+import android.util.Log; |
+ |
+import org.chromium.base.CalledByNative; |
+ |
+/** |
+ * This class provides methods to access content URI schemes. |
+ */ |
+public abstract class ContentUriUtils { |
+ private static final String TAG = "ContentUriUtils"; |
+ |
+ // Prevent instantiation. |
+ private ContentUriUtils() {} |
+ |
+ /** |
+ * Opens the content URI for reading, and returns the file descriptor to |
+ * the caller. The caller is responsible for closing the file desciptor. |
+ * |
+ * @param context {@link Context} in interest |
+ * @param uriString the content URI to open |
+ * @returns file desciptor upon sucess, or -1 otherwise. |
+ */ |
+ @CalledByNative |
+ public static int openContentUriForRead(Context context, String uriString) { |
+ ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString); |
+ if (pfd != null) { |
+ return pfd.detachFd(); |
+ } |
+ return -1; |
+ } |
+ |
+ /** |
+ * Get the length of the file that a content URI points to. |
+ * |
+ * @param context {@link Context} in interest |
+ * @param uriString the content URI to open |
+ * @returns length of the file on success, or -1 otherwise. |
+ */ |
+ @CalledByNative |
+ public static long getContentLength(Context context, String uriString) { |
+ ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString); |
+ if (pfd == null) { |
+ return -1; |
+ } |
+ |
+ long size = pfd.getStatSize(); |
+ try { |
+ pfd.close(); |
+ } catch (java.io.IOException e) { |
+ Log.e(TAG, "Failed to close the file: " + uriString + ", " + e.toString()); |
+ } |
+ return size; |
+ } |
+ |
+ /** |
+ * Helper method to open a content URI and return the ParcelFileDescriptor. |
+ * |
+ * @param context {@link Context} in interest |
+ * @param uriString the content URI to open |
+ * @returns ParcelFileDescriptor of the content URI, or NULL if the file does not exist. |
+ */ |
+ private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) { |
+ ContentResolver resolver = context.getContentResolver(); |
+ Uri uri = Uri.parse(uriString); |
+ |
+ ParcelFileDescriptor pfd = null; |
+ try { |
+ pfd = resolver.openFileDescriptor(uri, "r"); |
+ } catch (java.io.FileNotFoundException e) { |
+ Log.e(TAG, "Cannot find content uri: " + uriString + ", " + e.toString()); |
joth
2013/10/29 01:30:25
nit: pass 'e' as the third param.
not sure if fil
qinmin
2013/10/29 02:45:57
Done. I am not sure how we can get exception on an
|
+ } |
+ return pfd; |
+ } |
+} |