Search in sources :

Example 1 with FileLockInterruptionException

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

the class FileLockInterruptionExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.FileLockInterruptionException#FileLockInterruptionException()}
     */
public void test_Constructor() {
    FileLockInterruptionException e = new FileLockInterruptionException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : FileLockInterruptionException(java.nio.channels.FileLockInterruptionException)

Example 2 with FileLockInterruptionException

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

the class FileChannelImpl method lock.

public FileLock lock(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);
    boolean completed = false;
    int ti = -1;
    try {
        begin();
        ti = threads.add();
        if (!isOpen())
            return null;
        int n;
        do {
            n = nd.lock(fd, true, position, size, shared);
        } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
        if (isOpen()) {
            if (n == FileDispatcher.RET_EX_LOCK) {
                assert shared;
                FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
                flt.replace(fli, fli2);
                fli = fli2;
            }
            completed = true;
        }
    } finally {
        if (!completed)
            flt.remove(fli);
        threads.remove(ti);
        try {
            end(completed);
        } catch (ClosedByInterruptException e) {
            throw new FileLockInterruptionException();
        }
    }
    return fli;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) NonReadableChannelException(java.nio.channels.NonReadableChannelException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 3 with FileLockInterruptionException

use of java.nio.channels.FileLockInterruptionException in project Bytecoder by mirkosertic.

the class FileChannelImpl method lock.

public FileLock lock(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);
    boolean completed = false;
    int ti = -1;
    try {
        begin();
        ti = threads.add();
        if (!isOpen())
            return null;
        int n;
        do {
            n = nd.lock(fd, true, position, size, shared);
        } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
        if (isOpen()) {
            if (n == FileDispatcher.RET_EX_LOCK) {
                assert shared;
                FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
                flt.replace(fli, fli2);
                fli = fli2;
            }
            completed = true;
        }
    } finally {
        if (!completed)
            flt.remove(fli);
        threads.remove(ti);
        try {
            end(completed);
        } catch (ClosedByInterruptException e) {
            throw new FileLockInterruptionException();
        }
    }
    return fli;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) NonReadableChannelException(java.nio.channels.NonReadableChannelException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 4 with FileLockInterruptionException

use of java.nio.channels.FileLockInterruptionException in project jimfs by google.

the class JimfsFileChannelTest method assertClosedByInterrupt.

/**
 * Asserts that when the given operation is run on an interrupted thread, {@code
 * ClosedByInterruptException} is thrown, the channel is closed and the thread is no longer
 * interrupted.
 */
private static void assertClosedByInterrupt(FileChannelMethod method) throws IOException {
    FileChannel channel = channel(regularFile(10), READ, WRITE);
    Thread.currentThread().interrupt();
    try {
        method.call(channel);
        fail("expected the method to throw ClosedByInterruptException or " + "FileLockInterruptionException");
    } catch (ClosedByInterruptException | FileLockInterruptionException expected) {
        assertFalse("expected the channel to be closed", channel.isOpen());
        assertTrue("expected the thread to still be interrupted", Thread.interrupted());
    } finally {
        // ensure the thread isn't interrupted when this method returns
        Thread.interrupted();
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) FileChannel(java.nio.channels.FileChannel)

Example 5 with FileLockInterruptionException

use of java.nio.channels.FileLockInterruptionException in project Purus-Pasta by puruscor.

the class HashDirCache method lock2.

/* These locks should never have to be waited for long at all, so
     * blocking interruptions until complete should be perfectly
     * okay. */
private static LockedFile lock2(File path) throws IOException {
    boolean intr = false;
    try {
        while (true) {
            try {
                RandomAccessFile fp = null;
                try {
                    fp = new RandomAccessFile(path, "rw");
                    FileLock lk = fp.getChannel().lock();
                    LockedFile ret = new LockedFile(fp, lk);
                    fp = null;
                    return (ret);
                } finally {
                    if (fp != null)
                        fp.close();
                }
            } catch (FileLockInterruptionException e) {
                Thread.currentThread().interrupted();
                intr = true;
            }
        }
    } finally {
        if (intr)
            Thread.currentThread().interrupt();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) FileLock(java.nio.channels.FileLock)

Aggregations

FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)8 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 NonReadableChannelException (java.nio.channels.NonReadableChannelException)3 NonWritableChannelException (java.nio.channels.NonWritableChannelException)3 RandomAccessFile (java.io.RandomAccessFile)1 FileChannel (java.nio.channels.FileChannel)1 FileLock (java.nio.channels.FileLock)1