From 4d089e6ae79b011abbd6495b5181674d91561188 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Tue, 15 Mar 2016 13:43:35 -0700 Subject: Fix flakey vcard test When the test fails, I see an error from MemoryFile, which is used in ParcelFileDescriptor.fromData(). However the thing is this method is deprecated -- so I rewrote it to use a pipe instead. Bug 27335482 Change-Id: Ib75d9e93ac2610d3145889fe269abeec3d7c3dee --- .../providers/contacts/ContactsProvider2.java | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index 6414fbdf..4ba7a818 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -191,7 +191,9 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; @@ -9092,10 +9094,24 @@ public class ContactsProvider2 extends AbstractContactsProvider try { stream.flush(); - final byte[] byteData = stream.toByteArray(); - return makeAssetFileDescriptor( - ParcelFileDescriptor.fromData(byteData, CONTACT_MEMORY_FILE_NAME), - byteData.length); + final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe(); + final FileDescriptor outFd = fds[1].getFileDescriptor(); + + AsyncTask task = new AsyncTask() { + @Override + protected Object doInBackground(Object... params) { + try (FileOutputStream fout = new FileOutputStream(outFd)) { + fout.write(stream.toByteArray()); + } catch (IOException|RuntimeException e) { + Log.w(TAG, "Failure closing pipe", e); + } + IoUtils.closeQuietly(outFd); + return null; + } + }; + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[])null); + + return makeAssetFileDescriptor(fds[0]); } catch (IOException e) { Log.w(TAG, "Problem writing stream into an ParcelFileDescriptor: " + e.toString()); return null; -- cgit v1.2.3