diff options
| author | Daichi Hirono <hirono@google.com> | 2017-04-12 14:23:06 +0900 |
|---|---|---|
| committer | Daichi Hirono <hirono@google.com> | 2017-04-20 15:05:16 +0900 |
| commit | 3d9f3fccf21444aba68e31f8ddbc8c07dffd39c9 (patch) | |
| tree | 97e2fbe33674a96f1bc031ef4cb404562d55091e /core/java/android/content/ClipData.java | |
| parent | 1ce83f009ea6d51b7c03772343f54827c7dd9dd5 (diff) | |
Handle runtime exception in clip data
When invoking ContentProvider through binder, ContentProvider may return
any type of RuntimeException. To let ClipData fall back to URI when it
failes to obtain text explanation, the CL add a catch closure for
RuntimeException.
Bug: 37260995
Test: Manually
Change-Id: I54ffbbcb05be3f3ff1ebc1c3dc68c074e6eb9825
Diffstat (limited to 'core/java/android/content/ClipData.java')
| -rw-r--r-- | core/java/android/content/ClipData.java | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/core/java/android/content/ClipData.java b/core/java/android/content/ClipData.java index 6703bd426282..7f03c3abcddb 100644 --- a/core/java/android/content/ClipData.java +++ b/core/java/android/content/ClipData.java @@ -43,6 +43,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; +import libcore.io.IoUtils; /** * Representation of a clipped data on the clipboard. @@ -335,46 +336,46 @@ public class ClipData implements Parcelable { // If this Item has a URI value, try using that. Uri uri = getUri(); if (uri != null) { - // First see if the URI can be opened as a plain text stream // (of any sub-type). If so, this is the best textual // representation for it. + final ContentResolver resolver = context.getContentResolver(); + AssetFileDescriptor descr = null; FileInputStream stream = null; + InputStreamReader reader = null; try { - // Ask for a stream of the desired type. - AssetFileDescriptor descr = context.getContentResolver() - .openTypedAssetFileDescriptor(uri, "text/*", null); - stream = descr.createInputStream(); - InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); - - // Got it... copy the stream into a local string and return it. - StringBuilder builder = new StringBuilder(128); - char[] buffer = new char[8192]; - int len; - while ((len=reader.read(buffer)) > 0) { - builder.append(buffer, 0, len); + try { + // Ask for a stream of the desired type. + descr = resolver.openTypedAssetFileDescriptor(uri, "text/*", null); + } catch (SecurityException e) { + Log.w("ClipData", "Failure opening stream", e); + } catch (FileNotFoundException|RuntimeException e) { + // Unable to open content URI as text... not really an + // error, just something to ignore. } - return builder.toString(); - - } catch (SecurityException e) { - Log.w("ClipData", "Failure opening stream", e); - - } catch (FileNotFoundException e) { - // Unable to open content URI as text... not really an - // error, just something to ignore. - - } catch (IOException e) { - // Something bad has happened. - Log.w("ClipData", "Failure loading text", e); - return e.toString(); - - } finally { - if (stream != null) { + if (descr != null) { try { - stream.close(); + stream = descr.createInputStream(); + reader = new InputStreamReader(stream, "UTF-8"); + + // Got it... copy the stream into a local string and return it. + final StringBuilder builder = new StringBuilder(128); + char[] buffer = new char[8192]; + int len; + while ((len=reader.read(buffer)) > 0) { + builder.append(buffer, 0, len); + } + return builder.toString(); } catch (IOException e) { + // Something bad has happened. + Log.w("ClipData", "Failure loading text", e); + return e.toString(); } } + } finally { + IoUtils.closeQuietly(descr); + IoUtils.closeQuietly(stream); + IoUtils.closeQuietly(reader); } // If we couldn't open the URI as a stream, use the URI itself as a textual |
