Ignore:
Timestamp:
2013-04-16T19:57:43+02:00 (11 years ago)
Author:
Don-vip
Message:

see #8570, #7406 - I/O refactorization:

  • Move different file copy functions to Utils.copyFile (to be replaced later by Files.copy when switching to Java 7)
  • Replace all Utils.close(XXX) by Utils.close(Closeable) -> impacted plugins: commandline, mirrored_download, opendata, piclayer
  • Add new Utils.close(ZipFile)
  • Use of Utils.close almost everywhere
  • Javadoc fixes
Location:
trunk/src/org/openstreetmap/josm/data
Files:
8 edited

Legend:

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

    r5566 r5874  
    3838import org.openstreetmap.josm.io.OsmExporter;
    3939import org.openstreetmap.josm.io.OsmImporter;
     40import org.openstreetmap.josm.tools.Utils;
    4041
    4142/**
     
    156157                        PrintStream ps = new PrintStream(pidFile);
    157158                        ps.println(ManagementFactory.getRuntimeMXBean().getName());
    158                         ps.close();
     159                        Utils.close(ps);
    159160                    } catch (Throwable t) {
    160161                        System.err.println(t.getMessage());
     
    299300                            System.err.println(t.getClass()+":"+t.getMessage());
    300301                        } finally {
    301                             reader.close();
     302                            Utils.close(reader);
    302303                        }
    303304                    } catch (Throwable t) {
  • trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java

    r5590 r5874  
    11package org.openstreetmap.josm.data;
    22
    3 import javax.script.ScriptException;
    4 import org.openstreetmap.josm.Main;
    5 import org.openstreetmap.josm.data.Preferences.Setting;
    63import static org.openstreetmap.josm.tools.I18n.tr;
    74
     
    129import java.io.File;
    1310import java.io.FileInputStream;
    14 import java.io.IOException;
    1511import java.io.InputStream;
    16 
    1712import java.util.ArrayList;
    18 import java.util.Arrays;
    1913import java.util.Collection;
    2014import java.util.Collections;
     
    3024import java.util.regex.Matcher;
    3125import java.util.regex.Pattern;
     26
    3227import javax.script.ScriptEngine;
    3328import javax.script.ScriptEngineManager;
     29import javax.script.ScriptException;
    3430import javax.swing.JOptionPane;
    3531import javax.swing.SwingUtilities;
     
    4238import javax.xml.transform.stream.StreamResult;
    4339
     40import org.openstreetmap.josm.Main;
     41import org.openstreetmap.josm.data.Preferences.Setting;
    4442import org.openstreetmap.josm.gui.io.DownloadFileTask;
    4543import org.openstreetmap.josm.plugins.PluginDownloadTask;
     
    4745import org.openstreetmap.josm.plugins.ReadLocalPluginInformationTask;
    4846import org.openstreetmap.josm.tools.LanguageInfo;
     47import org.openstreetmap.josm.tools.Utils;
    4948import org.w3c.dom.Document;
    5049import org.w3c.dom.Element;
     
    441440                log("Error reading custom preferences: "+ex.getMessage());
    442441            } finally {
    443                 try {
    444                     if (is != null) {
    445                         is.close();
    446                     }
    447                 } catch (IOException ex) {         }
     442                Utils.close(is);
    448443            }
    449444            log("-- Reading complete --");
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r5871 r5874  
    1818import java.lang.annotation.RetentionPolicy;
    1919import java.lang.reflect.Field;
    20 import java.nio.channels.FileChannel;
    2120import java.util.ArrayList;
    2221import java.util.Arrays;
     
    137136    }
    138137
     138    /**
     139     * Base abstract class of all settings, holding the setting value.
     140     *
     141     * @param <T> The setting type
     142     */
    139143    abstract public static class AbstractSetting<T> implements Setting<T> {
    140         private T value;
     144        private final T value;
     145        /**
     146         * Constructs a new {@code AbstractSetting} with the given value
     147         * @param value The setting value
     148         */
    141149        public AbstractSetting(T value) {
    142150            this.value = value;
    143151        }
    144         @Override
    145         public T getValue() {
     152        @Override public T getValue() {
    146153            return value;
    147154        }
    148         @Override
    149         public String toString() {
     155        @Override public String toString() {
    150156            return value != null ? value.toString() : "null";
    151157        }
    152158    }
    153159
     160    /**
     161     * Setting containing a {@link String} value.
     162     */
    154163    public static class StringSetting extends AbstractSetting<String> {
     164        /**
     165         * Constructs a new {@code StringSetting} with the given value
     166         * @param value The setting value
     167         */
    155168        public StringSetting(String value) {
    156169            super(value);
    157170        }
    158         public void visit(SettingVisitor visitor) {
     171        @Override public void visit(SettingVisitor visitor) {
    159172            visitor.visit(this);
    160173        }
    161         public StringSetting getNullInstance() {
     174        @Override public StringSetting getNullInstance() {
    162175            return new StringSetting(null);
    163176        }
    164177    }
    165178
     179    /**
     180     * Setting containing a {@link List} of {@link String} values.
     181     */
    166182    public static class ListSetting extends AbstractSetting<List<String>> {
     183        /**
     184         * Constructs a new {@code ListSetting} with the given value
     185         * @param value The setting value
     186         */
    167187        public ListSetting(List<String> value) {
    168188            super(value);
    169189        }
    170         public void visit(SettingVisitor visitor) {
     190        @Override public void visit(SettingVisitor visitor) {
    171191            visitor.visit(this);
    172192        }
    173         public ListSetting getNullInstance() {
     193        @Override public ListSetting getNullInstance() {
    174194            return new ListSetting(null);
    175195        }
    176196    }
    177197
     198    /**
     199     * Setting containing a {@link List} of {@code List}s of {@link String} values.
     200     */
    178201    public static class ListListSetting extends AbstractSetting<List<List<String>>> {
     202        /**
     203         * Constructs a new {@code ListListSetting} with the given value
     204         * @param value The setting value
     205         */
    179206        public ListListSetting(List<List<String>> value) {
    180207            super(value);
    181208        }
    182         public void visit(SettingVisitor visitor) {
     209        @Override public void visit(SettingVisitor visitor) {
    183210            visitor.visit(this);
    184211        }
    185         public ListListSetting getNullInstance() {
     212        @Override public ListListSetting getNullInstance() {
    186213            return new ListListSetting(null);
    187214        }
    188215    }
    189216
     217    /**
     218     * Setting containing a {@link List} of {@link Map}s of {@link String} values.
     219     */
    190220    public static class MapListSetting extends AbstractSetting<List<Map<String, String>>> {
     221        /**
     222         * Constructs a new {@code MapListSetting} with the given value
     223         * @param value The setting value
     224         */
    191225        public MapListSetting(List<Map<String, String>> value) {
    192226            super(value);
    193227        }
    194         public void visit(SettingVisitor visitor) {
     228        @Override public void visit(SettingVisitor visitor) {
    195229            visitor.visit(this);
    196230        }
    197         public MapListSetting getNullInstance() {
     231        @Override public MapListSetting getNullInstance() {
    198232            return new MapListSetting(null);
    199233        }
     
    265299
    266300    /**
    267      * Return the location of the user defined preferences file
     301     * Returns the location of the user defined preferences directory
     302     * @return The location of the user defined preferences directory
    268303     */
    269304    public String getPreferencesDir() {
     
    274309    }
    275310
     311    /**
     312     * Returns the user defined preferences directory
     313     * @return The user defined preferences directory
     314     */
    276315    public File getPreferencesDirFile() {
    277316        if (preferencesDirFile != null)
     
    292331    }
    293332
     333    /**
     334     * Returns the user preferences file
     335     * @return The user preferences file
     336     */
    294337    public File getPreferenceFile() {
    295338        return new File(getPreferencesDirFile(), "preferences.xml");
    296339    }
    297340
     341    /**
     342     * Returns the user plugin directory
     343     * @return The user plugin directory
     344     */
    298345    public File getPluginsDirectory() {
    299346        return new File(getPreferencesDirFile(), "plugins");
     
    551598
    552599        // Backup old preferences if there are old preferences
    553         if(prefFile.exists()) {
    554             copyFile(prefFile, backupFile);
     600        if (prefFile.exists()) {
     601            Utils.copyFile(prefFile, backupFile);
    555602        }
    556603
     
    558605                new FileOutputStream(prefFile + "_tmp"), "utf-8"), false);
    559606        out.print(toXML(false));
    560         out.close();
     607        Utils.close(out);
    561608
    562609        File tmpFile = new File(prefFile + "_tmp");
    563         copyFile(tmpFile, prefFile);
     610        Utils.copyFile(tmpFile, prefFile);
    564611        tmpFile.delete();
    565612
     
    575622        file.setReadable(true, true);
    576623        file.setWritable(true, true);
    577     }
    578 
    579     /**
    580      * Simple file copy function that will overwrite the target file
    581      * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA)
    582      * @param in
    583      * @param out
    584      * @throws IOException
    585      */
    586     public static void copyFile(File in, File out) throws IOException  {
    587         FileChannel inChannel = new FileInputStream(in).getChannel();
    588         FileChannel outChannel = new FileOutputStream(out).getChannel();
    589         try {
    590             inChannel.transferTo(0, inChannel.size(),
    591                     outChannel);
    592         }
    593         catch (IOException e) {
    594             throw e;
    595         }
    596         finally {
    597             if (inChannel != null) {
    598                 inChannel.close();
    599             }
    600             if (outChannel != null) {
    601                 outChannel.close();
    602             }
    603         }
    604624    }
    605625
     
    615635                fromXML(in);
    616636            } finally {
    617                 in.close();
     637                Utils.close(in);
    618638            }
    619639        }
  • trunk/src/org/openstreetmap/josm/data/ServerSidePreferences.java

    r4874 r5874  
    2222import org.openstreetmap.josm.io.OsmConnection;
    2323import org.openstreetmap.josm.tools.Base64;
     24import org.openstreetmap.josm.tools.Utils;
    2425
    2526/**
     
    9192                PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream()));
    9293                out.println(s);
    93                 out.close();
    94                 con.getInputStream().close();
     94                Utils.close(out);
     95                Utils.close(con.getInputStream());
    9596                con.disconnect();
    9697                JOptionPane.showMessageDialog(
  • trunk/src/org/openstreetmap/josm/data/Version.java

    r5868 r5874  
    4444                }
    4545            } finally {
    46                 in.close();
     46                Utils.close(in);
    4747            }
    4848            s = sb.toString();
  • trunk/src/org/openstreetmap/josm/data/imagery/WmsCache.java

    r5763 r5874  
    156156            }
    157157        } finally {
    158             try {
    159                 if (fis != null) {
    160                     fis.close();
    161                 }
    162                 if (fos != null) {
    163                     fos.close();
    164                 }
    165             } catch (IOException e) {
    166                 e.printStackTrace();
    167             }
     158            Utils.close(fos);
     159            Utils.close(fis);
    168160        }
    169161
     
    532524                totalFileSize += Utils.copyStream(imageData, os);
    533525            } finally {
    534                 os.close();
     526                Utils.close(os);
    535527            }
    536528        }
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFile.java

    r5073 r5874  
    2525import java.util.ArrayList;
    2626import java.util.HashMap;
     27
     28import org.openstreetmap.josm.tools.Utils;
    2729
    2830/**
     
    151153        lastSubGrid = topLevelSubGrid[0];
    152154
    153         in.close();
     155        Utils.close(in);
    154156    }
    155157
  • trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java

    r5644 r5874  
    5757import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
    5858import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
     59import org.openstreetmap.josm.tools.Utils;
    5960
    6061/**
     
    167168
    168169    public static void saveIgnoredErrors() {
     170        PrintWriter out = null;
    169171        try {
    170             final PrintWriter out = new PrintWriter(new FileWriter(getValidatorDir() + "ignorederrors"), false);
     172            out = new PrintWriter(new FileWriter(getValidatorDir() + "ignorederrors"), false);
    171173            for (String e : ignoredErrors) {
    172174                out.println(e);
    173175            }
    174             out.close();
    175         } catch (final IOException e) {
     176        } catch (IOException e) {
    176177            e.printStackTrace();
     178        } finally {
     179            Utils.close(out);
    177180        }
    178181    }
Note: See TracChangeset for help on using the changeset viewer.