1 19 20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects; 21 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.nio.ByteBuffer ; 26 import java.nio.channels.FileChannel ; 27 import org.openide.util.Exceptions; 28 29 30 33 34 35 public class WriteLockUtils { 36 static final String PREFIX = ".LCK"; 37 static final String SUFFIX = "~"; 38 39 40 private WriteLockUtils(){} 41 42 public static synchronized boolean hasActiveLockFileSigns(final String filePath) { 43 return filePath.indexOf(WriteLockUtils.PREFIX) != -1 && filePath.indexOf(WriteLockUtils.SUFFIX) != -1; 44 } 45 46 public static synchronized boolean isActiveLockFile(final File file) { 47 final String name = file.getName(); 48 boolean isActiveLockFile = hasActiveLockFileSigns(name); 49 if (isActiveLockFile) { 50 final String newName = name.substring(WriteLockUtils.PREFIX.length(), (name.length() - WriteLockUtils.SUFFIX.length())); 51 isActiveLockFile = new File (file.getParentFile(), newName).exists(); 52 } 53 54 return isActiveLockFile; 55 } 56 57 public static File getAssociatedLockFile(File file) { 58 try { 59 file = file.getCanonicalFile(); 60 } catch (IOException iex) { 61 Exceptions.printStackTrace(iex); 62 } 63 64 final File parentFile = file.getParentFile(); 65 final StringBuilder sb = new StringBuilder (); 66 67 sb.append(WriteLockUtils.PREFIX); sb.append(file.getName()); sb.append(WriteLockUtils.SUFFIX); 71 final String lckName = sb.toString(); 72 final File lck = new File (parentFile, lckName); 73 return lck; 74 } 75 76 static String getContentOfLckFile(File lckFile, FileChannel channel) throws IOException { 77 final byte[] readContent = new byte[(int) lckFile.length()]; 78 channel.read(ByteBuffer.wrap(readContent)); 79 80 final String retVal = new String (readContent); 81 return (new File (retVal).exists()) ? retVal : null; 82 } 83 84 static String writeContentOfLckFile(final File lck, FileChannel channel) throws IOException { 85 final String absolutePath = lck.getAbsolutePath(); 86 final ByteBuffer buf = ByteBuffer.wrap(absolutePath.getBytes()); 87 channel.write(buf); 88 return absolutePath; 89 } 90 } 91 | Popular Tags |