/** * 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(); } }
/** * 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 void socketpair(int domain, int type, int protocol, FileDescriptor fd1, FileDescriptor fd2) throws ErrnoException { os.socketpair(domain, type, protocol, fd1, fd2); tagSocket(fd1); tagSocket(fd2); }
/** * Use this to mmap the whole file read-only. */ public static MemoryMappedFile mmapRO(String path) throws ErrnoException { FileDescriptor fd = Libcore.os.open(path, O_RDONLY, 0); long size = Libcore.os.fstat(fd).st_size; long address = Libcore.os.mmap(0L, size, PROT_READ, MAP_SHARED, fd, 0); Libcore.os.close(fd); return new MemoryMappedFile(address, size); }
/** * Converts the stashed function name and errno value to a human-readable string. * We do this here rather than in the constructor so that callers only pay for * this if they need it. */ @Override public String getMessage() { String errnoName = OsConstants.errnoName(errno); if (errnoName == null) { errnoName = "errno " + errno; } String description = Libcore.os.strerror(errno); return functionName + " failed: " + errnoName + " (" + description + ")"; }
/** * 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(); } }
@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(); } }
/** * Converts the stashed function name and error value to a human-readable string. * We do this here rather than in the constructor so that callers only pay for * this if they need it. */ @Override public String getMessage() { String gaiName = OsConstants.gaiName(error); if (gaiName == null) { gaiName = "GAI_ error " + error; } String description = Libcore.os.gai_strerror(error); return functionName + " failed: " + gaiName + " (" + description + ")"; }
private static boolean isLingerSocket(FileDescriptor fd) throws ErrnoException { StructLinger linger = Libcore.os.getsockoptLinger(fd, SOL_SOCKET, SO_LINGER); return linger.isOn() && linger.l_linger > 0; }
public static void addText(String tag, String data) { getReporter().addText(tag, data); } }
public static void writeEvent(int code, Object... list) { getReporter().report(code, list); } }
public static int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws IOException { boolean isDatagram = (inetAddress != null); if (!isDatagram && byteCount <= 0) { return 0; } int result; try { result = Libcore.os.sendto(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); } catch (ErrnoException errnoException) { result = maybeThrowAfterSendto(isDatagram, errnoException); } return result; }
/** * Indicates if this file represents a <em>file</em> on the underlying * file system. * * @return {@code true} if this file is a file, {@code false} otherwise. */ public boolean isFile() { try { return S_ISREG(Libcore.os.stat(path).st_mode); } catch (ErrnoException errnoException) { // The RI returns false on error. (Even for errors like EACCES or ELOOP.) return false; } }
/** * Returns a new iterator over {@code buffer}, starting at {@code offset} and continuing for * {@code byteCount} bytes. Items larger than a byte are interpreted using the given byte order. */ public static BufferIterator iterator(byte[] buffer, int offset, int byteCount, ByteOrder order) { return new HeapBufferIterator(buffer, offset, byteCount, order); } }
/** * Returns the request's port number. */ public int getPort() { return Memory.peekShort(buffer, INDEX_PORT, ByteOrder.BIG_ENDIAN); }
/** * Returns the number of processor cores available to the VM, at least 1. * Traditionally this returned the number currently online, * but many mobile devices are able to take unused cores offline to * save power, so releases newer than Android 4.2 (Jelly Bean) return the maximum number of * cores that could be made available if there were no power or heat * constraints. */ public int availableProcessors() { return (int) Libcore.os.sysconf(_SC_NPROCESSORS_CONF); }
/** * Shutdown the output portion of the socket. */ @Override protected void shutdownOutput() throws IOException { try { Libcore.os.shutdown(fd, SHUT_WR); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }