Example usage for android.os ParcelFileDescriptor adoptFd

List of usage examples for android.os ParcelFileDescriptor adoptFd

Introduction

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

Prototype

public static ParcelFileDescriptor adoptFd(int fd) 

Source Link

Document

Take ownership of a raw native fd in to a new ParcelFileDescriptor.

Usage

From source file:net.sf.xfd.provider.PublicProvider.java

@Nullable
@Override/*from   ww  w.j  av  a 2 s.com*/
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String requestedMode, CancellationSignal signal)
        throws FileNotFoundException {
    String path = uri.getPath();

    assertAbsolute(path);

    final int readableMode = ParcelFileDescriptor.parseMode(requestedMode);

    if (signal != null) {
        final Thread theThread = Thread.currentThread();

        signal.setOnCancelListener(theThread::interrupt);
    }

    path = canonString(path);

    if (!path.equals(uri.getPath())) {
        uri = uri.buildUpon().path(path).build();
    }

    try {
        if (!checkAccess(uri, requestedMode)) {
            return null;
        }

        final OS rooted = base.getOS();

        if (rooted == null) {
            throw new FileNotFoundException("Failed to open " + uri.getPath() + ": unable to acquire access");
        }

        int openFlags;

        if ((readableMode & MODE_READ_ONLY) == readableMode) {
            openFlags = OS.O_RDONLY;
        } else if ((readableMode & MODE_WRITE_ONLY) == readableMode) {
            openFlags = OS.O_WRONLY;
        } else {
            openFlags = OS.O_RDWR;
        }

        if (signal == null) {
            openFlags |= NativeBits.O_NONBLOCK;
        }

        //noinspection WrongConstant
        @Fd
        int fd = rooted.open(path, openFlags, 0);

        return ParcelFileDescriptor.adoptFd(fd);
    } catch (IOException e) {
        throw new FileNotFoundException("Unable to open " + uri.getPath() + ": " + e.getMessage());
    } finally {
        if (signal != null) {
            signal.setOnCancelListener(null);
        }

        Thread.interrupted();
    }
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static OutputStream openOutputStream(Context context, DocumentFile f) throws IOException {
    ContentResolver cr = context.getContentResolver();
    ParcelFileDescriptor pfd = cr.openFileDescriptor(f.getUri(), "rw");

    int fd = pfd.detachFd(); // this trick the internal system to trigger the media scanner on nothing
    pfd = ParcelFileDescriptor.adoptFd(fd);

    return new AutoSyncOutputStream(pfd);
}

From source file:io.requery.android.database.sqlite.SQLiteConnection.java

/**
 * Executes a statement that returns a single BLOB result as a
 * file descriptor to a shared memory region.
 *
 * @param sql The SQL statement to execute.
 * @param bindArgs The arguments to bind, or null if none.
 * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
 * @return The file descriptor for a shared memory region that contains
 * the value of the first column in the first row of the result set as a BLOB,
 * or null if none./*  w  w w .  ja  va 2s .co m*/
 *
 * @throws SQLiteException if an error occurs, such as a syntax error
 * or invalid number of bind arguments.
 * @throws OperationCanceledException if the operation was canceled.
 */
public ParcelFileDescriptor executeForBlobFileDescriptor(String sql, Object[] bindArgs,
        CancellationSignal cancellationSignal) {
    if (sql == null) {
        throw new IllegalArgumentException("sql must not be null.");
    }

    final int cookie = mRecentOperations.beginOperation("executeForBlobFileDescriptor", sql, bindArgs);
    try {
        final PreparedStatement statement = acquirePreparedStatement(sql);
        try {
            throwIfStatementForbidden(statement);
            bindArguments(statement, bindArgs);
            applyBlockGuardPolicy(statement);
            attachCancellationSignal(cancellationSignal);
            try {
                int fd = nativeExecuteForBlobFileDescriptor(mConnectionPtr, statement.mStatementPtr);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                    return fd >= 0 ? ParcelFileDescriptor.adoptFd(fd) : null;
                } else {
                    throw new UnsupportedOperationException();
                }
            } finally {
                detachCancellationSignal(cancellationSignal);
            }
        } finally {
            releasePreparedStatement(statement);
        }
    } catch (RuntimeException ex) {
        mRecentOperations.failOperation(cookie, ex);
        throw ex;
    } finally {
        mRecentOperations.endOperation(cookie);
    }
}