Index: mojo/java/src/org/chromium/mojo/common/DataPipeUtils.java |
diff --git a/mojo/java/org/chromium/mojo/common/DataPipeUtils.java b/mojo/java/src/org/chromium/mojo/common/DataPipeUtils.java |
similarity index 59% |
rename from mojo/java/org/chromium/mojo/common/DataPipeUtils.java |
rename to mojo/java/src/org/chromium/mojo/common/DataPipeUtils.java |
index b35ea0c97e833e1e7b1872056b257e7c47c9a879..fb8389b8317f29917da88c94898f4c180a1d8183 100644 |
--- a/mojo/java/org/chromium/mojo/common/DataPipeUtils.java |
+++ b/mojo/java/src/org/chromium/mojo/common/DataPipeUtils.java |
@@ -4,13 +4,11 @@ |
package org.chromium.mojo.common; |
-import android.os.Handler; |
-import android.util.Log; |
- |
import org.chromium.mojo.system.Core; |
import org.chromium.mojo.system.DataPipe; |
import org.chromium.mojo.system.MojoException; |
import org.chromium.mojo.system.MojoResult; |
+import org.chromium.mojo.system.RunLoop; |
import java.io.File; |
import java.io.FileOutputStream; |
@@ -19,21 +17,18 @@ import java.nio.ByteBuffer; |
import java.nio.channels.FileChannel; |
import java.util.concurrent.Executor; |
- |
/** |
* Helper class for copyToFile. |
*/ |
class CopyToFileJob implements Runnable { |
- private static final String TAG = "CopyToFileJob"; |
- |
private final DataPipe.ConsumerHandle mSource; |
private final File mDest; |
private final Runnable mComplete; |
- private final Handler mCaller; |
+ private final RunLoop mCaller; |
private final Core mCore; |
- public CopyToFileJob(Core core, DataPipe.ConsumerHandle handle, |
- File file, Runnable complete, Handler caller) { |
+ public CopyToFileJob(Core core, DataPipe.ConsumerHandle handle, File file, Runnable complete, |
+ RunLoop caller) { |
mCore = core; |
mSource = handle; |
mDest = file; |
@@ -41,13 +36,11 @@ class CopyToFileJob implements Runnable { |
mCaller = caller; |
} |
- private void readLoop(FileChannel dest) { |
+ private void readLoop(FileChannel dest) throws IOException { |
do { |
try { |
- ByteBuffer buffer = mSource.beginReadData(0, |
- DataPipe.ReadFlags.NONE); |
- if (buffer.capacity() == 0) |
- break; |
+ ByteBuffer buffer = mSource.beginReadData(0, DataPipe.ReadFlags.NONE); |
+ if (buffer.capacity() == 0) break; |
dest.write(buffer); |
mSource.endReadData(buffer.capacity()); |
} catch (MojoException e) { |
@@ -59,42 +52,30 @@ class CopyToFileJob implements Runnable { |
} else { |
throw e; |
} |
- } catch (IOException e) { |
- Log.e(TAG, "mDest.write failed", e); |
- break; |
} |
} while (true); |
} |
@Override |
public void run() { |
- FileChannel dest = null; |
- try { |
- dest = new FileOutputStream(mDest, false).getChannel(); |
+ try (DataPipe.ConsumerHandle source = mSource; |
+ FileOutputStream stream = new FileOutputStream(mDest, false); |
+ FileChannel dest = stream.getChannel()) { |
readLoop(dest); |
- } catch (java.io.FileNotFoundException e) { |
- Log.e(TAG, "destination file does not exist ", e); |
- } |
- mSource.close(); |
- try { |
- dest.close(); |
- } catch (IOException e) { |
- Log.e(TAG, "failed to close file ", e); |
+ } catch (java.io.IOException e) { |
+ throw new MojoException(e); |
} |
- mCaller.post(mComplete); |
+ mCaller.postDelayedTask(mComplete, 0L); |
} |
} |
- |
/** |
* Java helpers for dealing with DataPipes. |
*/ |
public class DataPipeUtils { |
- public static void copyToFile(Core core, DataPipe.ConsumerHandle source, |
- File dest, Executor executor, Runnable complete) { |
- // The default constructor for Handler uses the Looper for this thread. |
- executor.execute( |
- new CopyToFileJob(core, source, dest, complete, new Handler())); |
+ public static void copyToFile(Core core, DataPipe.ConsumerHandle source, File dest, |
+ Executor executor, Runnable complete) { |
+ executor.execute(new CopyToFileJob(core, source, dest, complete, core.getCurrentRunLoop())); |
} |
} |