Example usage for android.os ParcelFileDescriptor createPipe

List of usage examples for android.os ParcelFileDescriptor createPipe

Introduction

In this page you can find the example usage for android.os ParcelFileDescriptor createPipe.

Prototype

public static ParcelFileDescriptor[] createPipe() throws IOException 

Source Link

Document

Create two ParcelFileDescriptors structured as a data pipe.

Usage

From source file:com.github.mjdev.libaums.storageprovider.util.ParcelFileDescriptorUtil.java

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];
}

From source file:com.github.mjdev.libaums.storageprovider.util.ParcelFileDescriptorUtil.java

@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];
}

From source file:org.opensilk.music.artwork.provider.ArtworkProvider.java

private ParcelFileDescriptor pullSnapshot(String cacheKey) {
    Timber.v("Checking DiskCache for " + cacheKey);
    try {//  w w  w  .j  av  a  2  s  . com
        if (mL2Cache == null) {
            throw new IOException("Unable to obtain cache instance");
        }
        final ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
        final OutputStream out = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]);
        final DiskLruCache.Snapshot snapshot = mL2Cache.getSnapshot(cacheKey);
        if (snapshot != null && snapshot.getInputStream(0) != null) {
            final Scheduler.Worker worker = mScheduler.createWorker();
            worker.schedule(new Action0() {
                @Override
                public void call() {
                    try {
                        IOUtils.copy(snapshot.getInputStream(0), out);
                    } catch (IOException e) {
                        Timber.e(e, "ParcelFileDescriptorPipe");
                    } finally {
                        snapshot.close();
                        IOUtils.closeQuietly(out);
                        worker.unsubscribe();
                    }
                }
            });
            return pipe[0];
        } else {
            pipe[0].close();
            out.close();
        }
    } catch (IOException e) {
        Timber.w("pullSnapshot failed: %s", e.getMessage());
    }
    return null;
}

From source file:com.github.michalbednarski.intentslab.Utils.java

@TargetApi(13) // Function handles all supported api levels
public static InputStream dumpSystemService(Context context, String serviceName, final String[] arguments)
        throws Exception {
    // Check if we have permission to invoke dump from our process
    final boolean canDumpLocally = context.getPackageManager().checkPermission(android.Manifest.permission.DUMP,
            context.getPackageName()) == PackageManager.PERMISSION_GRANTED;

    // On versions without createPipe() just execute dumpsys
    if (android.os.Build.VERSION.SDK_INT < 9) {
        if (!canDumpLocally) {
            throw new Exception("Dumping is not supported on this system version");
        }// ww  w. j ava 2 s. c o m
        String[] progArray = new String[arguments != null ? 2 + arguments.length : 2];
        progArray[0] = "dumpsys";
        progArray[1] = serviceName;
        if (arguments != null) {
            System.arraycopy(arguments, 0, progArray, 2, arguments.length);
        }
        return Runtime.getRuntime().exec(progArray).getInputStream();
    }

    // Get service
    final Class<?> serviceManager = Class.forName("android.os.ServiceManager");
    final IBinder service = (IBinder) serviceManager.getMethod("getService", String.class).invoke(null,
            serviceName);

    // Check permissions and get remote interface if needed
    IRemoteInterface remoteInterface = null;
    if (!canDumpLocally) {
        remoteInterface = RunAsManager.getRemoteInterfaceForSystemDebuggingCommands();
        if (remoteInterface == null) {
            throw new SecurityException("Process has no permission to dump services");
        }
    }

    // Create pipe, write(pipe[0]) -> read(pipe[1])
    final ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
    final ParcelFileDescriptor readablePipe = pipe[0];
    final ParcelFileDescriptor writablePipe = pipe[1];

    try {
        // Execute dump
        if (canDumpLocally) {
            if (android.os.Build.VERSION.SDK_INT >= 13) {
                service.dumpAsync(writablePipe.getFileDescriptor(), arguments);
                writablePipe.close();
            } else {
                (new Thread() {
                    @Override
                    public void run() {
                        try {
                            service.dump(writablePipe.getFileDescriptor(), arguments);
                            writablePipe.close();
                        } catch (Exception e) {
                            // TODO: can we handle this?
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        } else {
            remoteInterface.dumpServiceAsync(service, writablePipe, arguments);
            writablePipe.close();
        }
        // If anything went wrong, close pipe and rethrow
    } catch (Throwable e) {
        readablePipe.close();
        writablePipe.close();
        throwUnchecked(e);
        throw new Error(); // Unreachable
    }

    // Return stream that will ensure closing fd
    return new FileInputStream(readablePipe.getFileDescriptor()) {
        @Override
        public void close() throws IOException {
            super.close();
            readablePipe.close();
        }
    };
}

From source file:org.opensilk.music.artwork.ArtworkRequestManagerImpl.java

private ParcelFileDescriptor pullSnapshot(String cacheKey) {
    Timber.d("Checking DiskCache for " + cacheKey);
    try {/*from   w  ww  . j a  va  2 s  .  c  o m*/
        if (mL2Cache == null) {
            throw new IOException("Unable to obtain cache instance");
        }
        final ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
        final OutputStream out = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]);
        final DiskLruCache.Snapshot snapshot = mL2Cache.getSnapshot(cacheKey);
        if (snapshot != null && snapshot.getInputStream(0) != null) {
            final Scheduler.Worker worker = Schedulers.io().createWorker();
            worker.schedule(new Action0() {
                @Override
                public void call() {
                    try {
                        IOUtils.copy(snapshot.getInputStream(0), out);
                    } catch (IOException e) {
                        //                            e.printStackTrace();
                    } finally {
                        snapshot.close();
                        IOUtils.closeQuietly(out);
                        worker.unsubscribe();
                    }
                }
            });
            return pipe[0];
        } else {
            pipe[0].close();
            out.close();
        }
    } catch (IOException e) {
        Timber.w(e, "pullSnapshot");
    }
    return null;
}