Search in sources :

Example 91 with FileDescriptor

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

the class ApfFilter method maybeStartFilter.

/**
     * Attempt to start listening for RAs and, if RAs are received, generating and installing
     * filters to ignore useless RAs.
     */
@VisibleForTesting
void maybeStartFilter() {
    FileDescriptor socket;
    try {
        mHardwareAddress = mNetworkInterface.getHardwareAddress();
        synchronized (this) {
            // Install basic filters
            installNewProgramLocked();
        }
        socket = Os.socket(AF_PACKET, SOCK_RAW, ETH_P_IPV6);
        PacketSocketAddress addr = new PacketSocketAddress((short) ETH_P_IPV6, mNetworkInterface.getIndex());
        Os.bind(socket, addr);
        NetworkUtils.attachRaFilter(socket, mApfCapabilities.apfPacketFormat);
    } catch (SocketException | ErrnoException e) {
        Log.e(TAG, "Error starting filter", e);
        return;
    }
    mReceiveThread = new ReceiveThread(socket);
    mReceiveThread.start();
}
Also used : SocketException(java.net.SocketException) PacketSocketAddress(android.system.PacketSocketAddress) ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 92 with FileDescriptor

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

the class LocalSocketTest method testBasic.

@SmallTest
public void testBasic() throws Exception {
    LocalServerSocket ss;
    LocalSocket ls;
    LocalSocket ls1;
    ss = new LocalServerSocket("android.net.LocalSocketTest");
    ls = new LocalSocket();
    ls.connect(new LocalSocketAddress("android.net.LocalSocketTest"));
    ls1 = ss.accept();
    // Test trivial read and write
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    // Test getting credentials
    Credentials c = ls1.getPeerCredentials();
    MoreAsserts.assertNotEqual(0, c.getPid());
    // Test sending and receiving file descriptors
    ls.setFileDescriptorsForSend(new FileDescriptor[] { FileDescriptor.in });
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    FileDescriptor[] out = ls1.getAncillaryFileDescriptors();
    assertEquals(1, out.length);
    // Test multible byte write and available()
    ls1.getOutputStream().write(new byte[] { 0, 1, 2, 3, 4, 5 }, 1, 5);
    assertEquals(1, ls.getInputStream().read());
    assertEquals(4, ls.getInputStream().available());
    byte[] buffer = new byte[16];
    int countRead;
    countRead = ls.getInputStream().read(buffer, 1, 15);
    assertEquals(4, countRead);
    assertEquals(2, buffer[1]);
    assertEquals(3, buffer[2]);
    assertEquals(4, buffer[3]);
    assertEquals(5, buffer[4]);
    // Try various array-out-of-bound cases
    try {
        ls.getInputStream().read(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    // Try read of length 0
    ls.getOutputStream().write(42);
    countRead = ls1.getInputStream().read(buffer, 0, 0);
    assertEquals(0, countRead);
    assertEquals(42, ls1.getInputStream().read());
    ss.close();
    ls.close();
    try {
        ls.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls.getInputStream().read();
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls1.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    // Try read on socket whose peer has closed
    assertEquals(-1, ls1.getInputStream().read());
    ls1.close();
}
Also used : LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) Credentials(android.net.Credentials) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 93 with FileDescriptor

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

the class NetworkTest method testBindSocketOfInvalidFdThrows.

@SmallTest
public void testBindSocketOfInvalidFdThrows() throws Exception {
    final FileDescriptor fd = new FileDescriptor();
    assertFalse(fd.valid());
    try {
        mNetwork.bindSocket(fd);
        fail("SocketException not thrown");
    } catch (SocketException expected) {
    }
}
Also used : SocketException(java.net.SocketException) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 94 with FileDescriptor

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

the class AccessoryChat method openAccessory.

private void openAccessory(UsbAccessory accessory) {
    Log.d(TAG, "openAccessory: " + accessory);
    mFileDescriptor = mUsbManager.openAccessory(accessory);
    if (mFileDescriptor != null) {
        FileDescriptor fd = mFileDescriptor.getFileDescriptor();
        mInputStream = new FileInputStream(fd);
        mOutputStream = new FileOutputStream(fd);
        Thread thread = new Thread(null, this, "AccessoryChat");
        thread.start();
        Log.d(TAG, "openAccessory succeeded");
    } else {
        Log.d(TAG, "openAccessory fail");
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream)

Example 95 with FileDescriptor

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

the class BluetoothSocket method bindListen.

/**
     * Currently returns unix errno instead of throwing IOException,
     * so that BluetoothAdapter can check the error code for EADDRINUSE
     */
/*package*/
int bindListen() {
    int ret;
    if (mSocketState == SocketState.CLOSED)
        return EBADFD;
    IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null);
    if (bluetoothProxy == null) {
        Log.e(TAG, "bindListen fail, reason: bluetooth is off");
        return -1;
    }
    try {
        mPfd = bluetoothProxy.createSocketChannel(mType, mServiceName, mUuid, mPort, getSecurityFlags());
    } catch (RemoteException e) {
        Log.e(TAG, Log.getStackTraceString(new Throwable()));
        return -1;
    }
    // read out port number
    try {
        synchronized (this) {
            if (DBG)
                Log.d(TAG, "bindListen(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
            if (mSocketState != SocketState.INIT)
                return EBADFD;
            if (mPfd == null)
                return -1;
            FileDescriptor fd = mPfd.getFileDescriptor();
            if (DBG)
                Log.d(TAG, "bindListen(), new LocalSocket ");
            mSocket = new LocalSocket(fd);
            if (DBG)
                Log.d(TAG, "bindListen(), new LocalSocket.getInputStream() ");
            mSocketIS = mSocket.getInputStream();
            mSocketOS = mSocket.getOutputStream();
        }
        if (DBG)
            Log.d(TAG, "bindListen(), readInt mSocketIS: " + mSocketIS);
        int channel = readInt(mSocketIS);
        synchronized (this) {
            if (mSocketState == SocketState.INIT)
                mSocketState = SocketState.LISTENING;
        }
        if (DBG)
            Log.d(TAG, "channel: " + channel);
        if (mPort <= -1) {
            mPort = channel;
        }
        // else ASSERT(mPort == channel)
        ret = 0;
    } catch (IOException e) {
        if (mPfd != null) {
            try {
                mPfd.close();
            } catch (IOException e1) {
                Log.e(TAG, "bindListen, close mPfd: " + e1);
            }
            mPfd = null;
        }
        Log.e(TAG, "bindListen, fail to get port number, exception: " + e);
        return -1;
    }
    return ret;
}
Also used : LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) RemoteException(android.os.RemoteException) 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