16

ParcelFileDescriptor.createPipe()我想通过使用流到流复制线程和 ParcelFileDescriptor将 InputStream 从一个 Android 服务“发送”到在不同进程中运行的另一个服务,它代表管道的读取端,它被提供给另一个服务借助 Binder IPC。

发送代码(过程 A)

我想将给定的 InputStream 发送到接收服务:

public sendInputStream() {
    InputStream is = ...; // that's the stream for process/service B
    ParcelFileDescriptor pdf = ParcelFileDescriptorUtil.pipeFrom(is);
    inputStreamService.inputStream(pdf);
}

ParcelFileDescriptorUtil 是一个辅助类,具有经典java.io.的流到流复制线程:

public class ParcelFileDescriptorUtil {

    public static ParcelFileDescriptor pipeFrom(InputStream inputStream) throws IOException {
        ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
        ParcelFileDescriptor readSide = pipe[0];
        ParcelFileDescriptor writeSide = pipe[1];

        // start the transfer thread
        new TransferThread(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writeSide)).start();

        return readSide;
    }

    static class TransferThread extends Thread {
        final InputStream mIn;
        final OutputStream mOut;

        TransferThread(InputStream in, OutputStream out) {
            super("ParcelFileDescriptor Transfer Thread");
            mIn = in;
            mOut = out;
            setDaemon(true);
        }

        @Override
        public void run() {
            byte[] buf = new byte[1024];
            int len;

            try {
                while ((len = mIn.read(buf)) > 0) {
                    mOut.write(buf, 0, len);
                }
                mOut.flush(); // just to be safe
            } catch (IOException e) {
                LOG.e("TransferThread", e);
            }
            finally {
                try {
                    mIn.close();
                } catch (IOException e) {
                }
                try {
                    mOut.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

接收服务代码(过程 B)

接收服务的.aidl

package org.exmaple;
interface IInputStreamService {
    void inputStream(in ParcelFileDescriptor pfd);
}

进程 A 调用的接收服务:

public class InputStreamService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

private final IInputStreamService.Stub mBinder = new IInputStreamService.Stub() {

    @Override
    public void inputStream(ParcelFileDescriptor pfd) throws RemoteException {

        InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
        OutputStream os = ...;
        int len;
        byte[] buf = new byte[1024];
        try {
            while ((len = is.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
        } catch (IOException e) {
                        // this catches the exception shown below
        }
    }
};

in.read()总是inputStream()抛出一个IOException

java.io.IOException: read failed: EBADF (Bad file number)
    at libcore.io.IoBridge.read(IoBridge.java:442)
    at java.io.FileInputStream.read(FileInputStream.java:179)
    at java.io.InputStream.read(InputStream.java:163)

似乎 EBADF errno 是由read()文件描述符关闭时设置的。但我不知道是什么原因造成的以及如何解决它。

是的,我知道 ConentProvider 也是一种可能性。但它不应该也适用于我的方法吗?有没有其他方法可以将 InputStream 流传递给 Android 中的不同服务?

附带说明:CommonsWare使用 ContentProvider创建了一个类似的项目(相关的 SO 问题12)。我从这里获得了我的方法的大部分想法

4

1 回答 1

14

似乎原因是ParcelFileDescriptor服务方法的一个参数。如果服务确实返回ParcelFileDescriptor它会按预期工作。

发送服务(过程 A)

public void sendInputStream() {
    InputStream is = ...; // that's the stream for process/service B
    ParcelFileDescriptor pfd = inputStreamService.inputStream();
    OutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(pfd);

    int len;
    byte[] buf = new byte[1024];
    try {
        while ((len = is.read(buf)) > 0) {
            os.write(buf, 0, len);
        } 
    } catch (IOException e) {
    } finally {
        try { is.close(); } catch (IOException e1) {}
        try { os.close(); } catch (IOException e1) {}
    }
}

接收服务代码(过程 B)

接收服务的.aidl

package org.exmaple;
interface IInputStreamService {
    ParcelFileDescriptor inputStream();
}

进程 A 调用的接收服务:

public class InputStreamService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

private final IInputStreamService.Stub mBinder = new IInputStreamService.Stub() {

    @Override
    public void ParcelFileDescriptor inputStream() throws RemoteException {
                // one can read the contents of the Processes A's InputStream
                // from the following OutputStream
                OutputStream os = ...;
                ParcelFileDescriptor pfd = ParcelFileDescriptorUtil.pipeTo(os);
                return pfd;
    }
};

ParcelFileDescriptorUtil 是一个辅助类,具有经典java.io.的流到流复制线程。现在我们必须使用pipeTo()方法

public class ParcelFileDescriptorUtil {

    public static ParcelFileDescriptor pipeTo(OutputStream outputStream) throws IOException {
        ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
        ParcelFileDescriptor readSide = pipe[0];
        ParcelFileDescriptor writeSide = pipe[1];

        // start the transfer thread
        new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(readSide), outputStream).start();

        return writeSide;
    }

    static class TransferThread extends Thread {
        final InputStream mIn;
        final OutputStream mOut;

        TransferThread(InputStream in, OutputStream out) {
            super("ParcelFileDescriptor Transfer Thread");
            mIn = in;
            mOut = out;
            setDaemon(true);
        }

        @Override
        public void run() {
            byte[] buf = new byte[1024];
            int len;

            try {
                while ((len = mIn.read(buf)) > 0) {
                    mOut.write(buf, 0, len);
                }
                mOut.flush(); // just to be safe
            } catch (IOException e) {
                LOG.e("TransferThread", e);
            }
            finally {
                try {
                    mIn.close();
                } catch (IOException e) {
                }
                try {
                    mOut.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

这允许您跨进程边界传输 InputStream,一个缺点是流到流的副本涉及一些 CPU 时间。

于 2013-08-14T12:48:37.100 回答