Changeset 7033 in josm for trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
- Timestamp:
- 2014-05-01T02:34:43+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
r7005 r7033 203 203 204 204 private void handleIOException(final ProgressMonitor monitor, IOException e, final String title, final String firstMessage, boolean displayMsg) { 205 InputStream errStream = connection.getErrorStream();206 205 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); 221 224 } 222 225 final String msg = e.getMessage(); … … 265 268 */ 266 269 protected void downloadPluginIcons(String site, File destFile, ProgressMonitor monitor) { 267 InputStream in = null;268 OutputStream out = null;269 270 try { 270 271 site = site.replaceAll("%<(.*)>", ""); … … 278 279 connection.setRequestProperty("Cache-Control", "no-cache"); 279 280 } 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 } 285 289 } 286 290 } catch (MalformedURLException e) { … … 293 297 return; 294 298 } finally { 295 Utils.close(out);296 299 synchronized(this) { 297 300 if (connection != null) { … … 300 303 connection = null; 301 304 } 302 Utils.close(in);303 305 monitor.finishTask(); 304 306 } … … 321 323 */ 322 324 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))) { 332 332 writer.write(list); 333 writer.flush(); 333 334 } catch(IOException e) { 334 335 // just failed to write the cache file. No big deal, but log the exception anyway 335 336 Main.error(e); 336 } finally {337 if (writer != null) {338 writer.flush();339 Utils.close(writer);340 }341 337 } 342 338 }
Note:
See TracChangeset
for help on using the changeset viewer.