Changeset 7033 in josm for trunk/src/org/openstreetmap/josm/plugins
- Timestamp:
- 2014-05-01T02:34:43+02:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/plugins
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/plugins/Plugin.java
r6615 r7033 110 110 pluginDir.mkdirs(); 111 111 } 112 FileOutputStream out = null; 113 InputStream in = null; 114 try { 115 out = new FileOutputStream(new File(pluginDirName, to)); 116 in = getClass().getResourceAsStream(from); 112 try ( 113 FileOutputStream out = new FileOutputStream(new File(pluginDirName, to)); 114 InputStream in = getClass().getResourceAsStream(from) 115 ) { 117 116 if (in == null) { 118 117 throw new IOException("Resource not found: "+from); … … 122 121 out.write(buffer, 0, len); 123 122 } 124 } finally {125 Utils.close(in);126 Utils.close(out);127 123 } 128 124 } -
trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
r7005 r7033 24 24 import org.openstreetmap.josm.io.MirroredInputStream; 25 25 import org.openstreetmap.josm.tools.CheckParameterUtil; 26 import org.openstreetmap.josm.tools.Utils;27 26 import org.xml.sax.SAXException; 28 27 … … 118 117 throw new PluginDownloadException(tr("Download skipped")); 119 118 } 120 OutputStream out = null;121 InputStream in = null;122 119 try { 123 120 if (pi.downloadlink == null) { … … 130 127 downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES); 131 128 } 132 in = downloadConnection.getInputStream(); 133 out = new FileOutputStream(file); 134 byte[] buffer = new byte[8192]; 135 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { 136 out.write(buffer, 0, read); 129 try ( 130 InputStream in = downloadConnection.getInputStream(); 131 OutputStream out = new FileOutputStream(file) 132 ) { 133 byte[] buffer = new byte[8192]; 134 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { 135 out.write(buffer, 0, read); 136 } 137 137 } 138 138 } catch (MalformedURLException e) { … … 145 145 throw new PluginDownloadException(e); 146 146 } finally { 147 Utils.close(in);148 147 synchronized(this) { 149 148 downloadConnection = null; 150 149 } 151 Utils.close(out);152 150 } 153 151 } -
trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java
r7005 r7033 107 107 this.name = name; 108 108 this.file = file; 109 FileInputStream fis = null; 110 JarInputStream jar = null; 111 try { 112 fis = new FileInputStream(file); 113 jar = new JarInputStream(fis); 109 try ( 110 FileInputStream fis = new FileInputStream(file); 111 JarInputStream jar = new JarInputStream(fis) 112 ) { 114 113 Manifest manifest = jar.getManifest(); 115 114 if (manifest == null) … … 119 118 } catch (IOException e) { 120 119 throw new PluginException(name, e); 121 } finally {122 Utils.close(jar);123 Utils.close(fis);124 120 } 125 121 } … … 380 376 String name = pluginName; 381 377 name = name.replaceAll("[-. ]", ""); 382 InputStream manifestStream = PluginInformation.class.getResourceAsStream("/org/openstreetmap/josm/plugins/"+name+"/MANIFEST.MF"); 383 if (manifestStream != null) 384 return new PluginInformation(manifestStream, pluginName, null); 378 try (InputStream manifestStream = PluginInformation.class.getResourceAsStream("/org/openstreetmap/josm/plugins/"+name+"/MANIFEST.MF")) { 379 if (manifestStream != null) { 380 return new PluginInformation(manifestStream, pluginName, null); 381 } 382 } catch (IOException e) { 383 Main.warn(e); 384 } 385 385 386 386 Collection<String> locations = getPluginLocations(); -
trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java
r7005 r7033 172 172 173 173 protected void processLocalPluginInformationFile(File file) throws PluginListParseException{ 174 FileInputStream fin = null; 175 try { 176 fin = new FileInputStream(file); 174 try (FileInputStream fin = new FileInputStream(file)) { 177 175 List<PluginInformation> pis = new PluginListParser().parse(fin); 178 176 for (PluginInformation pi : pis) { … … 185 183 } catch(IOException e) { 186 184 throw new PluginListParseException(e); 187 } finally {188 Utils.close(fin);189 185 } 190 186 } -
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.