Search in sources :

Example 16 with NonWritableChannelException

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

the class FileChannelTest method test_writeLByteBufferJ_ReadOnly.

public void test_writeLByteBufferJ_ReadOnly() throws Exception {
    ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
    try {
        readOnlyFileChannel.write(writeBuffer, 10);
        fail();
    } catch (NonWritableChannelException expected) {
    }
}
Also used : NonWritableChannelException(java.nio.channels.NonWritableChannelException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 17 with NonWritableChannelException

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

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

Example 19 with NonWritableChannelException

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

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

the class FileChannelImpl method truncate.

public FileChannel truncate(long newSize) throws IOException {
    ensureOpen();
    if (newSize < 0)
        throw new IllegalArgumentException("Negative size");
    if (!writable)
        throw new NonWritableChannelException();
    synchronized (positionLock) {
        int rv = -1;
        long p = -1;
        int ti = -1;
        long rp = -1;
        try {
            begin();
            ti = threads.add();
            if (!isOpen())
                return null;
            // get current size
            long size;
            do {
                size = nd.size(fd);
            } while ((size == IOStatus.INTERRUPTED) && isOpen());
            if (!isOpen())
                return null;
            // get current position
            do {
                p = position0(fd, -1);
            } while ((p == IOStatus.INTERRUPTED) && isOpen());
            if (!isOpen())
                return null;
            assert p >= 0;
            // truncate file if given size is less than the current size
            if (newSize < size) {
                do {
                    rv = nd.truncate(fd, newSize);
                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                if (!isOpen())
                    return null;
            }
            // if position is beyond new size then adjust it
            if (p > newSize)
                p = newSize;
            do {
                rp = position0(fd, p);
            } while ((rp == IOStatus.INTERRUPTED) && isOpen());
            return this;
        } finally {
            threads.remove(ti);
            end(rv > -1);
            assert IOStatus.check(rv);
        }
    }
}
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