Package java.nio.channels

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


    private synchronized void writeToFile(String action) {
        Date date = new Date();
        try {
            FileOutputStream out = new FileOutputStream(logFile, true);
            FileChannel channel = out.getChannel();
            FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
            PrintWriter writer = new PrintWriter(out, false);
            writer.println(DATE_FORMAT.format(date)+" - "+action+" - "+username);
            writer.flush();
            writer.close();
            if(lock.isValid()) {
View Full Code Here


            if (isMatched(file)) {
                if (isAttemptFileLock()) {
                    FileChannel fc = null;
                    try {
                        fc = new RandomAccessFile(file, "rw").getChannel();
                        fc.lock();
                        result = true;
                    }
                    catch (Throwable e) {
                        log.debug("Failed to get the lock on file: " + file, e);
                    }
View Full Code Here

    if (fileChannel == null) {
      return;
    }
    FileLock fileLock = null;
    try {
      fileLock = fileChannel.lock();
      long position = fileChannel.position();
      long size = fileChannel.size();
      if (size != position) {
        fileChannel.position(size);
      }
View Full Code Here

            try
            {
                Properties props = new Properties();

                channel = new RandomAccessFile( touchfile, "rw" ).getChannel();
                lock = channel.lock( 0, channel.size(), false );

                if ( touchfile.canRead() )
                {
                    getLogger().debug( "Reading resolution-state from: " + touchfile );
                    ByteBuffer buffer = ByteBuffer.allocate( (int) channel.size() );
View Full Code Here

            {
                Properties props = new Properties();

                stream = new FileInputStream( touchfile );
                channel = stream.getChannel();
                lock = channel.lock( 0, channel.size(), true );

                getLogger().debug( "Reading resolution-state from: " + touchfile );
                props.load( stream );

                return props;
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

    /* acquire exclusive write write lock for this directory */
    void acquireWritable() throws IOException {
        synchronized (openFileWrites) {
            if (writeMutexFile == null) {
                FileChannel tmp = FileChannel.open(streamDirectory.resolve("mff.lock"), READ, WRITE, CREATE);
                writeMutexLock = tmp.lock();
                if (writeMutexLock == null || writeMutexLock.isShared()) {
                    throw new IOException("unable to acquire exclusive writeLock for directory " + streamDirectory);
                }
                writeMutexFile = tmp;
                publishEvent(MuxyFileEvent.WRITE_LOCK_ACQUIRED, writeMutexLock);
View Full Code Here

    private synchronized void writeToFile(String action) {
        Date date = new Date();
        try {
            FileOutputStream out = new FileOutputStream(logFile, true);
            FileChannel channel = out.getChannel();
            FileLock lock = channel.lock(0, Long.MAX_VALUE, false);
            PrintWriter writer = new PrintWriter(out, false);
            writer.println(DATE_FORMAT.format(date)+" - "+action+" - "+username);
            writer.flush();
            writer.close();
            if(lock.isValid()) {
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.*/
                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

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.