List of usage examples for java.nio.channels FileChannel lock
public final FileLock lock() throws IOException
From source file:JNLPAppletLauncher.java
private void validateCache(URLConnection conn, File nativeFile, File indexFile) throws IOException { // Lock the cache directory final String lckFileName = "cache.lck"; File lckFile = new File(cacheDir, lckFileName); lckFile.createNewFile();//from w w w . j a va 2 s. c o m final FileOutputStream lckOut = new FileOutputStream(lckFile); final FileChannel lckChannel = lckOut.getChannel(); final FileLock lckLock = lckChannel.lock(); try { // Check to see whether the cached jar file exists and is valid boolean valid = false; long cachedTimeStamp = readTimeStamp(indexFile); long urlTimeStamp = conn.getLastModified(); if (nativeFile.exists() && urlTimeStamp > 0 && urlTimeStamp == readTimeStamp(indexFile)) { valid = true; } // Validate the cache, download the jar if needed if (!valid) { if (VERBOSE) { System.err.println("processNativeJar: downloading " + nativeFile.getAbsolutePath()); } indexFile.delete(); nativeFile.delete(); // Copy from URL to File int len = conn.getContentLength(); if (VERBOSE) { System.err.println("Content length = " + len + " bytes"); } int totalNumBytes = copyURLToFile(conn, nativeFile); if (DEBUG) { System.err.println("processNativeJar: " + conn.getURL().toString() + " --> " + nativeFile.getAbsolutePath() + " : " + totalNumBytes + " bytes written"); } // Write timestamp to index file. writeTimeStamp(indexFile, urlTimeStamp); } else { if (DEBUG) { System.err .println("processNativeJar: using previously cached: " + nativeFile.getAbsolutePath()); } } } finally { // Unlock the cache directory lckLock.release(); } }
From source file:com.MainFiles.Functions.java
public int createStan() { int x = 0;/*from w w w. j a va 2s.c om*/ String filename = COUNT_FILE; File inwrite = new File(filename); // Get a file channel for the file try { FileChannel channel = new RandomAccessFile(inwrite, "rw").getChannel(); // Use the file channel to create a lock on the file. // This method blocks until it can retrieve the lock. FileLock lock = channel.lock(); // if(!inwrite.exists()) { String s = ""; try { int fileSize = (int) channel.size(); // System.out.println("int is" + fileSize); ByteBuffer bafa = ByteBuffer.allocate(fileSize); int numRead = 0; while (numRead >= 0) { numRead = channel.read(bafa); bafa.rewind(); for (int i = 0; i < numRead; i++) { int b = (int) bafa.get(); char c = (char) b; s = s + c; } } x = Integer.parseInt(s); if (x > 999999) { x = 100000; } else if (x < 100000) { x = 100000; } x = ++x; String xx = String.valueOf(x); byte[] yy = xx.getBytes(); channel.truncate(0); channel.write(ByteBuffer.wrap(yy)); // channel.close(); } catch (IOException e) { e.printStackTrace(); } lock.release(); // Close the file channel.close(); } catch (FileNotFoundException e) { String message = "The file " + inwrite.getName() + " does not exist. So no input can be written on it"; System.out.println(message); e.printStackTrace(); //log to error file } catch (IOException e) { System.out.println("Problem writing to the logfile " + inwrite.getName()); } filename = ""; return x; }