private static String connectDetail(InetAddress inetAddress, int port, int timeoutMs, ErrnoException cause) { String detail = "failed to connect to " + inetAddress + " (port " + port + ")"; if (timeoutMs > 0) { detail += " after " + timeoutMs + "ms"; } if (cause != null) { detail += ": " + cause.getMessage(); } return detail; }
private FileDescriptor tagSocket(FileDescriptor fd) throws ErrnoException { try { SocketTagger.get().tag(fd); return fd; } catch (SocketException e) { throw new ErrnoException("socket", EINVAL, e); } }
/** * Returns the length of this file in bytes. * * @return the file's length in bytes. * @throws IOException * if this file is closed or some other I/O error occurs. */ public long length() throws IOException { try { return Libcore.os.fstat(fd).st_size; } catch (ErrnoException errnoException) { throw errnoException.rethrowAsIOException(); } }
throw new ErrnoException("open", EISDIR); FileNotFoundException ex = new FileNotFoundException(path + ": " + errnoException.getMessage()); ex.initCause(errnoException); throw ex;
private static int maybeThrowAfterSendto(boolean isDatagram, ErrnoException errnoException) throws SocketException { if (isDatagram) { if (errnoException.errno == ECONNRESET || errnoException.errno == ECONNREFUSED) { return 0; } } else { if (errnoException.errno == EAGAIN) { // We were asked to write to a non-blocking socket, but were told // it would block, so report "no bytes written". return 0; } } throw errnoException.rethrowAsSocketException(); }
private static int maybeThrowAfterRecvfrom(boolean isRead, boolean isConnected, ErrnoException errnoException) throws SocketException, SocketTimeoutException { if (isRead) { if (errnoException.errno == EAGAIN) { return 0; } else { throw errnoException.rethrowAsSocketException(); } } else { if (isConnected && errnoException.errno == ECONNREFUSED) { throw new PortUnreachableException("", errnoException); } else if (errnoException.errno == EAGAIN) { throw new SocketTimeoutException(errnoException); } else { throw errnoException.rethrowAsSocketException(); } } }
public SocketException rethrowAsSocketException() throws SocketException { throw new SocketException(getMessage(), this); } }
/** * java.net has its own socket options similar to the underlying Unix ones. We paper over the * differences here. */ public static Object getSocketOption(FileDescriptor fd, int option) throws SocketException { try { return getSocketOptionErrno(fd, option); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * Gets the current position within this file. All reads and * writes take place at the current file pointer position. * * @return the current offset in bytes from the beginning of the file. * * @throws IOException * if an error occurs while getting the file pointer of this * file. */ public long getFilePointer() throws IOException { try { return Libcore.os.lseek(fd, 0L, SEEK_CUR); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsIOException(); } }
private void untagSocket(FileDescriptor fd) throws ErrnoException { try { SocketTagger.get().untag(fd); } catch (SocketException e) { throw new ErrnoException("socket", EINVAL, e); } }
public IOException rethrowAsIOException() throws IOException { IOException newException = new IOException(getMessage()); newException.initCause(this); throw newException; }
/** * Shutdown the input portion of the socket. */ @Override protected void shutdownInput() throws IOException { shutdownInput = true; try { Libcore.os.shutdown(fd, SHUT_RD); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
@Override public long skip(long byteCount) throws IOException { if (byteCount < 0) { throw new IOException("byteCount < 0: " + byteCount); } try { // Try lseek(2). That returns the new offset, but we'll throw an // exception if it couldn't perform exactly the seek we asked for. Libcore.os.lseek(fd, byteCount, SEEK_CUR); return byteCount; } catch (ErrnoException errnoException) { if (errnoException.errno == ESPIPE) { // You can't seek on a pipe, so fall back to the superclass' implementation. return super.skip(byteCount); } throw errnoException.rethrowAsIOException(); } } }
return true; // Success! throw new ErrnoException("isConnected", connectError); // The connect(2) failed. } catch (ErrnoException errnoException) { if (!fd.valid()) {
public static void bind(FileDescriptor fd, InetAddress address, int port) throws SocketException { if (address instanceof Inet6Address && ((Inet6Address) address).getScopeId() == 0) { // Linux won't let you bind a link-local address without a scope id. Find one. NetworkInterface nif = NetworkInterface.getByInetAddress(address); if (nif == null) { throw new SocketException("Can't bind to a link-local address without a scope id: " + address); } try { address = Inet6Address.getByAddress(address.getHostName(), address.getAddress(), nif.getIndex()); } catch (UnknownHostException ex) { throw new AssertionError(ex); // Can't happen. } } try { Libcore.os.bind(fd, address, port); } catch (ErrnoException errnoException) { throw new BindException(errnoException.getMessage(), errnoException); } }
@Override protected void sendUrgentData(int value) throws IOException { try { byte[] buffer = new byte[] { (byte) value }; Libcore.os.sendto(fd, buffer, 0, 1, MSG_OOB, null, 0); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * Moves this file's file pointer to a new position, from where following * {@code read}, {@code write} or {@code skip} operations are done. The * position may be greater than the current length of the file, but the * file's length will only change if the moving of the pointer is followed * by a {@code write} operation. * * @param offset * the new file pointer position. * @throws IOException * if this file is closed, {@code pos < 0} or another I/O error * occurs. */ public void seek(long offset) throws IOException { if (offset < 0) { throw new IOException("offset < 0: " + offset); } try { Libcore.os.lseek(fd, offset, SEEK_SET); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsIOException(); } }
private FileDescriptor tagSocket(FileDescriptor fd) throws ErrnoException { try { SocketTagger.get().tag(fd); return fd; } catch (SocketException e) { throw new ErrnoException("socket", EINVAL, e); } }
/** * Ensures that data which is buffered within the underlying implementation * is written out to the appropriate device before returning. */ public void sync() throws SyncFailedException { try { if (Libcore.os.isatty(this)) { Libcore.os.tcdrain(this); } else { Libcore.os.fsync(this); } } catch (ErrnoException errnoException) { SyncFailedException sfe = new SyncFailedException(errnoException.getMessage()); sfe.initCause(errnoException); throw sfe; } }
/** * java.net has its own socket options similar to the underlying Unix ones. We paper over the * differences here. */ public static void setSocketOption(FileDescriptor fd, int option, Object value) throws SocketException { try { setSocketOptionErrno(fd, option, value); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }