Changeset 2038 in josm


Ignore:
Timestamp:
2009-09-03T15:08:25+02:00 (15 years ago)
Author:
Gubaer
Message:

applied #2322: patch by xeen: JOSM destroys its preferences, when disk is full

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r2017 r2038  
    1313import java.io.OutputStreamWriter;
    1414import java.io.PrintWriter;
     15import java.nio.channels.FileChannel;
    1516import java.util.ArrayList;
    1617import java.util.Arrays;
     
    4445
    4546    /**
    46      * Internal storage for the preferenced directory.
     47     * Internal storage for the preference directory.
    4748     * Do not access this variable directly!
    4849     * @see #getPreferencesDirFile()
     
    291292        try {
    292293            setSystemProperties();
     294            String prefFile = getPreferencesDir() + "preferences";
     295
     296            // Backup old preferences
     297            copyFile(new File(prefFile), new File(prefFile + "_backup"));
     298
    293299            final PrintWriter out = new PrintWriter(new OutputStreamWriter(
    294                     new FileOutputStream(getPreferencesDir() + "preferences"), "utf-8"), false);
     300                    new FileOutputStream(prefFile + "_tmp"), "utf-8"), false);
    295301            for (final Entry<String, String> e : properties.entrySet()) {
    296302                String s = defaults.get(e.getKey());
     
    301307            }
    302308            out.close();
     309
     310            File tmpFile = new File(prefFile + "_tmp");
     311            copyFile(tmpFile, new File(prefFile));
     312            tmpFile.delete();
     313
    303314        } catch (final IOException e) {
    304315            e.printStackTrace();
     
    307318        }
    308319    }
     320
     321    /**
     322     * Simple file copy function that will overwrite the target file
     323     * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA)
     324     * @param in
     325     * @param out
     326     * @throws IOException
     327     */
     328    public static void copyFile(File in, File out) throws IOException  {
     329        FileChannel inChannel = new FileInputStream(in).getChannel();
     330        FileChannel outChannel = new FileOutputStream(out).getChannel();
     331        try {
     332            inChannel.transferTo(0, inChannel.size(),
     333                    outChannel);
     334        }
     335        catch (IOException e) {
     336            throw e;
     337        }
     338        finally {
     339            if (inChannel != null) {
     340                inChannel.close();
     341            }
     342            if (outChannel != null) {
     343                outChannel.close();
     344            }
     345        }
     346    }
     347
    309348
    310349    public void load() throws IOException {
Note: See TracChangeset for help on using the changeset viewer.