Chromium Code Reviews| Index: mojo/java/org/chromium/mojo/common/DataPipeUtils.java |
| diff --git a/mojo/java/org/chromium/mojo/common/DataPipeUtils.java b/mojo/java/org/chromium/mojo/common/DataPipeUtils.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bc022e95ba5c1940504cff5898c53a3c5ecc956c |
| --- /dev/null |
| +++ b/mojo/java/org/chromium/mojo/common/DataPipeUtils.java |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2015 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.mojo.common; |
| + |
| +// TODO(eseidel): Can we not depend on Android? |
|
abarth-chromium
2015/07/01 17:52:13
I think this is ok for this library. We don't wan
|
| +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 java.io.File; |
| +import java.io.FileOutputStream; |
| +import java.io.IOException; |
| +import java.nio.ByteBuffer; |
| +import java.nio.channels.FileChannel; |
| +import java.util.concurrent.Executor; |
| + |
| +/** |
| + * Java helpers for dealing with DataPipes. |
| + */ |
| +public class DataPipeUtils { |
| + private static final String TAG = "DataPipeUtils"; |
| + |
| + class CopyToFileJob implements Runnable { |
|
abarth-chromium
2015/07/01 17:52:13
Move outside the DataPipeUtils (but keep in this f
|
| + private final DataPipe.ConsumerHandle mSource; |
| + private final File mDest; |
| + private final Runnable mComplete; |
| + private final Handler mCaller; |
| + private final Core mCore; |
| + |
| + public CopyToFileJob(Core core, DataPipe.ConsumerHandle handle, |
| + File file, Runnable complete, Handler caller) { |
| + mCore = core; |
| + mSource = handle; |
| + mDest = file; |
| + mComplete = complete; |
| + mCaller = caller; |
| + } |
| + |
| + private void readLoop(FileChannel dest) { |
| + do { |
| + try { |
| + ByteBuffer buffer = mSource.beginReadData(0, |
| + DataPipe.ReadFlags.NONE); |
| + if (buffer.capacity() == 0) |
| + break; |
| + dest.write(buffer); |
| + mSource.endReadData(buffer.capacity()); |
| + } catch (MojoException e) { |
| + // No one read the pipe, they just closed it. |
| + if (e.getMojoResult() == MojoResult.FAILED_PRECONDITION) { |
| + break; |
| + } else if (e.getMojoResult() == MojoResult.SHOULD_WAIT) { |
| + mCore.wait(mSource, Core.HandleSignals.READABLE, -1); |
| + } 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(); |
| + 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); |
| + } |
| + |
| + mCaller.post(mComplete); |
| + } |
| + } |
| + |
| + public void copyToFile(Core core, DataPipe.ConsumerHandle source, File dest, |
|
abarth-chromium
2015/07/01 17:52:13
public void copyToFile -> public static void copyT
|
| + 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())); |
| + } |
| +} |