static void flushFilePrefsImpl(java.io.File file, java.util.Properties prefs) { //throws IOException { java.io.BufferedWriter outJ = null; java.nio.channels.FileLock lockJ = null; try { java.io.FileOutputStream ostream = new java.io.FileOutputStream(file); outJ = new java.io.BufferedWriter(new java.io.OutputStreamWriter(ostream, "UTF-8")); //$NON-NLS-1$ java.nio.channels.FileChannel channel = ostream.getChannel(); lockJ = channel.lockJ(); outJ.write(HEADER); outJ.newLine(); outJ.write(FILE_PREFS); outJ.newLine(); if (prefs.size() == 0) { exportEntries(EMPTY_SARRAY, EMPTY_SARRAY, outJ); } else { String[] keys = prefs.keySet() .toArray(new String[prefs.size()]); int length = keys.Length; String[] values = new String[length]; for (int i = 0; i < length; i++) { values [i] = prefs.getProperty(keys [i]); } exportEntries(keys, values, outJ); } outJ.flush(); } finally { releaseQuietly(lockJ); closeQuietly(outJ); } }
private static void releaseQuietly(java.nio.channels.FileLock lockJ) { if (lockJ == null) { return; } try { lockJ.release(); } catch (java.io.IOException e) { } }
static java.util.Properties loadFilePrefsImpl(java.io.File file) { Properties result = new Properties(); if (!file.exists()) { file.getParentFile().mkdirs(); return(result); } if (file.canRead()) { java.io.InputStream inJ = null; java.nio.channels.FileLock lockJ = null; try { java.io.FileInputStream istream = new java.io.FileInputStream(file); inJ = new java.io.BufferedInputStream(istream); java.nio.channels.FileChannel channel = istream.getChannel(); lockJ = channel.lockJ(0L, java.lang.Long.MAX_VALUE, true); org.w3c.dom.Document doc = builder.parse(inJ); org.w3c.dom.NodeList entries = org.apache.xpath.XPathAPI.selectNodeList(doc .getDocumentElement(), "entry"); //$NON-NLS-1$ int length = entries.getLength(); for (int i = 0; i < length; i++) { org.w3c.dom.Element node = (org.w3c.dom.Element)entries.item(i); String key = node.getAttribute("key"); //$NON-NLS-1$ String value = node.getAttribute("value"); //$NON-NLS-1$ result.setProperty(key, value); } return(result); } catch (java.io.IOException e) { } catch (org.xml.sax.SAXException e) { } catch (javax.xml.transform.TransformerException e) { // transform shouldn't fail for xpath call throw new java.lang.AssertionError(e); } finally { releaseQuietly(lockJ); closeQuietly(inJ); } } else { file.delete(); } return(result); }
private void initOutputFiles()//throws FileNotFoundException, IOException { { while (true) { // try to find a unique file which is not locked by other process uniqueID++; // FIXME: improve performance here for (int generation = 0; generation < count; generation++) { // cache all file names for rotation use files[generation] = new java.io.File(parseFileName(generation)); } fileName = files[0].getAbsolutePath(); lock (allLocks) { /* * if current process has held lock for this fileName continue * to find next file */ if (null != allLocks.get(fileName)) { continue; } if (files[0].exists() && (!append || files[0].length() >= limit)) { for (int i = count - 1; i > 0; i--) { if (files[i].exists()) { files[i].delete(); } files[i - 1].renameTo(files[i]); } } java.io.FileOutputStream fileStream = new java.io.FileOutputStream(fileName + LCK_EXT); java.nio.channels.FileChannel channel = fileStream.getChannel(); /* * if lock is unsupported and IOException thrown, just let the * IOException throws out and exit otherwise it will go into an * undead cycle */ lockJ = channel.tryLock(); if (null == lockJ) { try { fileStream.close(); } catch (Exception e) { // ignore } continue; } allLocks.put(fileName, lockJ); break; } } output = new MeasureOutputStream(new java.io.BufferedOutputStream( new java.io.FileOutputStream(fileName, append)), files[0].length()); setOutputStream(output); }
private void initOutputFiles() { //throws FileNotFoundException, IOException { while (true) { // try to find a unique file which is not locked by other process uniqueID++; // FIXME: improve performance here for (int generation = 0; generation < count; generation++) { // cache all file names for rotation use files[generation] = new java.io.File(parseFileName(generation)); } fileName = files[0].getAbsolutePath(); lock (allLocks) { /* * if current process has held lock for this fileName continue * to find next file */ if (null != allLocks.get(fileName)) { continue; } if (files[0].exists() && (!append || files[0].length() >= limit)) { for (int i = count - 1; i > 0; i--) { if (files[i].exists()) { files[i].delete(); } files[i - 1].renameTo(files[i]); } } java.io.FileOutputStream fileStream = new java.io.FileOutputStream(fileName + LCK_EXT); java.nio.channels.FileChannel channel = fileStream.getChannel(); /* * if lock is unsupported and IOException thrown, just let the * IOException throws out and exit otherwise it will go into an * undead cycle */ lockJ = channel.tryLock(); if (null == lockJ) { try { fileStream.close(); } catch (Exception e) { // ignore } continue; } allLocks.put(fileName, lockJ); break; } } output = new MeasureOutputStream(new java.io.BufferedOutputStream( new java.io.FileOutputStream(fileName, append)), files[0].length()); setOutputStream(output); }