Search in sources :

Example 76 with FileDescriptor

use of java.io.FileDescriptor in project android_frameworks_base by ResurrectionRemix.

the class ParcelFileDescriptor method fromFd.

/** {@hide} */
public static ParcelFileDescriptor fromFd(FileDescriptor fd, Handler handler, final OnCloseListener listener) throws IOException {
    if (handler == null) {
        throw new IllegalArgumentException("Handler must not be null");
    }
    if (listener == null) {
        throw new IllegalArgumentException("Listener must not be null");
    }
    final FileDescriptor[] comm = createCommSocketPair();
    final ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd, comm[0]);
    final MessageQueue queue = handler.getLooper().getQueue();
    queue.addOnFileDescriptorEventListener(comm[1], OnFileDescriptorEventListener.EVENT_INPUT, new OnFileDescriptorEventListener() {

        @Override
        public int onFileDescriptorEvents(FileDescriptor fd, int events) {
            Status status = null;
            if ((events & OnFileDescriptorEventListener.EVENT_INPUT) != 0) {
                final byte[] buf = new byte[MAX_STATUS];
                status = readCommStatus(fd, buf);
            } else if ((events & OnFileDescriptorEventListener.EVENT_ERROR) != 0) {
                status = new Status(Status.DEAD);
            }
            if (status != null) {
                queue.removeOnFileDescriptorEventListener(fd);
                IoUtils.closeQuietly(fd);
                listener.onClose(status.asIOException());
                return 0;
            }
            return EVENT_INPUT;
        }
    });
    return pfd;
}
Also used : FileDescriptor(java.io.FileDescriptor) OnFileDescriptorEventListener(android.os.MessageQueue.OnFileDescriptorEventListener)

Example 77 with FileDescriptor

use of java.io.FileDescriptor in project android_frameworks_base by ResurrectionRemix.

the class ParcelFileDescriptor method createCommSocketPair.

private static FileDescriptor[] createCommSocketPair() throws IOException {
    try {
        // Use SOCK_SEQPACKET so that we have a guarantee that the status
        // is written and read atomically as one unit and is not split
        // across multiple IO operations.
        final FileDescriptor comm1 = new FileDescriptor();
        final FileDescriptor comm2 = new FileDescriptor();
        Os.socketpair(AF_UNIX, SOCK_SEQPACKET, 0, comm1, comm2);
        IoUtils.setBlocking(comm1, false);
        IoUtils.setBlocking(comm2, false);
        return new FileDescriptor[] { comm1, comm2 };
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Example 78 with FileDescriptor

use of java.io.FileDescriptor in project android_frameworks_base by ResurrectionRemix.

the class ParcelFileDescriptor method createReliableSocketPair.

/**
     * @hide
     */
public static ParcelFileDescriptor[] createReliableSocketPair(int type) throws IOException {
    try {
        final FileDescriptor[] comm = createCommSocketPair();
        final FileDescriptor fd0 = new FileDescriptor();
        final FileDescriptor fd1 = new FileDescriptor();
        Os.socketpair(AF_UNIX, type, 0, fd0, fd1);
        return new ParcelFileDescriptor[] { new ParcelFileDescriptor(fd0, comm[0]), new ParcelFileDescriptor(fd1, comm[1]) };
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Example 79 with FileDescriptor

use of java.io.FileDescriptor in project android_frameworks_base by ResurrectionRemix.

the class BitmapFactoryTest method testBitmapParcelFileDescriptor.

// tests that we can decode bitmaps from MemoryFiles
@SmallTest
public void testBitmapParcelFileDescriptor() throws Exception {
    Bitmap bitmap1 = Bitmap.createBitmap(new int[] { Color.BLUE }, 1, 1, Bitmap.Config.RGB_565);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap1.compress(Bitmap.CompressFormat.PNG, 100, out);
    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromData(out.toByteArray(), null);
    FileDescriptor fd = pfd.getFileDescriptor();
    assertNotNull("Got null FileDescriptor", fd);
    assertTrue("Got invalid FileDescriptor", fd.valid());
    Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd);
    assertNotNull("BitmapFactory returned null", bitmap);
    assertEquals("Bitmap width", 1, bitmap.getWidth());
    assertEquals("Bitmap height", 1, bitmap.getHeight());
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 80 with FileDescriptor

use of java.io.FileDescriptor in project android_frameworks_base by ResurrectionRemix.

the class DocumentsContract method getDocumentThumbnail.

/** {@hide} */
public static Bitmap getDocumentThumbnail(ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal) throws RemoteException, IOException {
    final Bundle openOpts = new Bundle();
    openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size);
    AssetFileDescriptor afd = null;
    Bitmap bitmap = null;
    try {
        afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal);
        final FileDescriptor fd = afd.getFileDescriptor();
        final long offset = afd.getStartOffset();
        // Try seeking on the returned FD, since it gives us the most
        // optimal decode path; otherwise fall back to buffering.
        BufferedInputStream is = null;
        try {
            Os.lseek(fd, offset, SEEK_SET);
        } catch (ErrnoException e) {
            is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE);
            is.mark(THUMBNAIL_BUFFER_SIZE);
        }
        // We requested a rough thumbnail size, but the remote size may have
        // returned something giant, so defensively scale down as needed.
        final BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        if (is != null) {
            BitmapFactory.decodeStream(is, null, opts);
        } else {
            BitmapFactory.decodeFileDescriptor(fd, null, opts);
        }
        final int widthSample = opts.outWidth / size.x;
        final int heightSample = opts.outHeight / size.y;
        opts.inJustDecodeBounds = false;
        opts.inSampleSize = Math.min(widthSample, heightSample);
        if (is != null) {
            is.reset();
            bitmap = BitmapFactory.decodeStream(is, null, opts);
        } else {
            try {
                Os.lseek(fd, offset, SEEK_SET);
            } catch (ErrnoException e) {
                e.rethrowAsIOException();
            }
            bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts);
        }
        // Transform the bitmap if requested. We use a side-channel to
        // communicate the orientation, since EXIF thumbnails don't contain
        // the rotation flags of the original image.
        final Bundle extras = afd.getExtras();
        final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
        if (orientation != 0) {
            final int width = bitmap.getWidth();
            final int height = bitmap.getHeight();
            final Matrix m = new Matrix();
            m.setRotate(orientation, width / 2, height / 2);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
        }
    } finally {
        IoUtils.closeQuietly(afd);
    }
    return bitmap;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Bundle(android.os.Bundle) AssetFileDescriptor(android.content.res.AssetFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) Point(android.graphics.Point) Bitmap(android.graphics.Bitmap) ErrnoException(android.system.ErrnoException) Matrix(android.graphics.Matrix) BufferedInputStream(java.io.BufferedInputStream) BitmapFactory(android.graphics.BitmapFactory)

Aggregations

FileDescriptor (java.io.FileDescriptor)363 IOException (java.io.IOException)162 ParcelFileDescriptor (android.os.ParcelFileDescriptor)95 ErrnoException (android.system.ErrnoException)95 FileInputStream (java.io.FileInputStream)83 File (java.io.File)51 AssetFileDescriptor (android.content.res.AssetFileDescriptor)37 FileOutputStream (java.io.FileOutputStream)35 Bitmap (android.graphics.Bitmap)30 LocalSocket (android.net.LocalSocket)26 FileNotFoundException (java.io.FileNotFoundException)26 SmallTest (android.test.suitebuilder.annotation.SmallTest)17 InputStream (java.io.InputStream)16 Socket (java.net.Socket)14 ErrnoException (libcore.io.ErrnoException)14 Test (org.junit.Test)14 ServerSocket (java.net.ServerSocket)13 SSLHandshakeCallbacks (org.conscrypt.NativeCrypto.SSLHandshakeCallbacks)13 RemoteException (android.os.RemoteException)12 BitmapFactory (android.graphics.BitmapFactory)11