Search in sources :

Example 36 with ErrnoException

use of libcore.io.ErrnoException in project XobotOS by xamarin.

the class FileChannelImpl method transferTo.

public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
    checkOpen();
    if (!target.isOpen()) {
        throw new ClosedChannelException();
    }
    checkReadable();
    if (target instanceof FileChannelImpl) {
        ((FileChannelImpl) target).checkWritable();
    }
    if (position < 0 || count < 0) {
        throw new IllegalArgumentException("position=" + position + " count=" + count);
    }
    if (count == 0 || position >= size()) {
        return 0;
    }
    count = Math.min(count, size() - position);
    // Try sendfile(2) first...
    boolean completed = false;
    if (target instanceof SocketChannelImpl) {
        FileDescriptor outFd = ((SocketChannelImpl) target).getFD();
        try {
            begin();
            try {
                MutableLong offset = new MutableLong(position);
                long rc = Libcore.os.sendfile(outFd, fd, offset, count);
                completed = true;
                return rc;
            } catch (ErrnoException errnoException) {
                // try a different approach. If it does support it, but it failed, we're done.
                if (errnoException.errno != ENOSYS && errnoException.errno != EINVAL) {
                    throw errnoException.rethrowAsIOException();
                }
            }
        } finally {
            end(completed);
        }
    }
    // ...fall back to write(2).
    ByteBuffer buffer = null;
    try {
        buffer = map(MapMode.READ_ONLY, position, count);
        return target.write(buffer);
    } finally {
        NioUtils.freeDirectBuffer(buffer);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MutableLong(libcore.util.MutableLong) ErrnoException(libcore.io.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Example 37 with ErrnoException

use of libcore.io.ErrnoException in project XobotOS by xamarin.

the class SocketChannelImpl method initLocalAddressAndPort.

private void initLocalAddressAndPort() {
    SocketAddress sa;
    try {
        sa = Libcore.os.getsockname(fd);
    } catch (ErrnoException errnoException) {
        throw new AssertionError(errnoException);
    }
    InetSocketAddress isa = (InetSocketAddress) sa;
    localAddress = isa.getAddress();
    localPort = isa.getPort();
    if (socket != null) {
        socket.socketImpl().initLocalPort(localPort);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 38 with ErrnoException

use of libcore.io.ErrnoException in project robovm by robovm.

the class File method doChmod.

private boolean doChmod(int mask, boolean set) {
    try {
        StructStat sb = Libcore.os.stat(path);
        int newMode = set ? (sb.st_mode | mask) : (sb.st_mode & ~mask);
        Libcore.os.chmod(path, newMode);
        return true;
    } catch (ErrnoException errnoException) {
        return false;
    }
}
Also used : StructStat(libcore.io.StructStat) ErrnoException(libcore.io.ErrnoException)

Example 39 with ErrnoException

use of libcore.io.ErrnoException in project robovm by robovm.

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 40 with ErrnoException

use of libcore.io.ErrnoException in project robovm by robovm.

the class FileChannelImpl method transferTo.

public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
    checkOpen();
    if (!target.isOpen()) {
        throw new ClosedChannelException();
    }
    checkReadable();
    if (target instanceof FileChannelImpl) {
        ((FileChannelImpl) target).checkWritable();
    }
    if (position < 0 || count < 0) {
        throw new IllegalArgumentException("position=" + position + " count=" + count);
    }
    if (count == 0 || position >= size()) {
        return 0;
    }
    count = Math.min(count, size() - position);
    // Try sendfile(2) first...
    boolean completed = false;
    if (target instanceof SocketChannelImpl) {
        FileDescriptor outFd = ((SocketChannelImpl) target).getFD();
        try {
            begin();
            try {
                MutableLong offset = new MutableLong(position);
                long rc = Libcore.os.sendfile(outFd, fd, offset, count);
                completed = true;
                return rc;
            } catch (ErrnoException errnoException) {
                // try a different approach. If it does support it, but it failed, we're done.
                if (errnoException.errno != ENOSYS && errnoException.errno != EINVAL) {
                    throw errnoException.rethrowAsIOException();
                }
            }
        } finally {
            end(completed);
        }
    }
    // ...fall back to write(2).
    ByteBuffer buffer = null;
    try {
        buffer = map(MapMode.READ_ONLY, position, count);
        return target.write(buffer);
    } finally {
        NioUtils.freeDirectBuffer(buffer);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MutableLong(libcore.util.MutableLong) ErrnoException(libcore.io.ErrnoException) FileDescriptor(java.io.FileDescriptor)

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