locktest.java /* * Copyright (c) 2001-2005 Servertec. All Rights Reserved. * * 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; either version 2 of the License, or * (at your option) any later version. * * 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 * * THIS NOTICE MUST NOT BE ALTERED NOR REMOVED. * * CopyrightVersion 1.0 */ import stec.sfc.Win32.RandomAccessFile; import stec.sfc.Win32.FileAccessMode; import stec.sfc.Win32.FileShareMode; import stec.sfc.Win32.FileOperation; import stec.sfc.Win32.FileAttribute; import stec.sfc.Win32.Console; import stec.sfc.Win32.FileSystem; public class locktest { public static void main(String[] args) throws Exception { RandomAccessFile fhandle = new RandomAccessFile("./locktest.dat", FileAccessMode.READ | FileAccessMode.WRITE, FileShareMode.READ | FileShareMode.WRITE, FileOperation.OPEN_ALWAYS, FileAttribute.NORMAL); try { while(true) { System.out.println("file region is not locked, press q to quit or any other key to try to lock it"); while(true) { if(Console.getch() == 'q') { System.out.println("file region is now unlocked, test completed successfully"); return; } if(fhandle.lock(0, 20)) { break; } System.out.println("file region is already locked by another process, press q to quit or any other key to try to lock it"); } System.out.println("file region is now locked, press any key to try to unlock it"); Console.getch(); if(!fhandle.unlock(0, 20)) { throw new Exception("Unable to unload region"); } } } finally { fhandle.close(); } } }