private void checkNotClosed() throws IOException { if (!fd.valid()) { throw new SocketException("Socket is closed"); } }
public PlainSocketImpl(FileDescriptor fd, int localport, InetAddress addr, int port) { this.fd = fd; this.localport = localport; this.address = addr; this.port = port; if (fd.valid()) { guard.open("close"); } }
public PlainSocketImpl(FileDescriptor fd) { this.fd = fd; if (fd.valid()) { guard.open("close"); } }
public PlainDatagramSocketImpl(FileDescriptor fd, int localPort) { this.fd = fd; this.localPort = localPort; if (fd.valid()) { guard.open("close"); } }
@Override public void close() throws IOException { if (streamClosed) { return; } streamClosed = true; if (fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_RD); } closedInputStream = true; checkClose(); }
@Override protected void shutdownInput() throws IOException { if (!closed && fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_RD); } }
@Override protected void shutdownOutput() throws IOException { if (!closed && fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_WR); } }
@Override public void close() throws IOException { if (streamClosed) { return; } streamClosed = true; if (fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_WR); } closedOutputStream = true; checkClose(); } }
public static void closeQuietly(FileDescriptor fd) { if (fd != null && fd.valid()) { try { Os.close(fd); } catch (ErrnoException e) { e.printStackTrace(); } } }
/** * Calls close(2) on 'fd'. Also resets the internal int to -1. Does nothing if 'fd' is null * or invalid. */ public static void close(FileDescriptor fd) throws IOException { try { if (fd != null && fd.valid()) { Libcore.os.close(fd); } } catch (ErrnoException errnoException) { throw errnoException.rethrowAsIOException(); } }
@Override protected synchronized void close() throws IOException { if (closed) { return; } closed = true; if (fd.valid()) { NativeUnixSocket.shutdown(fd, SHUT_RD_WR); NativeUnixSocket.close(fd); } if (bound) { NativeUnixSocket.unlink(socketFile); } connected = false; }
@Test public void testOpens_canWriteWritableFile() throws Exception { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor()); os.write(5); os.close(); }
@Test public void testStatSize_writtenFile() throws Exception { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileOutputStream os = new FileOutputStream(pfd.getFileDescriptor()); os.write(5); assertThat(pfd.getStatSize()).isEqualTo(1); // One byte. os.close(); }
@Test public void testOpens_canReadReadOnlyFile() throws Exception { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(readOnlyFile, ParcelFileDescriptor.MODE_READ_ONLY); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); FileInputStream is = new FileInputStream(pfd.getFileDescriptor()); assertThat(is.read()).isEqualTo(READ_ONLY_FILE_CONTENTS); pfd.close(); }
@Test public void testOpens() throws Exception { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); pfd.close(); }
@Test public void testAutoCloseOutputStream() throws Exception { File f = new File(ApplicationProvider.getApplicationContext().getFilesDir(), "outfile"); ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, -1); ParcelFileDescriptor.AutoCloseOutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(pfd); os.close(); assertThat(pfd.getFileDescriptor().valid()).isFalse(); }
@Test public void testStatSize_emptyFile() throws Exception { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); assertThat(pfd).isNotNull(); assertThat(pfd.getFileDescriptor().valid()).isTrue(); assertThat(pfd.getStatSize()).isEqualTo(0); pfd.close(); }
public static void closeSocket(FileDescriptor fd) throws IOException { if (!fd.valid()) { // Socket.close doesn't throw if you try to close an already-closed socket. return; } int intFd = fd.getInt$(); fd.setInt$(-1); FileDescriptor oldFd = new FileDescriptor(); oldFd.setInt$(intFd); AsynchronousCloseMonitor.signalBlockedThreads(oldFd); try { Libcore.os.close(oldFd); } catch (ErrnoException errnoException) { // TODO: are there any cases in which we should throw? } }
@Test public void testCloses() throws Exception { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1); pfd.close(); assertThat(pfd.getFileDescriptor().valid()).isFalse(); assertThat(pfd.getFd()).isEqualTo(-1); }
@Test public void testAutoCloseInputStream() throws Exception { ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, -1); ParcelFileDescriptor.AutoCloseInputStream is = new ParcelFileDescriptor.AutoCloseInputStream(pfd); is.close(); assertThat(pfd.getFileDescriptor().valid()).isFalse(); }