Search in sources :

Example 11 with ErrnoException

use of libcore.io.ErrnoException in project j2objc by google.

the class FileChannelImpl method basicLock.

private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
    int accessMode = (mode & O_ACCMODE);
    if (accessMode == O_RDONLY) {
        if (!shared) {
            throw new NonWritableChannelException();
        }
    } else if (accessMode == O_WRONLY) {
        if (shared) {
            throw new NonReadableChannelException();
        }
    }
    if (position < 0 || size < 0) {
        throw new IllegalArgumentException("position=" + position + " size=" + size);
    }
    FileLock pendingLock = new FileLockImpl(this, position, size, shared);
    addLock(pendingLock);
    StructFlock flock = new StructFlock();
    flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
    flock.l_whence = (short) SEEK_SET;
    flock.l_start = position;
    flock.l_len = translateLockLength(size);
    boolean success = false;
    try {
        success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
    } catch (ErrnoException errnoException) {
        throw errnoException.rethrowAsIOException();
    } finally {
        if (!success) {
            removeLock(pendingLock);
        }
    }
    return success ? pendingLock : null;
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ErrnoException(libcore.io.ErrnoException) StructFlock(libcore.io.StructFlock) FileLock(java.nio.channels.FileLock) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 12 with ErrnoException

use of libcore.io.ErrnoException in project j2objc by google.

the class FileChannelImpl method map.

public final MappedByteBuffer map(MapMode mapMode, long position, long size) throws IOException {
    checkOpen();
    if (mapMode == null) {
        throw new NullPointerException("mapMode == null");
    }
    if (position < 0 || size < 0 || size > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("position=" + position + " size=" + size);
    }
    int accessMode = (mode & O_ACCMODE);
    if (accessMode == O_RDONLY) {
        if (mapMode != MapMode.READ_ONLY) {
            throw new NonWritableChannelException();
        }
    } else if (accessMode == O_WRONLY) {
        throw new NonReadableChannelException();
    }
    if (position + size > size()) {
        // and we only care about making our backing file longer here.
        try {
            Libcore.os.ftruncate(fd, position + size);
        } catch (ErrnoException ftruncateException) {
            // continue on.
            try {
                if (S_ISREG(Libcore.os.fstat(fd).st_mode) || ftruncateException.errno != EINVAL) {
                    throw ftruncateException.rethrowAsIOException();
                }
            } catch (ErrnoException fstatException) {
                throw fstatException.rethrowAsIOException();
            }
        }
    }
    long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
    int offset = (int) (position - alignment);
    MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, ChannelUtils.mapModeToInt(mapMode));
    return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), ChannelUtils.mapModeToInt(mapMode));
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ErrnoException(libcore.io.ErrnoException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 13 with ErrnoException

use of libcore.io.ErrnoException in project j2objc by google.

the class InetAddress method isReachable.

private boolean isReachable(InetAddress destination, InetAddress source, int timeout) throws IOException {
    // TODO: try ICMP first (http://code.google.com/p/android/issues/detail?id=20106)
    FileDescriptor fd = NetworkBridge.socket(true);
    boolean reached = false;
    try {
        if (source != null) {
            NetworkBridge.bind(fd, source, 0);
        }
        NetworkBridge.connect(fd, destination, 7, timeout);
        reached = true;
    } catch (IOException e) {
        if (e.getCause() instanceof ErrnoException) {
            // "Connection refused" means the IP address was reachable.
            reached = (((ErrnoException) e.getCause()).errno == ECONNREFUSED);
        }
    }
    NetworkBridge.closeSocket(fd);
    return reached;
}
Also used : ErrnoException(libcore.io.ErrnoException) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor)

Example 14 with ErrnoException

use of libcore.io.ErrnoException in project j2objc by google.

the class NetworkInterface method collectIpv4Address.

private static void collectIpv4Address(String interfaceName, List<InetAddress> addresses, List<InterfaceAddress> interfaceAddresses) throws SocketException {
    FileDescriptor fd = null;
    try {
        fd = Libcore.os.socket(AF_INET, SOCK_DGRAM, 0);
        InetAddress address = NetworkOs.ioctlInetAddress(fd, SIOCGIFADDR, interfaceName);
        InetAddress broadcast = Inet4Address.ANY;
        try {
            broadcast = NetworkOs.ioctlInetAddress(fd, SIOCGIFBRDADDR, interfaceName);
        } catch (ErrnoException e) {
            if (e.errno != EINVAL) {
                throw e;
            }
        }
        InetAddress netmask = NetworkOs.ioctlInetAddress(fd, SIOCGIFNETMASK, interfaceName);
        if (broadcast.equals(Inet4Address.ANY)) {
            broadcast = null;
        }
        addresses.add(address);
        interfaceAddresses.add(new InterfaceAddress((Inet4Address) address, (Inet4Address) broadcast, (Inet4Address) netmask));
    } catch (ErrnoException errnoException) {
        if (errnoException.errno != EADDRNOTAVAIL && errnoException.errno != EOPNOTSUPP) {
            // Anything else is a real error.
            throw rethrowAsSocketException(errnoException);
        }
    } catch (Exception ex) {
        throw rethrowAsSocketException(ex);
    } finally {
        IoUtils.closeQuietly(fd);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) FileDescriptor(java.io.FileDescriptor) IOException(java.io.IOException) ErrnoException(libcore.io.ErrnoException)

Example 15 with ErrnoException

use of libcore.io.ErrnoException in project j2objc by google.

the class PlainSocketImpl method sendUrgentData.

@Override
protected void sendUrgentData(int value) throws IOException {
    try {
        byte[] buffer = new byte[] { (byte) value };
        NetworkOs.sendto(fd, buffer, 0, 1, MSG_OOB, null, 0);
    } catch (ErrnoException errnoException) {
        throw new SocketException(errnoException.getMessage(), errnoException);
    }
}
Also used : SocketException(java.net.SocketException) ErrnoException(libcore.io.ErrnoException)

Aggregations

ErrnoException (libcore.io.ErrnoException)46 IOException (java.io.IOException)15 FileDescriptor (java.io.FileDescriptor)13 StructStat (libcore.io.StructStat)9 File (java.io.File)7 InetSocketAddress (java.net.InetSocketAddress)7 NonReadableChannelException (java.nio.channels.NonReadableChannelException)6 NonWritableChannelException (java.nio.channels.NonWritableChannelException)6 StructFlock (libcore.io.StructFlock)6 FileOutputStream (java.io.FileOutputStream)5 SocketAddress (java.net.SocketAddress)4 FileInputStream (java.io.FileInputStream)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 FileLock (java.nio.channels.FileLock)3 MutableLong (libcore.util.MutableLong)3 PrintStream (java.io.PrintStream)2 SocketException (java.net.SocketException)2 LinkedList (java.util.LinkedList)2 GaiException (libcore.io.GaiException)2