Ignore:
Timestamp:
2014-05-09T05:21:14+02:00 (11 years ago)
Author:
donvip
Message:

[josm_plugins] see #josm8465 - Java 7 code update

Location:
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/SevenZipReader.java

    r30340 r30436  
    3535    public SevenZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) throws IOException {
    3636        super(handler, handler != null ? handler.getArchiveHandler() : null, promptUser);
    37         OutputStream out = null;
    38         try {
    39             // Write entire 7z file as a temp file on disk as we need random access later, and "in" can be a network stream
    40             File tmpFile = File.createTempFile("7z_", ".7z", OdUtils.createTempDir());
    41             out = new FileOutputStream(tmpFile);
     37        // Write entire 7z file as a temp file on disk as we need random access later, and "in" can be a network stream
     38        File tmpFile = File.createTempFile("7z_", ".7z", OdUtils.createTempDir());
     39        try (OutputStream out = new FileOutputStream(tmpFile)) {
    4240            Utils.copyStream(in, out);
    43             Utils.close(out);
    44             out = null;
    45             IInStream random = new MyRandomAccessFile(tmpFile.getPath(), "r");
    46             if (archive.Open(random) != 0) {
    47                 String message = "Unable to open 7z archive: "+tmpFile.getPath();
    48                 Main.warn(message);
    49                 random.close();
    50                 if (!tmpFile.delete()) {
    51                     tmpFile.deleteOnExit();
    52                 }
    53                 throw new IOException(message);
     41        }
     42        IInStream random = new MyRandomAccessFile(tmpFile.getPath(), "r");
     43        if (archive.Open(random) != 0) {
     44            String message = "Unable to open 7z archive: "+tmpFile.getPath();
     45            Main.warn(message);
     46            random.close();
     47            if (!tmpFile.delete()) {
     48                tmpFile.deleteOnExit();
    5449            }
    55         } finally {
    56             Utils.close(out);
     50            throw new IOException(message);
    5751        }
    5852    }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleDownloadTask.java

    r30340 r30436  
    2222import org.openstreetmap.josm.plugins.opendata.OdPlugin;
    2323import org.openstreetmap.josm.tools.CheckParameterUtil;
    24 import org.openstreetmap.josm.tools.Utils;
    2524import org.xml.sax.SAXException;
    2625
     
    9291
    9392    protected void download(ModuleInformation pi, File file) throws ModuleDownloadException{
    94         OutputStream out = null;
    95         InputStream in = null;
    9693        try {
    9794            if (pi.downloadlink == null) {
     
    108105                downloadConnection.connect();
    109106            }
    110             in = downloadConnection.getInputStream();
    111             out = new FileOutputStream(file);
    112             byte[] buffer = new byte[8192];
    113             for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
    114                 out.write(buffer, 0, read);
     107            try (
     108                InputStream in = downloadConnection.getInputStream();
     109                OutputStream out = new FileOutputStream(file)
     110            ) {
     111                byte[] buffer = new byte[8192];
     112                for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
     113                    out.write(buffer, 0, read);
     114                }
    115115            }
    116             out.close();
    117             in.close();
    118116        } catch(MalformedURLException e) {
    119117            String msg = tr("Warning: Cannot download module ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", pi.name, pi.downloadlink);
     
    125123            throw new ModuleDownloadException(e);
    126124        } finally {
    127             Utils.close(in);
    128125            synchronized(this) {
    129126                downloadConnection = null;
    130127            }
    131             Utils.close(out);
    132128        }
    133129    }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadLocalModuleInformationTask.java

    r30340 r30436  
    174174
    175175    protected void processLocalModuleInformationFile(File file) throws ModuleListParseException{
    176         FileInputStream fin = null;
    177         try {
    178             fin = new FileInputStream(file);
     176        try (FileInputStream fin = new FileInputStream(file)) {
    179177            List<ModuleInformation> pis = new ModuleListParser().parse(fin);
    180178            for (ModuleInformation pi : pis) {
     
    187185        } catch(IOException e) {
    188186            throw new ModuleListParseException(e);
    189         } finally {
    190             Utils.close(fin);
    191187        }
    192188    }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadRemoteModuleInformationTask.java

    r30340 r30436  
    3333import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
    3434import org.openstreetmap.josm.tools.ImageProvider;
    35 import org.openstreetmap.josm.tools.Utils;
    3635import org.xml.sax.SAXException;
    3736
     
    142141     */
    143142    protected String downloadModuleList(String site, ProgressMonitor monitor) {
    144         BufferedReader in = null;
    145143        StringBuilder sb = new StringBuilder();
    146144        try {
     
    159157                connection.setRequestProperty("Accept-Charset", "utf-8");
    160158            }
    161             in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    162             String line;
    163             while((line = in.readLine()) != null) {
    164                 sb.append(line).append("\n");
    165             }
    166             return sb.toString();
     159            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
     160                String line;
     161                while((line = in.readLine()) != null) {
     162                    sb.append(line).append("\n");
     163                }
     164                return sb.toString();
     165            }
    167166        } catch(MalformedURLException e) {
    168167            if (canceled) return null;
     
    180179                connection = null;
    181180            }
    182             Utils.close(in);
    183181            monitor.finishTask();
    184182        }
     
    192190     */
    193191    protected void downloadModuleIcons(String site, File destFile, ProgressMonitor monitor) {
    194         InputStream in = null;
    195         OutputStream out = null;
    196192        try {
    197193            site = site.replaceAll("%<(.*)>", "");
     
    207203                connection.setRequestProperty("Host", url.getHost());
    208204            }
    209             in = connection.getInputStream();
    210             out = new FileOutputStream(destFile);
    211             byte[] buffer = new byte[8192];
    212             for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
    213                 out.write(buffer, 0, read);
    214             }
    215             out.close();
    216             in.close();
     205            try (
     206                InputStream in = connection.getInputStream();
     207                OutputStream out = new FileOutputStream(destFile)
     208            ) {
     209                byte[] buffer = new byte[8192];
     210                for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
     211                    out.write(buffer, 0, read);
     212                }
     213            }
    217214        } catch(MalformedURLException e) {
    218215            if (canceled) return;
     
    230227                connection = null;
    231228            }
    232             Utils.close(in);
    233229            monitor.finishTask();
    234230        }
Note: See TracChangeset for help on using the changeset viewer.