Search in sources :

Example 16 with NonReadableChannelException

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

the class FileChannelImpl method tryLock.

public FileLock tryLock(long position, long size, boolean shared) throws IOException {
    ensureOpen();
    if (shared && !readable)
        throw new NonReadableChannelException();
    if (!shared && !writable)
        throw new NonWritableChannelException();
    FileLockImpl fli = new FileLockImpl(this, position, size, shared);
    FileLockTable flt = fileLockTable();
    flt.add(fli);
    int result;
    int ti = threads.add();
    try {
        try {
            ensureOpen();
            result = nd.lock(fd, false, position, size, shared);
        } catch (IOException e) {
            flt.remove(fli);
            throw e;
        }
        if (result == FileDispatcher.NO_LOCK) {
            flt.remove(fli);
            return null;
        }
        if (result == FileDispatcher.RET_EX_LOCK) {
            assert shared;
            FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
            flt.replace(fli, fli2);
            return fli2;
        }
        return fli;
    } finally {
        threads.remove(ti);
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) IOException(java.io.IOException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 17 with NonReadableChannelException

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

the class FileChannelImpl method map.

public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException {
    ensureOpen();
    if (mode == null)
        throw new NullPointerException("Mode is null");
    if (position < 0L)
        throw new IllegalArgumentException("Negative position");
    if (size < 0L)
        throw new IllegalArgumentException("Negative size");
    if (position + size < 0)
        throw new IllegalArgumentException("Position + size overflow");
    if (size > Integer.MAX_VALUE)
        throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
    int imode = -1;
    if (mode == MapMode.READ_ONLY)
        imode = MAP_RO;
    else if (mode == MapMode.READ_WRITE)
        imode = MAP_RW;
    else if (mode == MapMode.PRIVATE)
        imode = MAP_PV;
    assert (imode >= 0);
    if ((mode != MapMode.READ_ONLY) && !writable)
        throw new NonWritableChannelException();
    if (!readable)
        throw new NonReadableChannelException();
    long addr = -1;
    int ti = -1;
    try {
        begin();
        ti = threads.add();
        if (!isOpen())
            return null;
        long filesize;
        do {
            filesize = nd.size(fd);
        } while ((filesize == IOStatus.INTERRUPTED) && isOpen());
        if (!isOpen())
            return null;
        if (filesize < position + size) {
            // Extend file size
            if (!writable) {
                throw new IOException("Channel not open for writing " + "- cannot extend file to required size");
            }
            int rv;
            do {
                rv = nd.truncate(fd, position + size);
            } while ((rv == IOStatus.INTERRUPTED) && isOpen());
            if (!isOpen())
                return null;
        }
        if (size == 0) {
            addr = 0;
            // a valid file descriptor is not required
            FileDescriptor dummy = new FileDescriptor();
            if ((!writable) || (imode == MAP_RO))
                return Util.newMappedByteBufferR(0, 0, dummy, null);
            else
                return Util.newMappedByteBuffer(0, 0, dummy, null);
        }
        int pagePosition = (int) (position % allocationGranularity);
        long mapPosition = position - pagePosition;
        long mapSize = size + pagePosition;
        try {
            // If no exception was thrown from map0, the address is valid
            addr = map0(imode, mapPosition, mapSize);
        } catch (OutOfMemoryError x) {
            // An OutOfMemoryError may indicate that we've exhausted memory
            // so force gc and re-attempt map
            System.gc();
            try {
                Thread.sleep(100);
            } catch (InterruptedException y) {
                Thread.currentThread().interrupt();
            }
            try {
                addr = map0(imode, mapPosition, mapSize);
            } catch (OutOfMemoryError y) {
                // After a second OOME, fail
                throw new IOException("Map failed", y);
            }
        }
        // On Windows, and potentially other platforms, we need an open
        // file descriptor for some mapping operations.
        FileDescriptor mfd;
        try {
            mfd = nd.duplicateForMapping(fd);
        } catch (IOException ioe) {
            unmap0(addr, mapSize);
            throw ioe;
        }
        assert (IOStatus.checkAll(addr));
        assert (addr % allocationGranularity == 0);
        int isize = (int) size;
        Unmapper um = new Unmapper(addr, mapSize, isize, mfd);
        if ((!writable) || (imode == MAP_RO)) {
            return Util.newMappedByteBufferR(isize, addr + pagePosition, mfd, um);
        } else {
            return Util.newMappedByteBuffer(isize, addr + pagePosition, mfd, um);
        }
    } finally {
        threads.remove(ti);
        end(IOStatus.checkAll(addr));
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) IOException(java.io.IOException) NonWritableChannelException(java.nio.channels.NonWritableChannelException) FileDescriptor(java.io.FileDescriptor)

Example 18 with NonReadableChannelException

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

the class FileChannelImpl method read.

public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
    if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
        throw new IndexOutOfBoundsException();
    ensureOpen();
    if (!readable)
        throw new NonReadableChannelException();
    synchronized (positionLock) {
        long n = 0;
        int ti = -1;
        try {
            begin();
            ti = threads.add();
            if (!isOpen())
                return 0;
            do {
                n = IOUtil.read(fd, dsts, offset, length, nd);
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
            return IOStatus.normalize(n);
        } finally {
            threads.remove(ti);
            end(n > 0);
            assert IOStatus.check(n);
        }
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException)

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