Package java.nio.channels

Examples of java.nio.channels.FileChannel.lock()


                }

                // get the lock using either try lock or not depending on if we are using timeout or not
                FileLock lock = null;
                try {
                    lock = timeout > 0 ? channel.tryLock() : channel.lock();
                } catch (IllegalStateException ex) {
                    // Also catch the OverlappingFileLockException here. Do nothing here                   
                }
                if (lock != null) {
                    if (LOG.isTraceEnabled()) {
View Full Code Here


    {
        try
        {
            FileChannel channel =
                new RandomAccessFile(lockFile, "rw").getChannel();
            return channel.lock();
        }
        catch (IOException e)
        {
            LOG.warn("Unable to lock the lock file: " + e +
                ". Trying to run without a lock.", e);
View Full Code Here

                        LOGGER.debug("Writing test inputs to temp input file");
                        ByteBuffer buffer = ByteBuffer.allocate(1024*10);
                        FileOutputStream inFileStream =
                                new FileOutputStream(tempIn);
                        FileChannel inChannel = inFileStream.getChannel();
                        inChannel.lock();
                        for (int j = 0; j < testInputs.size(); j++)
                        {
                            buffer.put(testInputs.get(j).getBytes());
                            buffer.put(System.getProperty("line.separator")
                                    .getBytes());
View Full Code Here

                File tempIn = File.createTempFile("rage", ".tmp");
                File tempOut = File.createTempFile("rage", ".tmp");

                FileOutputStream outFile = new FileOutputStream(tempIn);
                FileChannel outChannel = outFile.getChannel();
                outChannel.lock();
                for (int i = 0; i < testCase.getInputs().size(); i++)
                {
                    buffer.put(testCase.getInputs().get(i).getBytes());
                    buffer.put(System.getProperty("line.separator").getBytes());
                    buffer.flip();
View Full Code Here

                   so locking just from the current position would allow reading on systems where
                   locking is mandatory.  Also, Java 6 will throw an exception if the region of the
                   file is already locked by another FileChannel in the same JVM. Hopefully, that will
                   be avoided since every file should have a single file manager - unless two different
                   files strings are configured that somehow map to the same file.*/
                final FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
                try {
                    super.write(bytes, offset, length);
                } finally {
                    lock.release();
                }
View Full Code Here

        f.createNewFile();
        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
        FileChannel fcr = raf.getChannel();

        try {
            fcr.lock(0L, Long.MAX_VALUE, false);
            fail("NonWritableChannelException expected!");
        } catch (NonWritableChannelException e) {}
        raf.close();
    }
View Full Code Here

                }

                // get the lock using either try lock or not depending on if we are using timeout or not
                FileLock lock = null;
                try {
                    lock = timeout > 0 ? channel.tryLock() : channel.lock();
                } catch (IllegalStateException ex) {
                    // Also catch the OverlappingFileLockException here. Do nothing here                   
                }
                if (lock != null) {
                    if (LOG.isTraceEnabled()) {
View Full Code Here

                    }
                }

                // get the lock using either try lock or not depending on if we are using timeout or not
                try {
                    lock = timeout > 0 ? channel.tryLock() : channel.lock();
                } catch (IllegalStateException ex) {
                    // Also catch the OverlappingFileLockException here. Do nothing here                   
                }
                if (lock != null) {
                    LOG.trace("Acquired exclusive read lock: {} to file: {}", lock, target);
View Full Code Here

                   so locking just from the current position would allow reading on systems where
                   locking is mandatory.  Also, Java 6 will throw an exception if the region of the
                   file is already locked by another FileChannel in the same JVM. Hopefully, that will
                   be avoided since every file should have a single file manager - unless two different
                   files strings are configured that somehow map to the same file.*/
                final FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
                try {
                    super.write(bytes, offset, length);
                } finally {
                    lock.release();
                }
View Full Code Here

                }

                // get the lock using either try lock or not depending on if we are using timeout or not
                FileLock lock = null;
                try {
                    lock = timeout > 0 ? channel.tryLock() : channel.lock();
                } catch (IllegalStateException ex) {
                    // Also catch the OverlappingFileLockException here. Do nothing here                   
                }
                if (lock != null) {
                    if (LOG.isTraceEnabled()) {
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.