Search in sources :

Example 21 with NonWritableChannelException

use of java.nio.channels.NonWritableChannelException 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 22 with NonWritableChannelException

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

the class FileChannelImpl method write.

public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
    if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
        throw new IndexOutOfBoundsException();
    ensureOpen();
    if (!writable)
        throw new NonWritableChannelException();
    synchronized (positionLock) {
        long n = 0;
        int ti = -1;
        try {
            begin();
            ti = threads.add();
            if (!isOpen())
                return 0;
            do {
                n = IOUtil.write(fd, srcs, 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 : NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Aggregations

NonWritableChannelException (java.nio.channels.NonWritableChannelException)22 NonReadableChannelException (java.nio.channels.NonReadableChannelException)11 ErrnoException (libcore.io.ErrnoException)6 FileChannel (java.nio.channels.FileChannel)5 FileLock (java.nio.channels.FileLock)4 MappedByteBuffer (java.nio.MappedByteBuffer)3 StructFlock (libcore.io.StructFlock)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 RandomAccessFile (java.io.RandomAccessFile)2 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.Test)2 FileDescriptor (java.io.FileDescriptor)1 FileInputStream (java.io.FileInputStream)1 BufferUnderflowException (java.nio.BufferUnderflowException)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