package com.commonsware.android.advservice;

import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ParcelFileDescriptorUtil {
    private static final int MAX_BUFFER = 4096;

    public static ParcelFileDescriptor pipeFrom(InputStream inputStream)
            throws IOException {
        final ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
        final OutputStream output = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]);

        new TransferThread(inputStream, output).start();

        return pipe[0];
    }

    @SuppressWarnings("unused")
    public static ParcelFileDescriptor pipeTo(OutputStream outputStream)
            throws IOException {
        final ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
        final InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]);

        new TransferThread(input, outputStream).start();

        return pipe[1];
    }

    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[MAX_BUFFER];
            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", "writing failed");
                e.printStackTrace();
            } finally {
                try {
                    mIn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    mOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
@@ -104,16 +104,55 @@
 l.Log;%0A%0A
+import org.apache.commons.io.IOUtils;%0A%0A
 import j
@@ -273,57 +273,8 @@
 l %7B%0A
-    private static final int MAX_BUFFER = 4096;%0A%0A
     
@@ -1414,717 +1414,299 @@
     
-byte%5B%5D buf = new byte%5BMAX_BUFFER%5D;%0A            int len;%0A%0A            try %7B%0A                while ((len = mIn.read(buf)) %3E 0) %7B%0A                    mOut.write(buf, 0, len);%0A                %7D%0A                mOut.flush(); // just to be safe%0A            %7D catch (IOException e) %7B%0A                Log.e(%22TransferThread%22, %22writing failed%22);%0A                e.printStackTrace();%0A            %7D finally %7B%0A                try %7B%0A                    mIn.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A                try %7B%0A                    mOut.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D
+try %7B%0A                IOUtils.copy(mIn, mOut);%0A            %7D catch (IOException e) %7B%0A                Log.e(%22TransferThread%22, %22writing failed%22);%0A                e.printStackTrace();%0A            %7D finally %7B%0A                IOUtils.closeQuietly(mIn);%0A                IOUtils.closeQuietly(mOut);
 %0A   

simplify ParcelFileDescriptorUtil.