Ignore:
Timestamp:
2014-05-01T02:34:43+02:00 (10 years ago)
Author:
Don-vip
Message:

see #8465 - global use of try-with-resources, according to

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r7005 r7033  
    203203
    204204    private void handleIOException(final ProgressMonitor monitor, IOException e, final String title, final String firstMessage, boolean displayMsg) {
    205         InputStream errStream = connection.getErrorStream();
    206205        StringBuilder sb = new StringBuilder();
    207         if (errStream != null) {
    208             BufferedReader err = null;
    209             try {
    210                 String line;
    211                 err = new BufferedReader(new InputStreamReader(errStream, Utils.UTF_8));
    212                 while ((line = err.readLine()) != null) {
    213                     sb.append(line).append("\n");
    214                 }
    215             } catch (Exception ex) {
    216                 Main.error(e);
    217                 Main.error(ex);
    218             } finally {
    219                 Utils.close(err);
    220             }
     206        try (InputStream errStream = connection.getErrorStream()) {
     207            if (errStream != null) {
     208                BufferedReader err = null;
     209                try {
     210                    String line;
     211                    err = new BufferedReader(new InputStreamReader(errStream, Utils.UTF_8));
     212                    while ((line = err.readLine()) != null) {
     213                        sb.append(line).append("\n");
     214                    }
     215                } catch (Exception ex) {
     216                    Main.error(e);
     217                    Main.error(ex);
     218                } finally {
     219                    Utils.close(err);
     220                }
     221            }
     222        } catch (IOException ex) {
     223            Main.warn(ex);
    221224        }
    222225        final String msg = e.getMessage();
     
    265268     */
    266269    protected void downloadPluginIcons(String site, File destFile, ProgressMonitor monitor) {
    267         InputStream in = null;
    268         OutputStream out = null;
    269270        try {
    270271            site = site.replaceAll("%<(.*)>", "");
     
    278279                connection.setRequestProperty("Cache-Control", "no-cache");
    279280            }
    280             in = connection.getInputStream();
    281             out = new FileOutputStream(destFile);
    282             byte[] buffer = new byte[8192];
    283             for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
    284                 out.write(buffer, 0, read);
     281            try (
     282                InputStream in = connection.getInputStream();
     283                OutputStream out = new FileOutputStream(destFile)
     284            ) {
     285                byte[] buffer = new byte[8192];
     286                for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
     287                    out.write(buffer, 0, read);
     288                }
    285289            }
    286290        } catch (MalformedURLException e) {
     
    293297            return;
    294298        } finally {
    295             Utils.close(out);
    296299            synchronized(this) {
    297300                if (connection != null) {
     
    300303                connection = null;
    301304            }
    302             Utils.close(in);
    303305            monitor.finishTask();
    304306        }
     
    321323     */
    322324    protected void cachePluginList(String site, String list) {
    323         PrintWriter writer = null;
    324         try {
    325             File pluginDir = Main.pref.getPluginsDirectory();
    326             if (!pluginDir.exists() && !pluginDir.mkdirs()) {
    327                 Main.warn(tr("Failed to create plugin directory ''{0}''. Cannot cache plugin list from plugin site ''{1}''.", pluginDir.toString(), site));
    328             }
    329             File cacheFile = createSiteCacheFile(pluginDir, site, CacheType.PLUGIN_LIST);
    330             getProgressMonitor().subTask(tr("Writing plugin list to local cache ''{0}''", cacheFile.toString()));
    331             writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), Utils.UTF_8));
     325        File pluginDir = Main.pref.getPluginsDirectory();
     326        if (!pluginDir.exists() && !pluginDir.mkdirs()) {
     327            Main.warn(tr("Failed to create plugin directory ''{0}''. Cannot cache plugin list from plugin site ''{1}''.", pluginDir.toString(), site));
     328        }
     329        File cacheFile = createSiteCacheFile(pluginDir, site, CacheType.PLUGIN_LIST);
     330        getProgressMonitor().subTask(tr("Writing plugin list to local cache ''{0}''", cacheFile.toString()));
     331        try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), Utils.UTF_8))) {
    332332            writer.write(list);
     333            writer.flush();
    333334        } catch(IOException e) {
    334335            // just failed to write the cache file. No big deal, but log the exception anyway
    335336            Main.error(e);
    336         } finally {
    337             if (writer != null) {
    338                 writer.flush();
    339                 Utils.close(writer);
    340             }
    341337        }
    342338    }
Note: See TracChangeset for help on using the changeset viewer.