Search in sources :

Example 11 with NonReadableChannelException

use of java.nio.channels.NonReadableChannelException in project jdk8u_jdk by JetBrains.

the class FileChannelImpl method transferTo.

public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
    ensureOpen();
    if (!target.isOpen())
        throw new ClosedChannelException();
    if (!readable)
        throw new NonReadableChannelException();
    if (target instanceof FileChannelImpl && !((FileChannelImpl) target).writable)
        throw new NonWritableChannelException();
    if ((position < 0) || (count < 0))
        throw new IllegalArgumentException();
    long sz = size();
    if (position > sz)
        return 0;
    int icount = (int) Math.min(count, Integer.MAX_VALUE);
    if ((sz - position) < icount)
        icount = (int) (sz - position);
    long n;
    // Attempt a direct transfer, if the kernel supports it
    if ((n = transferToDirectly(position, icount, target)) >= 0)
        return n;
    // Attempt a mapped transfer, but only to trusted channel types
    if ((n = transferToTrustedChannel(position, icount, target)) >= 0)
        return n;
    // Slow path for untrusted targets
    return transferToArbitraryChannel(position, icount, target);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) NonReadableChannelException(java.nio.channels.NonReadableChannelException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 12 with NonReadableChannelException

use of java.nio.channels.NonReadableChannelException in project jdk8u_jdk by JetBrains.

the class FileChannelImpl method transferFromFileChannel.

private long transferFromFileChannel(FileChannelImpl src, long position, long count) throws IOException {
    if (!src.readable)
        throw new NonReadableChannelException();
    synchronized (src.positionLock) {
        long pos = src.position();
        long max = Math.min(count, src.size() - pos);
        long remaining = max;
        long p = pos;
        while (remaining > 0L) {
            long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
            // ## Bug: Closing this channel will not terminate the write
            MappedByteBuffer bb = src.map(MapMode.READ_ONLY, p, size);
            try {
                long n = write(bb, position);
                assert n > 0;
                p += n;
                position += n;
                remaining -= n;
            } catch (IOException ioe) {
                // Only throw exception if no bytes have been written
                if (remaining == max)
                    throw ioe;
                break;
            } finally {
                unmap(bb);
            }
        }
        long nwritten = max - remaining;
        src.position(pos + nwritten);
        return nwritten;
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException)

Example 13 with NonReadableChannelException

use of java.nio.channels.NonReadableChannelException in project robovm by robovm.

the class FileChannelTest method test_readLByteBufferJ_WriteOnly.

public void test_readLByteBufferJ_WriteOnly() throws Exception {
    ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
    try {
        writeOnlyFileChannel.read(readBuffer, 0);
        fail();
    } catch (NonReadableChannelException expected) {
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 14 with NonReadableChannelException

use of java.nio.channels.NonReadableChannelException 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 15 with NonReadableChannelException

use of java.nio.channels.NonReadableChannelException in project robovm by robovm.

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, mapMode);
    return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), mapMode);
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ErrnoException(libcore.io.ErrnoException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Aggregations

NonReadableChannelException (java.nio.channels.NonReadableChannelException)18 NonWritableChannelException (java.nio.channels.NonWritableChannelException)11 ErrnoException (libcore.io.ErrnoException)6 IOException (java.io.IOException)3 MappedByteBuffer (java.nio.MappedByteBuffer)3 FileLock (java.nio.channels.FileLock)3 StructFlock (libcore.io.StructFlock)3 ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 Test (org.junit.Test)2 FileDescriptor (java.io.FileDescriptor)1 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)1 Path (java.nio.file.Path)1