Here you can find the source of closeFile(File file, RandomAccessFile raf)
Parameter | Description |
---|---|
file | The file. |
raf | The RandomAccessFile . |
Parameter | Description |
---|---|
IOException | an exception |
public static void closeFile(File file, RandomAccessFile raf) throws IOException
//package com.java2s; /*// w ww. jav a 2 s.c o m Copyright (C) SYSTAP, LLC 2006-2008. All rights reserved. Contact: SYSTAP, LLC 4501 Tower Road Greensboro, NC 27410 licenses@bigdata.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.RandomAccessFile; import java.util.UUID; public class Main { /** * Since Java does not have platform independent PIDs we use a static * {@link UUID} to identify this process. This {@link UUID} gets written * into all advisory lock files that the process creates. Another process * should check the {@link UUID} in the advisory lock file and refuse to * open the file if the {@link UUID} is not its own {@link UUID}. */ static String pid = UUID.randomUUID().toString(); /** * Close the file and automatically releases the {@link FileLock} (if any) * and removes the advisory lock for that file (if any). * <p> * Note: This method should be used in combination with * {@link FileLockUtility#openFile(File, String, boolean)} in order to ensure that the * optional advisory lock file is deleted when the file is closed. The * purpose of the advisory lock file is to provide advisory locking file * modes (read-only), platforms, or file systems (NFS) that do not support * {@link FileLock}. * * @param file * The file. * @param raf * The {@link RandomAccessFile}. * * @throws IOException */ public static void closeFile(File file, RandomAccessFile raf) throws IOException { if (file == null) throw new IllegalArgumentException(); if (raf == null) throw new IllegalArgumentException(); try { if (raf.getChannel().isOpen()) { /* * close the file iff open. * * Note: a thread that is interrupted during an IO can cause the * file to be closed asynchronously. This is handled by the * disk-based store modes. */ raf.close(); } } finally { /* * Remove the advisory lock (if present) regardles of whether the * file is currently open (see note above). */ removeAdvisoryLock(file); } } /** * Removes the advisory lock for the file if it exists. * * @param file * The file whose <code>.lock</code> file will be removed. * * @throws IOException * if the lock file exists but does not belong to this process * or can not be removed. * * @see #acquireAdvisoryLock(File) */ synchronized public static void removeAdvisoryLock(File file) throws IOException { final File lockFile = new File(file + ".lock"); // no advisory lock file. if (!lockFile.exists()) return; if (!isOurLockFile(lockFile)) { throw new IOException("Not our lock file: " + lockFile.getAbsolutePath()); } if (!lockFile.delete()) { throw new IOException("Could not delete lock file: " + lockFile.getAbsolutePath()); } } static public boolean isOurLockFile(File lockFile) throws IOException { final BufferedReader r = new BufferedReader(new FileReader(lockFile)); try { final String str = r.readLine(); if (pid.equals(str)) return true; return false; } finally { r.close(); } } }