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..24c452a6e01a07ec89b1b2e276eec0adb594d6f9 |
--- /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 { |
joth
2013/10/31 06:42:39
if this class is only used from native, no need to
qinmin
2013/11/05 01:41:31
Done.
|
+ 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(); |
joth
2013/10/31 06:42:39
oh right, if you're just opening the FD then calli
qinmin
2013/11/05 01:41:31
Done. Changed the GetFileInfo to call openContentU
|
+ try { |
+ pfd.close(); |
+ } catch (java.io.IOException e) { |
+ Log.w(TAG, "Failed to close the file: " + uriString + ", " + e); |
+ } |
+ 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.w(TAG, "Cannot find content uri: " + uriString + ", " + e); |
+ } |
+ return pfd; |
+ } |
+} |