Search in sources :

Example 1 with MutableLong

use of libcore.util.MutableLong in project j2objc by google.

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 2 with MutableLong

use of libcore.util.MutableLong 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)

Example 3 with MutableLong

use of libcore.util.MutableLong 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)

Aggregations

FileDescriptor (java.io.FileDescriptor)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ErrnoException (libcore.io.ErrnoException)3 MutableLong (libcore.util.MutableLong)3