Exemplo n.º 1
0
 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);
     }
 }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
0
 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);
     }
 }