Java Code Examples for android.system.ErrnoException#getMessage()

The following examples show how to use android.system.ErrnoException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IoBridge.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.io only throws FileNotFoundException when opening files, regardless of what actually
 * went wrong. Additionally, java.io is more restrictive than POSIX when it comes to opening
 * directories: POSIX says read-only is okay, but java.io doesn't even allow that. We also
 * have an Android-specific hack to alter the default permissions.
 */
public static FileDescriptor open(String path, int flags) throws FileNotFoundException {
    FileDescriptor fd = null;
    try {
        // On Android, we don't want default permissions to allow global access.
        int mode = ((flags & O_ACCMODE) == O_RDONLY) ? 0 : 0600;
        fd = Libcore.os.open(path, flags, mode);
        // Posix open(2) fails with EISDIR only if you ask for write permission.
        // Java disallows reading directories too.
        if (isDirectory(path)) {
            throw new ErrnoException("open", EISDIR);
        }
        return fd;
    } catch (ErrnoException errnoException) {
        try {
            if (fd != null) {
                IoUtils.close(fd);
            }
        } catch (IOException ignored) {
        }
        FileNotFoundException ex = new FileNotFoundException(path + ": " + errnoException.getMessage());
        ex.initCause(errnoException);
        throw ex;
    }
}
 
Example 2
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static void bind(FileDescriptor fd, InetAddress address, int port) throws SocketException {
    if (address instanceof Inet6Address) {
        Inet6Address inet6Address = (Inet6Address) address;
        if (inet6Address.getScopeId() == 0 && inet6Address.isLinkLocalAddress()) {
            // 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 {
        NetworkOs.bind(fd, address, port);
    } catch (ErrnoException errnoException) {
        throw new BindException(errnoException.getMessage(), errnoException);
    }
}
 
Example 3
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private static int maybeThrowAfterRecvfrom(boolean isRead, boolean isConnected, ErrnoException errnoException) throws SocketException, SocketTimeoutException {
    if (isRead) {
        if (errnoException.errno == EAGAIN) {
            return 0;
        } else {
            throw new SocketException(errnoException.getMessage(), errnoException);
        }
    } else {
        if (isConnected && errnoException.errno == ECONNREFUSED) {
            throw new PortUnreachableException("", errnoException);
        } else if (errnoException.errno == EAGAIN) {
            throw new SocketTimeoutException(errnoException);
        } else {
            throw new SocketException(errnoException.getMessage(), errnoException);
        }
    }
}
 
Example 4
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static FileDescriptor socket(boolean stream) throws SocketException {
    FileDescriptor fd;
    try {
        fd = Libcore.os.socket(AF_INET6, stream ? SOCK_STREAM : SOCK_DGRAM, 0);

        // The RFC (http://www.ietf.org/rfc/rfc3493.txt) says that IPV6_MULTICAST_HOPS defaults
        // to 1. The Linux kernel (at least up to 2.6.38) accidentally defaults to 64 (which
        // would be correct for the *unicast* hop limit).
        // See http://www.spinics.net/lists/netdev/msg129022.html, though no patch appears to
        // have been applied as a result of that discussion. If that bug is ever fixed, we can
        // remove this code. Until then, we manually set the hop limit on IPv6 datagram sockets.
        // (IPv4 is already correct.)
        if (!stream) {
            NetworkOs.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 1);
        }

        return fd;
    } catch (ErrnoException errnoException) {
        throw new SocketException(errnoException.getMessage(), errnoException);
    }
}
 
Example 5
Source File: ParcelFileDescriptor.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static FileDescriptor openInternal(File file, int mode) throws FileNotFoundException {
    if ((mode & MODE_READ_WRITE) == 0) {
        throw new IllegalArgumentException(
                "Must specify MODE_READ_ONLY, MODE_WRITE_ONLY, or MODE_READ_WRITE");
    }

    int flags = 0;
    switch (mode & MODE_READ_WRITE) {
        case 0:
        case MODE_READ_ONLY: flags = O_RDONLY; break;
        case MODE_WRITE_ONLY: flags = O_WRONLY; break;
        case MODE_READ_WRITE: flags = O_RDWR; break;
    }

    if ((mode & MODE_CREATE) != 0) flags |= O_CREAT;
    if ((mode & MODE_TRUNCATE) != 0) flags |= O_TRUNC;
    if ((mode & MODE_APPEND) != 0) flags |= O_APPEND;

    int realMode = S_IRWXU | S_IRWXG;
    if ((mode & MODE_WORLD_READABLE) != 0) realMode |= S_IROTH;
    if ((mode & MODE_WORLD_WRITEABLE) != 0) realMode |= S_IWOTH;

    final String path = file.getPath();
    try {
        return Os.open(path, flags, realMode);
    } catch (ErrnoException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
 
Example 6
Source File: FileDescriptor.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 7
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 new SocketException(errnoException.getMessage(), errnoException);
    }
}
 
Example 9
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 new SocketException(errnoException.getMessage(), errnoException);
    }
}
 
Example 10
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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 new SocketException(errnoException.getMessage(), errnoException);
}
 
Example 11
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public static InetAddress getSocketLocalAddress(FileDescriptor fd) throws SocketException {
    try {
        SocketAddress sa = NetworkOs.getsockname(fd);
        InetSocketAddress isa = (InetSocketAddress) sa;
        return isa.getAddress();
    } catch (ErrnoException errnoException) {
        throw new SocketException(errnoException.getMessage(), errnoException);
    }
}
 
Example 12
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public static int getSocketLocalPort(FileDescriptor fd) throws SocketException {
    try {
        SocketAddress sa = NetworkOs.getsockname(fd);
        InetSocketAddress isa = (InetSocketAddress) sa;
        return isa.getPort();
    } catch (ErrnoException errnoException) {
        throw new SocketException(errnoException.getMessage(), errnoException);
    }
}