Search in sources :

Example 56 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project facebook-android-sdk by facebook.

the class FacebookContentProviderTest method testOpenFileWithKnownUri.

@Test
public void testOpenFileWithKnownUri() throws Exception {
    MockAttachmentStore.addAttachment(CALL_ID, ATTACHMENT_NAME);
    ParcelFileDescriptor pfd = getTestAttachmentParcelFileDescriptor(CALL_ID);
    assertNotNull(pfd);
    pfd.close();
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 57 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project glide by bumptech.

the class GlideTest method mockUri.

private String mockUri(Uri uri, InputStream is) {
    if (is == null) {
        is = new ByteArrayInputStream(new byte[0]);
    }
    ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver();
    ShadowFileDescriptorContentResolver shadowContentResolver = (ShadowFileDescriptorContentResolver) ShadowExtractor.extract(contentResolver);
    shadowContentResolver.registerInputStream(uri, is);
    AssetFileDescriptor assetFileDescriptor = mock(AssetFileDescriptor.class);
    ParcelFileDescriptor parcelFileDescriptor = mock(ParcelFileDescriptor.class);
    when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor);
    shadowContentResolver.registerAssetFileDescriptor(uri, assetFileDescriptor);
    return uri.toString();
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) ContentResolver(android.content.ContentResolver)

Example 58 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project glide by bumptech.

the class FileDescriptorLocalUriFetcherTest method testLoadResource_returnsFileDescriptor.

@Test
public void testLoadResource_returnsFileDescriptor() throws Exception {
    Context context = RuntimeEnvironment.application;
    Uri uri = Uri.parse("file://nothing");
    ContentResolver contentResolver = context.getContentResolver();
    ContentResolverShadow shadow = (ContentResolverShadow) ShadowExtractor.extract(contentResolver);
    AssetFileDescriptor assetFileDescriptor = mock(AssetFileDescriptor.class);
    ParcelFileDescriptor parcelFileDescriptor = mock(ParcelFileDescriptor.class);
    when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor);
    shadow.registerFileDescriptor(uri, assetFileDescriptor);
    FileDescriptorLocalUriFetcher fetcher = new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri);
    fetcher.loadData(Priority.NORMAL, callback);
    verify(callback).onDataReady(eq(parcelFileDescriptor));
}
Also used : Context(android.content.Context) AssetFileDescriptor(android.content.res.AssetFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptorLocalUriFetcher(com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher) ContentResolverShadow(com.bumptech.glide.tests.ContentResolverShadow) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 59 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project fqrouter by fqrouter.

the class SocksVpnService method passTcpFileDescriptor.

private void passTcpFileDescriptor(LocalSocket fdSocket, OutputStream outputStream, String dstIp, int dstPort, int connectTimeout) throws Exception {
    Socket sock = new Socket();
    // force file descriptor being created
    sock.setTcpNoDelay(true);
    try {
        ParcelFileDescriptor fd = ParcelFileDescriptor.fromSocket(sock);
        if (protect(fd.getFd())) {
            try {
                sock.connect(new InetSocketAddress(dstIp, dstPort), connectTimeout);
                try {
                    fdSocket.setFileDescriptorsForSend(new FileDescriptor[] { fd.getFileDescriptor() });
                    outputStream.write('*');
                    outputStream.flush();
                } finally {
                    sock.close();
                    fd.close();
                }
            } catch (ConnectException e) {
                LogUtils.e("connect " + dstIp + ":" + dstPort + " failed");
                outputStream.write('!');
            } catch (SocketTimeoutException e) {
                LogUtils.e("connect " + dstIp + ":" + dstPort + " failed");
                outputStream.write('!');
            } finally {
                outputStream.flush();
            }
        } else {
            LogUtils.e("protect tcp socket failed");
        }
    } finally {
        sock.close();
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) LocalServerSocket(android.net.LocalServerSocket) LocalSocket(android.net.LocalSocket)

Example 60 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project fqrouter by fqrouter.

the class SocksVpnService method passUdpFileDescriptor.

private void passUdpFileDescriptor(LocalSocket fdSocket, OutputStream outputStream) throws Exception {
    DatagramSocket sock = new DatagramSocket();
    try {
        ParcelFileDescriptor fd = ParcelFileDescriptor.fromDatagramSocket(sock);
        if (protect(fd.getFd())) {
            try {
                fdSocket.setFileDescriptorsForSend(new FileDescriptor[] { fd.getFileDescriptor() });
                outputStream.write('*');
                outputStream.flush();
            } finally {
                sock.close();
                fd.close();
            }
        } else {
            LogUtils.e("protect udp socket failed");
        }
    } finally {
        sock.close();
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Aggregations

ParcelFileDescriptor (android.os.ParcelFileDescriptor)526 IOException (java.io.IOException)199 FileNotFoundException (java.io.FileNotFoundException)136 RemoteException (android.os.RemoteException)127 File (java.io.File)127 FileDescriptor (java.io.FileDescriptor)58 FileOutputStream (java.io.FileOutputStream)58 AssetFileDescriptor (android.content.res.AssetFileDescriptor)44 FileInputStream (java.io.FileInputStream)36 Parcel (android.os.Parcel)35 Uri (android.net.Uri)33 Intent (android.content.Intent)30 Bundle (android.os.Bundle)29 Cursor (android.database.Cursor)27 StorageManager (android.os.storage.StorageManager)25 Request (android.app.DownloadManager.Request)24 Bitmap (android.graphics.Bitmap)22 InputStream (java.io.InputStream)18 ProfilerInfo (android.app.ProfilerInfo)17 Binder (android.os.Binder)17