Package java.nio.channels

Examples of java.nio.channels.FileLock


   * @throws IOException
   * @see StorageDirectory#lock()
   */
  public boolean isLockSupported(int idx) throws IOException {
    StorageDirectory sd = storageDirs.get(idx);
    FileLock firstLock = null;
    FileLock secondLock = null;
    try {
      firstLock = sd.lock;
      if(firstLock == null) {
        firstLock = sd.tryLock();
        if(firstLock == null)
          return true;
      }
      secondLock = sd.tryLock();
      if(secondLock == null)
        return true;
    } finally {
      if(firstLock != null && firstLock != sd.lock) {
        firstLock.release();
        firstLock.channel().close();
      }
      if(secondLock != null) {
        secondLock.release();
        secondLock.channel().close();
      }
    }
    return false;
  }


    for (String root : rootDirs) {
      File rootFile = new File(root);
      rootFile.mkdirs();
      @SuppressWarnings("resource")
      FileOutputStream lockOutputStream = new FileOutputStream(root + "/.lock");
      FileLock fileLock = lockOutputStream.getChannel().tryLock();
      if (fileLock == null)
        try {
          throw new IOException("Failed to acquire lock file");
        } finally {
          if (lockOutputStream != null)

    public void lock() throws IOException {
      if (isShared()) {
        LOG.info("Locking is disabled");
        return;
      }
      FileLock newLock = tryLock();
      if (newLock == null) {
        String msg = "Cannot lock storage " + this.root
          + ". The directory is already locked";
        LOG.info(msg);
        throw new IOException(msg);

        lockF.deleteOnExit();
        deletionHookAdded = true;
      }
      RandomAccessFile file = new RandomAccessFile(lockF, "rws");
      String jvmName = ManagementFactory.getRuntimeMXBean().getName();
      FileLock res = null;
      try {
        res = file.getChannel().tryLock();
        if (null == res) {
          throw new OverlappingFileLockException();
        }

     *         <code>false</code> otherwise.
     * @throws IOException
     * @see StorageDirectory#lock()
     */
    public boolean isLockSupported() throws IOException {
      FileLock firstLock = null;
      FileLock secondLock = null;
      try {
        firstLock = lock;
        if(firstLock == null) {
          firstLock = tryLock();
          if(firstLock == null)
            return true;
        }
        secondLock = tryLock();
        if(secondLock == null)
          return true;
      } finally {
        if(firstLock != null && firstLock != lock) {
          firstLock.release();
          firstLock.channel().close();
        }
        if(secondLock != null) {
          secondLock.release();
          secondLock.channel().close();
        }
      }
      return false;
    }

        assertFalse(tree.isEmpty());
    }

    // @Test(expected = IOException.class)
    public void testSaveFileException() throws IOException {
        FileLock lock = null;
        RandomAccessFile file = null;
        try {
            String filename = "target/locked.png";
            File f = new File(filename);
            f.createNewFile();
            file = new RandomAccessFile(f, "rw");
            lock = file.getChannel().lock();
            RTree.create().visualize(600, 600).save(filename, "PNG");
        } finally {
            try {
                lock.release();
                file.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

                log.warn("File: " + file.getName() + " is not writable");
                return false;
            }

            FileOutputStream fos = null;
            FileLock lock = null;
            boolean writable;

            try {
                fos = new FileOutputStream(file, true);
                FileChannel channel = fos.getChannel();
                lock = channel.tryLock();
            } catch (IOException e) {
                log.warn("Error while attempting to lock the file: " + file.getName(), e);
                writable = false;
            } finally {
                if (lock != null) {
                    writable = true;
                    try {
                        lock.release();
                    } catch (IOException e) {
                        log.warn("Error while releasing the lock on file: " + file.getName(), e);
                        writable = false;
                    }
                } else {

    @Override
    public boolean writeExifGpsInfo(File jpeg, Wpt wpt, boolean overwrite){
        boolean result = false;
        try{
            FileChannel input = (new RandomAccessFile(jpeg, "rw")).getChannel();
            FileLock lock = input.tryLock();
            if(lock == null){
                throw new IOException("Cannot lock input file.");
            }
            File tmpFile = null;
            try{
                XMPJpeg xmpjpg = new XMPJpeg(input, true);
                ByteBuffer xmp = xmpjpg.getXMP();
                int previousSize =  (xmp == null)
                ? 0
                : xmp.remaining();
                XMPTree xmpEditor = (xmp == null)
                ? new XMPTree()
                : new XMPTree(xmp);
                //System.out.println(new String(xmpEditor.toUTF8Bytes(null)));
                xmpEditor.setXMPProperties(XMPTree.NS_EXIF, asXMPExifProperties(wpt));
                tmpFile = xmpjpg.setXMP(ByteBuffer.wrap(xmpEditor.toXPacket(previousSize)));
            } finally {
                lock.release();
                input.close();
            }
            result = (tmpFile == null) || (jpeg.delete() && tmpFile.renameTo(jpeg));
            if(!result){
                throw new IOException("Cannot overwrite " + jpeg + " with temporary file " + tmpFile);

    @Override
    protected ByteBuffer getXMPBytes(File jpeg) throws IOException, XMPReadException{
        ByteBuffer result = null;
        FileChannel input = (new FileInputStream(jpeg)).getChannel();
        FileLock lock = input.tryLock(0L, Long.MAX_VALUE, true);
        if(lock == null){
            input.close();
            throw new IOException("Cannot lock input file.");
        }
        try{
            result = new XMPJpeg(input, false).getXMP();
        } finally {
            //Do not release lock AFTER closing
            lock.release();
            input.close();
        }
        return result;
    }

      FileChannel destination) throws IOException {
   
    inputNotNull(source,destination);
    if(!source.isOpen()||!destination.isOpen())
      throw new IllegalStateException("Source and destination channels must be open.");   
    FileLock lock = null;
    try {

      lock = destination.lock();
      final long sourceSize = source.size();
      long pos = 0;
      while (pos < sourceSize) {
        // read and flip
        final long remaining = (sourceSize - pos);
        final int mappedZoneSize = remaining >= bufferSize ? bufferSize
            : (int) remaining;
        destination.transferFrom(source, pos, mappedZoneSize);
        // update zone
        pos += mappedZoneSize;

      }
    } finally {
      if (lock != null) {
        try {
          lock.release();
        }catch (Throwable t) {
              if(LOGGER.isLoggable(Level.INFO))
                LOGGER.log(Level.INFO,t.getLocalizedMessage(),t);
        }
      }

TOP

Related Classes of java.nio.channels.FileLock

Copyright © 2018 www.massapicom. 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.