Search in sources :

Example 56 with FileDescriptor

use of java.io.FileDescriptor in project robovm by robovm.

the class NativeCryptoTest method test_SSL_SESSION_get_time.

public void test_SSL_SESSION_get_time() throws Exception {
    try {
        NativeCrypto.SSL_SESSION_get_time(NULL);
        fail();
    } catch (NullPointerException expected) {
    }
    final ServerSocket listener = new ServerSocket(0);
    {
        Hooks cHooks = new Hooks() {

            @Override
            public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                long time = NativeCrypto.SSL_SESSION_get_time(session);
                assertTrue(time != 0);
                assertTrue(time < System.currentTimeMillis());
                super.afterHandshake(session, s, c, sock, fd, callback);
            }
        };
        Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
        Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
        Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
        client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) Future(java.util.concurrent.Future) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) SSLException(javax.net.ssl.SSLException)

Example 57 with FileDescriptor

use of java.io.FileDescriptor in project hadoop by apache.

the class TestNativeIO method testSetFilePointer.

@Test(timeout = 30000)
public void testSetFilePointer() throws Exception {
    assumeWindows();
    LOG.info("Set a file pointer on Windows");
    try {
        File testfile = new File(TEST_DIR, "testSetFilePointer");
        assertTrue("Create test subject", testfile.exists() || testfile.createNewFile());
        FileWriter writer = new FileWriter(testfile);
        try {
            for (int i = 0; i < 200; i++) if (i < 100)
                writer.write('a');
            else
                writer.write('b');
            writer.flush();
        } catch (Exception writerException) {
            fail("Got unexpected exception: " + writerException.getMessage());
        } finally {
            writer.close();
        }
        FileDescriptor fd = NativeIO.Windows.createFile(testfile.getCanonicalPath(), NativeIO.Windows.GENERIC_READ, NativeIO.Windows.FILE_SHARE_READ | NativeIO.Windows.FILE_SHARE_WRITE | NativeIO.Windows.FILE_SHARE_DELETE, NativeIO.Windows.OPEN_EXISTING);
        NativeIO.Windows.setFilePointer(fd, 120, NativeIO.Windows.FILE_BEGIN);
        FileReader reader = new FileReader(fd);
        try {
            int c = reader.read();
            assertTrue("Unexpected character: " + c, c == 'b');
        } catch (Exception readerException) {
            fail("Got unexpected exception: " + readerException.getMessage());
        } finally {
            reader.close();
        }
    } catch (Exception e) {
        fail("Got unexpected exception: " + e.getMessage());
    }
}
Also used : FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) Test(org.junit.Test)

Example 58 with FileDescriptor

use of java.io.FileDescriptor in project hadoop by apache.

the class TestNativeIO method testCreateFile.

@Test(timeout = 30000)
public void testCreateFile() throws Exception {
    assumeWindows();
    LOG.info("Open a file on Windows with SHARE_DELETE shared mode");
    try {
        File testfile = new File(TEST_DIR, "testCreateFile");
        assertTrue("Create test subject", testfile.exists() || testfile.createNewFile());
        FileDescriptor fd = NativeIO.Windows.createFile(testfile.getCanonicalPath(), NativeIO.Windows.GENERIC_READ, NativeIO.Windows.FILE_SHARE_READ | NativeIO.Windows.FILE_SHARE_WRITE | NativeIO.Windows.FILE_SHARE_DELETE, NativeIO.Windows.OPEN_EXISTING);
        FileInputStream fin = new FileInputStream(fd);
        try {
            fin.read();
            File newfile = new File(TEST_DIR, "testRenamedFile");
            boolean renamed = testfile.renameTo(newfile);
            assertTrue("Rename failed.", renamed);
            fin.read();
        } catch (Exception e) {
            fail("Got unexpected exception: " + e.getMessage());
        } finally {
            fin.close();
        }
    } catch (Exception e) {
        fail("Got unexpected exception: " + e.getMessage());
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 59 with FileDescriptor

use of java.io.FileDescriptor in project hadoop by apache.

the class TestNativeIO method testFDDoesntLeak.

/**
   * Test that opens and closes a file 10000 times - this would crash with
   * "Too many open files" if we leaked fds using this access pattern.
   */
@Test(timeout = 30000)
public void testFDDoesntLeak() throws IOException {
    assumeNotWindows();
    for (int i = 0; i < 10000; i++) {
        FileDescriptor fd = NativeIO.POSIX.open(new File(TEST_DIR, "testNoFdLeak").getAbsolutePath(), O_WRONLY | O_CREAT, 0700);
        assertNotNull(true);
        assertTrue(fd.valid());
        FileOutputStream fos = new FileOutputStream(fd);
        fos.write("foo".getBytes());
        fos.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileDescriptor(java.io.FileDescriptor) Test(org.junit.Test)

Example 60 with FileDescriptor

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

the class BackupHelperDispatcher method doOneBackup.

private void doOneBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState, Header header, BackupHelper helper) throws IOException {
    int err;
    FileDescriptor newStateFD = newState.getFileDescriptor();
    // allocate space for the header in the file
    int pos = allocateHeader_native(header, newStateFD);
    if (pos < 0) {
        throw new IOException("allocateHeader_native failed (error " + pos + ")");
    }
    data.setKeyPrefix(header.keyPrefix);
    // do the backup
    helper.performBackup(oldState, data, newState);
    // fill in the header (seeking back to pos).  The file pointer will be returned to
    // where it was at the end of performBackup.  Header.chunkSize will not be filled in.
    err = writeHeader_native(header, newStateFD, pos);
    if (err != 0) {
        throw new IOException("writeHeader_native failed (error " + err + ")");
    }
}
Also used : IOException(java.io.IOException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

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