Changeset 5874 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2013-04-16T19:57:43+02:00 (12 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 54 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java
r4996 r5874 38 38 import org.openstreetmap.josm.tools.MultiMap; 39 39 import org.openstreetmap.josm.tools.Shortcut; 40 import org.openstreetmap.josm.tools.Utils; 40 41 import org.xml.sax.SAXException; 41 42 … … 48 49 public class OpenFileAction extends DiskAccessAction { 49 50 51 /** 52 * The {@link ExtensionFileFilter} matching .url files 53 */ 50 54 public static final ExtensionFileFilter urlFileFilter = new ExtensionFileFilter("url", "url", tr("URL Files") + " (*.url)"); 51 55 … … 57 61 Shortcut.registerShortcut("system:open", tr("File: {0}", tr("Open...")), KeyEvent.VK_O, Shortcut.CTRL)); 58 62 putValue("help", ht("/Action/Open")); 59 60 63 } 61 64 … … 296 299 } 297 300 } 298 reader.close();301 Utils.close(reader); 299 302 } catch (Exception e) { 300 303 System.err.println(e.getMessage()); … … 333 336 } 334 337 338 /** 339 * Replies the list of files that have been successfully opened. 340 * @return The list of files that have been successfully opened. 341 */ 335 342 public List<File> getSuccessfullyOpenedFiles() { 336 343 return successfullyOpenedFiles; -
trunk/src/org/openstreetmap/josm/data/AutosaveTask.java
r5566 r5874 38 38 import org.openstreetmap.josm.io.OsmExporter; 39 39 import org.openstreetmap.josm.io.OsmImporter; 40 import org.openstreetmap.josm.tools.Utils; 40 41 41 42 /** … … 156 157 PrintStream ps = new PrintStream(pidFile); 157 158 ps.println(ManagementFactory.getRuntimeMXBean().getName()); 158 ps.close();159 Utils.close(ps); 159 160 } catch (Throwable t) { 160 161 System.err.println(t.getMessage()); … … 299 300 System.err.println(t.getClass()+":"+t.getMessage()); 300 301 } finally { 301 reader.close();302 Utils.close(reader); 302 303 } 303 304 } catch (Throwable t) { -
trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java
r5590 r5874 1 1 package org.openstreetmap.josm.data; 2 2 3 import javax.script.ScriptException;4 import org.openstreetmap.josm.Main;5 import org.openstreetmap.josm.data.Preferences.Setting;6 3 import static org.openstreetmap.josm.tools.I18n.tr; 7 4 … … 12 9 import java.io.File; 13 10 import java.io.FileInputStream; 14 import java.io.IOException;15 11 import java.io.InputStream; 16 17 12 import java.util.ArrayList; 18 import java.util.Arrays;19 13 import java.util.Collection; 20 14 import java.util.Collections; … … 30 24 import java.util.regex.Matcher; 31 25 import java.util.regex.Pattern; 26 32 27 import javax.script.ScriptEngine; 33 28 import javax.script.ScriptEngineManager; 29 import javax.script.ScriptException; 34 30 import javax.swing.JOptionPane; 35 31 import javax.swing.SwingUtilities; … … 42 38 import javax.xml.transform.stream.StreamResult; 43 39 40 import org.openstreetmap.josm.Main; 41 import org.openstreetmap.josm.data.Preferences.Setting; 44 42 import org.openstreetmap.josm.gui.io.DownloadFileTask; 45 43 import org.openstreetmap.josm.plugins.PluginDownloadTask; … … 47 45 import org.openstreetmap.josm.plugins.ReadLocalPluginInformationTask; 48 46 import org.openstreetmap.josm.tools.LanguageInfo; 47 import org.openstreetmap.josm.tools.Utils; 49 48 import org.w3c.dom.Document; 50 49 import org.w3c.dom.Element; … … 441 440 log("Error reading custom preferences: "+ex.getMessage()); 442 441 } finally { 443 try { 444 if (is != null) { 445 is.close(); 446 } 447 } catch (IOException ex) { } 442 Utils.close(is); 448 443 } 449 444 log("-- Reading complete --"); -
trunk/src/org/openstreetmap/josm/data/Preferences.java
r5871 r5874 18 18 import java.lang.annotation.RetentionPolicy; 19 19 import java.lang.reflect.Field; 20 import java.nio.channels.FileChannel;21 20 import java.util.ArrayList; 22 21 import java.util.Arrays; … … 137 136 } 138 137 138 /** 139 * Base abstract class of all settings, holding the setting value. 140 * 141 * @param <T> The setting type 142 */ 139 143 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 */ 141 149 public AbstractSetting(T value) { 142 150 this.value = value; 143 151 } 144 @Override 145 public T getValue() { 152 @Override public T getValue() { 146 153 return value; 147 154 } 148 @Override 149 public String toString() { 155 @Override public String toString() { 150 156 return value != null ? value.toString() : "null"; 151 157 } 152 158 } 153 159 160 /** 161 * Setting containing a {@link String} value. 162 */ 154 163 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 */ 155 168 public StringSetting(String value) { 156 169 super(value); 157 170 } 158 public void visit(SettingVisitor visitor) {171 @Override public void visit(SettingVisitor visitor) { 159 172 visitor.visit(this); 160 173 } 161 public StringSetting getNullInstance() {174 @Override public StringSetting getNullInstance() { 162 175 return new StringSetting(null); 163 176 } 164 177 } 165 178 179 /** 180 * Setting containing a {@link List} of {@link String} values. 181 */ 166 182 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 */ 167 187 public ListSetting(List<String> value) { 168 188 super(value); 169 189 } 170 public void visit(SettingVisitor visitor) {190 @Override public void visit(SettingVisitor visitor) { 171 191 visitor.visit(this); 172 192 } 173 public ListSetting getNullInstance() {193 @Override public ListSetting getNullInstance() { 174 194 return new ListSetting(null); 175 195 } 176 196 } 177 197 198 /** 199 * Setting containing a {@link List} of {@code List}s of {@link String} values. 200 */ 178 201 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 */ 179 206 public ListListSetting(List<List<String>> value) { 180 207 super(value); 181 208 } 182 public void visit(SettingVisitor visitor) {209 @Override public void visit(SettingVisitor visitor) { 183 210 visitor.visit(this); 184 211 } 185 public ListListSetting getNullInstance() {212 @Override public ListListSetting getNullInstance() { 186 213 return new ListListSetting(null); 187 214 } 188 215 } 189 216 217 /** 218 * Setting containing a {@link List} of {@link Map}s of {@link String} values. 219 */ 190 220 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 */ 191 225 public MapListSetting(List<Map<String, String>> value) { 192 226 super(value); 193 227 } 194 public void visit(SettingVisitor visitor) {228 @Override public void visit(SettingVisitor visitor) { 195 229 visitor.visit(this); 196 230 } 197 public MapListSetting getNullInstance() {231 @Override public MapListSetting getNullInstance() { 198 232 return new MapListSetting(null); 199 233 } … … 265 299 266 300 /** 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 268 303 */ 269 304 public String getPreferencesDir() { … … 274 309 } 275 310 311 /** 312 * Returns the user defined preferences directory 313 * @return The user defined preferences directory 314 */ 276 315 public File getPreferencesDirFile() { 277 316 if (preferencesDirFile != null) … … 292 331 } 293 332 333 /** 334 * Returns the user preferences file 335 * @return The user preferences file 336 */ 294 337 public File getPreferenceFile() { 295 338 return new File(getPreferencesDirFile(), "preferences.xml"); 296 339 } 297 340 341 /** 342 * Returns the user plugin directory 343 * @return The user plugin directory 344 */ 298 345 public File getPluginsDirectory() { 299 346 return new File(getPreferencesDirFile(), "plugins"); … … 551 598 552 599 // 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); 555 602 } 556 603 … … 558 605 new FileOutputStream(prefFile + "_tmp"), "utf-8"), false); 559 606 out.print(toXML(false)); 560 out.close();607 Utils.close(out); 561 608 562 609 File tmpFile = new File(prefFile + "_tmp"); 563 copyFile(tmpFile, prefFile);610 Utils.copyFile(tmpFile, prefFile); 564 611 tmpFile.delete(); 565 612 … … 575 622 file.setReadable(true, true); 576 623 file.setWritable(true, true); 577 }578 579 /**580 * Simple file copy function that will overwrite the target file581 * Taken from http://www.rgagnon.com/javadetails/java-0064.html (CC-NC-BY-SA)582 * @param in583 * @param out584 * @throws IOException585 */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 }604 624 } 605 625 … … 615 635 fromXML(in); 616 636 } finally { 617 in.close();637 Utils.close(in); 618 638 } 619 639 } -
trunk/src/org/openstreetmap/josm/data/ServerSidePreferences.java
r4874 r5874 22 22 import org.openstreetmap.josm.io.OsmConnection; 23 23 import org.openstreetmap.josm.tools.Base64; 24 import org.openstreetmap.josm.tools.Utils; 24 25 25 26 /** … … 91 92 PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream())); 92 93 out.println(s); 93 out.close();94 con.getInputStream().close();94 Utils.close(out); 95 Utils.close(con.getInputStream()); 95 96 con.disconnect(); 96 97 JOptionPane.showMessageDialog( -
trunk/src/org/openstreetmap/josm/data/Version.java
r5868 r5874 44 44 } 45 45 } finally { 46 in.close();46 Utils.close(in); 47 47 } 48 48 s = sb.toString(); -
trunk/src/org/openstreetmap/josm/data/imagery/WmsCache.java
r5763 r5874 156 156 } 157 157 } 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); 168 160 } 169 161 … … 532 524 totalFileSize += Utils.copyStream(imageData, os); 533 525 } finally { 534 os.close();526 Utils.close(os); 535 527 } 536 528 } -
trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFile.java
r5073 r5874 25 25 import java.util.ArrayList; 26 26 import java.util.HashMap; 27 28 import org.openstreetmap.josm.tools.Utils; 27 29 28 30 /** … … 151 153 lastSubGrid = topLevelSubGrid[0]; 152 154 153 in.close();155 Utils.close(in); 154 156 } 155 157 -
trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java
r5644 r5874 57 57 import org.openstreetmap.josm.gui.preferences.ValidatorPreference; 58 58 import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference; 59 import org.openstreetmap.josm.tools.Utils; 59 60 60 61 /** … … 167 168 168 169 public static void saveIgnoredErrors() { 170 PrintWriter out = null; 169 171 try { 170 final PrintWriterout = new PrintWriter(new FileWriter(getValidatorDir() + "ignorederrors"), false);172 out = new PrintWriter(new FileWriter(getValidatorDir() + "ignorederrors"), false); 171 173 for (String e : ignoredErrors) { 172 174 out.println(e); 173 175 } 174 out.close(); 175 } catch (final IOException e) { 176 } catch (IOException e) { 176 177 e.printStackTrace(); 178 } finally { 179 Utils.close(out); 177 180 } 178 181 } -
trunk/src/org/openstreetmap/josm/gui/BookmarkList.java
r3697 r5874 32 32 import org.openstreetmap.josm.data.Bounds; 33 33 import org.openstreetmap.josm.tools.ImageProvider; 34 import org.openstreetmap.josm.tools.Utils; 34 35 35 36 /** … … 151 152 bookmarks.add(b); 152 153 } 153 in.close();154 Utils.close(in); 154 155 Collections.sort(bookmarks); 155 156 for (Bookmark b : bookmarks) { -
trunk/src/org/openstreetmap/josm/gui/MultiSplitLayout.java
r5698 r5874 44 44 45 45 import javax.swing.UIManager; 46 47 import org.openstreetmap.josm.tools.Utils; 46 48 47 49 /** … … 1259 1261 } 1260 1262 finally { 1261 try { r.close(); } catch (IOException ignore) {}1263 Utils.close(r); 1262 1264 } 1263 1265 return null; -
trunk/src/org/openstreetmap/josm/gui/help/HelpBrowser.java
r5816 r5874 51 51 import org.openstreetmap.josm.tools.ImageProvider; 52 52 import org.openstreetmap.josm.tools.OpenBrowser; 53 import org.openstreetmap.josm.tools.Utils; 53 54 import org.openstreetmap.josm.tools.WindowGeometry; 54 55 … … 141 142 css.append("\n"); 142 143 } 143 reader.close();144 144 } catch(Exception e) { 145 145 System.err.println(tr("Failed to read CSS file ''help-browser.css''. Exception is: {0}", e.toString())); 146 146 e.printStackTrace(); 147 147 return ss; 148 } finally { 149 Utils.close(reader); 148 150 } 149 151 ss.addRule(css.toString()); -
trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java
r5868 r5874 62 62 throw ex; 63 63 } finally { 64 if (in != null) { 65 try { 66 in.close(); 67 } catch(IOException e) { 68 // ignore 69 } 70 } 64 Utils.close(in); 71 65 } 72 66 } -
trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java
r5587 r5874 23 23 import org.xml.sax.SAXException; 24 24 25 26 25 /** 27 26 * Asynchronous task for downloading and unpacking arbitrary file lists … … 38 37 * 39 38 * @param parent the parent component relative to which the {@link PleaseWaitDialog} is displayed 40 * @param title the title to display in the {@link PleaseWaitDialog} 41 * @throws IllegalArgumentException thrown if toUpdate is null 39 * @param address the URL to download 40 * @param file The destination file 41 * @param mkdir {@code true} if the destination directory must be created, {@code false} otherwise 42 * @param unpack {@code true} if zip archives must be unpacked recursively, {@code false} otherwise 43 * @throws IllegalArgumentException if {@code parent} is null 42 44 */ 43 45 public DownloadFileTask(Component parent, String address, File file, boolean mkdir, boolean unpack) { … … 47 49 this.mkdir = mkdir; 48 50 this.unpack = unpack; 49 50 51 } 51 52 … … 76 77 protected void finish() {} 77 78 79 /** 80 * Performs download. 81 * @throws DownloadException if the URL is invalid or if any I/O error occurs. 82 */ 78 83 public void download() throws DownloadException { 79 84 OutputStream out = null; … … 107 112 out.write(buffer, 0, read); 108 113 count+=read; 109 if (canceled) return;114 if (canceled) break; 110 115 p2 = 100 * count / size; 111 116 if (p2!=p1) { … … 114 119 } 115 120 } 116 out.close(); 117 System.out.println(tr("Download finished")); 118 if (unpack) { 119 System.out.println(tr("Unpacking {0} into {1}", file.getAbsolutePath(), file.getParent())); 120 unzipFileRecursively(file, file.getParent()); 121 file.delete(); 121 Utils.close(out); 122 if (!canceled) { 123 System.out.println(tr("Download finished")); 124 if (unpack) { 125 System.out.println(tr("Unpacking {0} into {1}", file.getAbsolutePath(), file.getParent())); 126 unzipFileRecursively(file, file.getParent()); 127 file.delete(); 128 } 122 129 } 123 130 } catch(MalformedURLException e) { … … 148 155 * Replies true if the task was canceled by the user 149 156 * 150 * @return 157 * @return {@code true} if the task was canceled by the user, {@code false} otherwise 151 158 */ 152 159 public boolean isCanceled() { … … 182 189 os.write(buffer, 0, read); 183 190 } 184 os.close(); 185 is.close(); 186 } 187 } 188 zf.close(); 191 Utils.close(os); 192 Utils.close(is); 193 } 194 } 189 195 } finally { 190 if (zf!=null) zf.close();196 Utils.close(zf); 191 197 } 192 198 } -
trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java
r5868 r5874 324 324 UTFInputStreamReader in = UTFInputStreamReader.create(Utils.openURL(u), "utf-8"); 325 325 String r = new Scanner(in).useDelimiter("\\A").next(); 326 in.close();326 Utils.close(in); 327 327 System.out.println("Successfully loaded Bing attribution data."); 328 328 return r.getBytes("utf-8"); -
trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
r5399 r5874 31 31 import org.openstreetmap.josm.io.MirroredInputStream; 32 32 import org.openstreetmap.josm.tools.ImageProvider; 33 import org.openstreetmap.josm.tools.Utils; 33 34 34 35 /** … … 247 248 e.printStackTrace(); 248 249 } finally { 249 try { 250 if (in != null) { 251 in.close(); 252 } 253 } catch (IOException ex) { 254 } 250 Utils.close(in); 255 251 } 256 252 return null; -
trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
r5587 r5874 405 405 dout.writeBytes(request); 406 406 dout.flush(); 407 dout.close();407 Utils.close(dout); 408 408 409 409 // after a successful login the OSM website sends a redirect to a follow up page. Everything … … 419 419 throw new OsmLoginFailedException(e); 420 420 } finally { 421 if (dout != null) { 422 try { 423 dout.close(); 424 } catch(IOException e) { /* ignore */ } 425 } 421 Utils.close(dout); 426 422 synchronized(this) { 427 423 connection = null; … … 496 492 dout.writeBytes(request); 497 493 dout.flush(); 498 dout.close();499 494 500 495 int retCode = connection.getResponseCode(); … … 506 501 throw new OsmOAuthAuthorizationException(e); 507 502 } finally { 508 if (dout != null) { 509 try { 510 dout.close(); 511 } catch(IOException e) { /* ignore */ } 512 } 503 Utils.close(dout); 513 504 synchronized(this) { 514 505 connection = null; -
trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
r5784 r5874 1174 1174 protected void cancel() { 1175 1175 canceled = true; 1176 if (reader!= null) { 1177 try { 1178 reader.close(); 1179 } catch(IOException e) { 1180 // ignore 1181 } 1182 } 1183 } 1184 1176 Utils.close(reader); 1177 } 1185 1178 1186 1179 protected void warn(Exception e) { -
trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java
r5587 r5874 216 216 return; 217 217 } finally { 218 if (bin != null) { 219 try { 220 bin.close(); 221 } catch(IOException e){/* ignore */} 222 } 218 Utils.close(bin); 223 219 } 224 220 } -
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r5866 r5874 1590 1590 tp = TaggingPreset.readAll(new BufferedReader(r), validate); 1591 1591 } finally { 1592 r.close();1592 Utils.close(r); 1593 1593 } 1594 1594 } finally { 1595 s.close();1595 Utils.close(s); 1596 1596 } 1597 1597 return tp; -
trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java
r5679 r5874 11 11 import org.openstreetmap.josm.data.osm.DataSet; 12 12 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 13 import org.openstreetmap.josm.tools.Utils; 13 14 import org.xml.sax.SAXException; 14 15 … … 52 53 done = true; 53 54 } 54 in.close();55 Utils.close(in); 55 56 activeConnection = null; 56 57 } … … 145 146 } finally { 146 147 progressMonitor.finishTask(); 147 if (in != null) { 148 try {in.close();} catch(IOException e) {} 149 } 148 Utils.close(in); 150 149 activeConnection = null; 151 150 } -
trunk/src/org/openstreetmap/josm/io/CacheCustomContent.java
r4810 r5874 12 12 13 13 import org.openstreetmap.josm.Main; 14 import org.openstreetmap.josm.tools.Utils; 14 15 15 16 /** … … 159 160 this.data = updateForce(); 160 161 else { 162 BufferedInputStream input = null; 161 163 try { 162 BufferedInputStreaminput = new BufferedInputStream(new FileInputStream(path));164 input = new BufferedInputStream(new FileInputStream(path)); 163 165 this.data = new byte[input.available()]; 164 166 input.read(this.data); 165 input.close();166 167 } catch (IOException e) { 167 168 this.data = updateForce(); 169 } finally { 170 Utils.close(input); 168 171 } 169 172 } … … 176 179 if (Main.applet) 177 180 return; 181 BufferedOutputStream output = null; 178 182 try { 179 BufferedOutputStreamoutput = new BufferedOutputStream(new FileOutputStream(path));183 output = new BufferedOutputStream(new FileOutputStream(path)); 180 184 output.write(this.data); 181 185 output.flush(); 182 output.close();183 186 } catch(Exception e) { 184 187 e.printStackTrace(); 188 } finally { 189 Utils.close(output); 185 190 } 186 191 } -
trunk/src/org/openstreetmap/josm/io/GeoJSONExporter.java
r5749 r5874 14 14 import org.openstreetmap.josm.gui.layer.Layer; 15 15 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 16 import org.openstreetmap.josm.tools.Utils; 16 17 17 18 public class GeoJSONExporter extends FileExporter { … … 32 33 out.write(json); 33 34 } finally { 34 out.close();35 Utils.close(out); 35 36 } 36 37 } else { -
trunk/src/org/openstreetmap/josm/io/GpxExporter.java
r5681 r5874 36 36 import org.openstreetmap.josm.tools.CheckParameterUtil; 37 37 import org.openstreetmap.josm.tools.GBC; 38 import org.openstreetmap.josm.tools.Utils; 38 39 39 40 public class GpxExporter extends FileExporter implements GpxConstants { … … 169 170 } 170 171 172 FileOutputStream fo = null; 171 173 try { 172 FileOutputStreamfo = new FileOutputStream(file);174 fo = new FileOutputStream(file); 173 175 new GpxWriter(fo).write(gpxData); 174 176 fo.flush(); 175 fo.close();176 177 } catch (IOException x) { 177 178 x.printStackTrace(); 178 179 JOptionPane.showMessageDialog(Main.parent, tr("Error while exporting {0}:\n{1}", fn, x.getMessage()), 179 180 tr("Error"), JOptionPane.ERROR_MESSAGE); 180 } 181 181 } finally { 182 Utils.close(fo); 183 } 182 184 } 183 185 -
trunk/src/org/openstreetmap/josm/io/GpxWriter.java
r5684 r5874 38 38 } 39 39 40 public GpxWriter() {41 super(null);42 //sorry for this one here, this will be cleaned up once the new scheme works43 }44 45 40 private GpxData data; 46 41 private String indent = ""; 47 public String creator = "JOSM GPX export";48 42 49 43 private final static int WAY_POINT = 0; -
trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java
r5868 r5874 124 124 res = zipFile.getInputStream(resentry); 125 125 } else { 126 zipFile.close();126 Utils.close(zipFile); 127 127 } 128 128 } catch (Exception e) { … … 224 224 bos.write(buffer, 0, length); 225 225 } 226 bos.close();226 Utils.close(bos); 227 227 bos = null; 228 228 /* close fos as well to be sure! */ 229 fos.close();229 Utils.close(fos); 230 230 fos = null; 231 231 localFile = new File(destDir, localPath); … … 304 304 @Override 305 305 public void close() throws IOException 306 { fs.close(); }306 { Utils.close(fs); } 307 307 @Override 308 308 public int read() throws IOException -
trunk/src/org/openstreetmap/josm/io/NmeaReader.java
r5108 r5874 20 20 import org.openstreetmap.josm.data.gpx.WayPoint; 21 21 import org.openstreetmap.josm.tools.DateUtils; 22 import org.openstreetmap.josm.tools.Utils; 22 23 23 24 /** … … 176 177 Collection<Collection<WayPoint>> currentTrack = new ArrayList<Collection<WayPoint>>(); 177 178 179 BufferedReader rd = null; 178 180 try { 179 BufferedReader rd = 180 new BufferedReader(new InputStreamReader(source)); 181 rd = new BufferedReader(new InputStreamReader(source)); 181 182 182 183 StringBuffer sb = new StringBuffer(1024); … … 206 207 } 207 208 } 208 rd.close();209 209 currentTrack.add(ps.waypoints); 210 210 data.tracks.add(new ImmutableGpxTrack(currentTrack, Collections.<String, Object>emptyMap())); 211 211 212 } catch ( finalIOException e) {212 } catch (IOException e) { 213 213 // TODO tell user about the problem? 214 } finally { 215 Utils.close(rd); 214 216 } 215 217 } -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r5635 r5874 625 625 bwr.flush(); 626 626 } 627 out.close();627 Utils.close(out); 628 628 } 629 629 -
trunk/src/org/openstreetmap/josm/io/OsmExporter.java
r5361 r5874 21 21 import org.openstreetmap.josm.gui.layer.Layer; 22 22 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 23 import org.openstreetmap.josm.tools.Utils; 23 24 24 25 public class OsmExporter extends FileExporter { … … 64 65 if (file.exists()) { 65 66 tmpFile = new File(file.getPath() + "~"); 66 copy(file, tmpFile);67 Utils.copyFile(file, tmpFile); 67 68 } 68 69 … … 75 76 try { 76 77 w.writeLayer(layer); 77 w.close();78 78 } finally { 79 Utils.close(w); 79 80 layer.data.getReadLock().unlock(); 80 81 } … … 99 100 // be deleted. So, restore the backup if we made one. 100 101 if (tmpFile != null && tmpFile.exists()) { 101 copy(tmpFile, file);102 Utils.copyFile(tmpFile, file); 102 103 } 103 104 } catch (IOException e2) { … … 112 113 } 113 114 } 114 115 private void copy(File src, File dst) throws IOException {116 FileInputStream srcStream;117 FileOutputStream dstStream;118 try {119 srcStream = new FileInputStream(src);120 dstStream = new FileOutputStream(dst);121 } catch (FileNotFoundException e) {122 JOptionPane.showMessageDialog(Main.parent, tr("Could not back up file. Exception is: {0}", e123 .getMessage()), tr("Error"), JOptionPane.ERROR_MESSAGE);124 return;125 }126 byte buf[] = new byte[1 << 16];127 int len;128 while ((len = srcStream.read(buf)) != -1) {129 dstStream.write(buf, 0, len);130 }131 srcStream.close();132 dstStream.close();133 }134 135 115 } -
trunk/src/org/openstreetmap/josm/io/OsmImporter.java
r5863 r5874 19 19 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 20 20 import org.openstreetmap.josm.gui.util.GuiHelper; 21 import org.openstreetmap.josm.tools.Utils; 21 22 22 23 public class OsmImporter extends FileImporter { … … 67 68 throw new IOException(tr("File ''{0}'' does not exist.", file.getName())); 68 69 } finally { 69 if (in != null) { 70 in.close(); 71 } 70 Utils.close(in); 72 71 } 73 72 } -
trunk/src/org/openstreetmap/josm/io/OsmServerBackreferenceReader.java
r5266 r5874 18 18 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 19 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 20 import org.openstreetmap.josm.tools.Utils; 20 21 21 22 /** 22 23 * OsmServerBackreferenceReader fetches the primitives from the OSM server which 23 * refer to a specific primitive. For a {@link Node}, ways and relations are retrieved24 * refer to a specific primitive. For a {@link org.openstreetmap.josm.data.osm.Node Node}, ways and relations are retrieved 24 25 * which refer to the node. For a {@link Way} or a {@link Relation}, only relations are 25 26 * read. … … 150 151 } finally { 151 152 progressMonitor.finishTask(); 152 if (in != null) { 153 try { 154 in.close(); 155 } catch(Exception e) {} 156 activeConnection = null; 157 } 153 Utils.close(in); 154 activeConnection = null; 158 155 } 159 156 } … … 188 185 } finally { 189 186 progressMonitor.finishTask(); 190 if (in != null) { 191 try { 192 in.close(); 193 } catch(Exception e) {} 194 activeConnection = null; 195 } 187 Utils.close(in); 188 activeConnection = null; 196 189 } 197 190 } -
trunk/src/org/openstreetmap/josm/io/OsmServerHistoryReader.java
r5835 r5874 12 12 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 13 13 import org.openstreetmap.josm.tools.CheckParameterUtil; 14 import org.openstreetmap.josm.tools.Utils; 14 15 15 16 /** … … 78 79 } finally { 79 80 progressMonitor.finishTask(); 80 if (in != null) { 81 try { 82 in.close(); 83 } catch(Exception e) {} 84 activeConnection = null; 85 } 81 Utils.close(in); 82 activeConnection = null; 86 83 } 87 84 } -
trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java
r5745 r5874 13 13 import org.openstreetmap.josm.data.osm.DataSet; 14 14 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 15 import org.openstreetmap.josm.tools.Utils; 15 16 import org.xml.sax.SAXException; 16 17 … … 40 41 } finally { 41 42 progressMonitor.finishTask(); 42 try { 43 activeConnection = null; 44 if (parser.in != null) { 45 parser.in.close(); 46 parser.in = null; 47 } 48 } catch(Exception e) {/* ignore it */} 43 activeConnection = null; 44 Utils.close(parser.in); 45 parser.in = null; 49 46 } 50 47 } -
trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java
r5266 r5874 15 15 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 16 16 import org.openstreetmap.josm.tools.CheckParameterUtil; 17 import org.openstreetmap.josm.tools.Utils; 17 18 import org.xml.sax.SAXException; 18 19 … … 144 145 } finally { 145 146 progressMonitor.finishTask(); 146 if (in!=null) { 147 try { 148 in.close(); 149 } catch(Exception e) {/* ignore this exception */} 150 } 147 Utils.close(in); 151 148 activeConnection = null; 152 149 } -
trunk/src/org/openstreetmap/josm/io/OsmWriter.java
r5737 r5874 71 71 header(null); 72 72 } 73 73 74 public void header(Boolean upload) { 74 75 out.println("<?xml version='1.0' encoding='UTF-8'?>"); … … 81 82 out.println("' generator='JOSM'>"); 82 83 } 84 83 85 public void footer() { 84 86 out.println("</osm>"); … … 313 315 } 314 316 } 315 316 public void close() {317 out.close();318 }319 320 @Override321 public void flush() {322 out.flush();323 }324 317 } -
trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java
r3005 r5874 49 49 50 50 @Override public void close() throws IOException { 51 in.close(); 52 progressMonitor.finishTask(); 51 try { 52 in.close(); 53 } finally { 54 progressMonitor.finishTask(); 55 } 53 56 } 54 57 -
trunk/src/org/openstreetmap/josm/io/WMSLayerExporter.java
r5459 r5874 10 10 import org.openstreetmap.josm.gui.layer.WMSLayer; 11 11 import org.openstreetmap.josm.tools.CheckParameterUtil; 12 import org.openstreetmap.josm.tools.Utils; 12 13 13 14 /** … … 34 35 ((WMSLayer)layer).writeExternal(oos); 35 36 } finally { 36 oos.close();37 Utils.close(oos); 37 38 } 38 39 } -
trunk/src/org/openstreetmap/josm/io/WMSLayerImporter.java
r5459 r5874 15 15 import org.openstreetmap.josm.gui.util.GuiHelper; 16 16 import org.openstreetmap.josm.tools.CheckParameterUtil; 17 import org.openstreetmap.josm.tools.Utils; 17 18 18 19 /** … … 55 56 throw new IllegalDataException(e); 56 57 } finally { 57 ois.close();58 Utils.close(ois); 58 59 } 59 60 -
trunk/src/org/openstreetmap/josm/io/XmlWriter.java
r5146 r5874 2 2 package org.openstreetmap.josm.io; 3 3 4 import java.io.Closeable; 5 import java.io.IOException; 4 6 import java.io.PrintWriter; 5 7 import java.util.HashMap; … … 10 12 * @author imi 11 13 */ 12 public class XmlWriter {14 public class XmlWriter implements Closeable { 13 15 14 protected PrintWriter out;16 protected final PrintWriter out; 15 17 16 18 public XmlWriter(PrintWriter out) { … … 18 20 } 19 21 22 /** 23 * Flushes the stream. 24 */ 20 25 public void flush() { 21 out.flush(); 26 if (out != null) { 27 out.flush(); 28 } 22 29 } 23 30 … … 69 76 encoding.put('\t', "	"); 70 77 } 78 79 @Override 80 public void close() throws IOException { 81 if (out != null) { 82 out.close(); 83 } 84 } 71 85 } -
trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java
r5819 r5874 178 178 Utils.copyStream(is, baos); 179 179 } finally { 180 is.close();180 Utils.close(is); 181 181 } 182 182 … … 201 201 return exception.toString(); 202 202 } finally { 203 br.close();203 Utils.close(br); 204 204 } 205 205 } -
trunk/src/org/openstreetmap/josm/io/session/MarkerSessionExporter.java
r5684 r5874 101 101 public MarkerWriter(PrintWriter out) { 102 102 super(out); 103 creator = "JOSM Marker export";104 103 } 105 104 -
trunk/src/org/openstreetmap/josm/io/session/SessionWriter.java
r5684 r5874 247 247 zipOut.putNextEntry(entry); 248 248 writeJos(doc, zipOut); 249 zipOut.close();249 Utils.close(zipOut); 250 250 } else { 251 251 writeJos(doc, new BufferedOutputStream(out)); -
trunk/src/org/openstreetmap/josm/plugins/Plugin.java
r5836 r5874 15 15 import org.openstreetmap.josm.gui.download.DownloadSelection; 16 16 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 17 import org.openstreetmap.josm.tools.Utils; 17 18 18 19 /** … … 110 111 pluginDir.mkdirs(); 111 112 } 112 FileOutputStream out = new FileOutputStream(new File(pluginDirName, to)); 113 InputStream in = getClass().getResourceAsStream(from); 114 byte[] buffer = new byte[8192]; 115 for(int len = in.read(buffer); len > 0; len = in.read(buffer)) { 116 out.write(buffer, 0, len); 113 FileOutputStream out = null; 114 InputStream in = null; 115 try { 116 out = new FileOutputStream(new File(pluginDirName, to)); 117 in = getClass().getResourceAsStream(from); 118 byte[] buffer = new byte[8192]; 119 for(int len = in.read(buffer); len > 0; len = in.read(buffer)) { 120 out.write(buffer, 0, len); 121 } 122 } finally { 123 Utils.close(in); 124 Utils.close(out); 117 125 } 118 in.close();119 out.close();120 126 } 121 127 -
trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java
r5836 r5874 129 129 out.write(buffer, 0, read); 130 130 } 131 out.close();132 in.close();133 131 } catch(MalformedURLException e) { 134 132 String msg = tr("Warning: Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", pi.name, pi.downloadlink); -
trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java
r5836 r5874 29 29 import org.openstreetmap.josm.tools.ImageProvider; 30 30 import org.openstreetmap.josm.tools.LanguageInfo; 31 import org.openstreetmap.josm.tools.Utils; 31 32 32 33 /** … … 100 101 throw new PluginException(name, e); 101 102 } finally { 102 if (jar != null) { 103 try { 104 jar.close(); 105 } catch(IOException e) { /* ignore */ } 106 } 107 if (fis != null) { 108 try { 109 fis.close(); 110 } catch(IOException e) { /* ignore */ } 111 } 103 Utils.close(jar); 104 Utils.close(fis); 112 105 } 113 106 } -
trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
r5836 r5874 218 218 out.write(buffer, 0, read); 219 219 } 220 out.close();221 in.close();222 220 } catch(MalformedURLException e) { 223 221 if (canceled) return; … … 229 227 return; 230 228 } finally { 229 Utils.close(out); 231 230 synchronized(this) { 232 231 if (connection != null) { … … 274 273 if (writer != null) { 275 274 writer.flush(); 276 writer.close();275 Utils.close(writer); 277 276 } 278 277 } -
trunk/src/org/openstreetmap/josm/tools/AudioPlayer.java
r5871 r5874 256 256 audioOutputLine.close(); 257 257 audioOutputLine = null; 258 audioInputStream.close();258 Utils.close(audioInputStream); 259 259 audioInputStream = null; 260 260 playingUrl = null; … … 277 277 { 278 278 if (audioInputStream != null) { 279 audioInputStream.close();279 Utils.close(audioInputStream); 280 280 audioInputStream = null; 281 281 } -
trunk/src/org/openstreetmap/josm/tools/AudioUtil.java
r5871 r5874 33 33 * audioFormat.getFrameSize() /* bytes per frame */; 34 34 double naturalLength = filesize / bytesPerSecond; 35 audioInputStream.close();35 Utils.close(audioInputStream); 36 36 double calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */); 37 37 return naturalLength / calibration; -
trunk/src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java
r5849 r5874 44 44 45 45 //http://stuffthathappens.com/blog/2007/10/15/one-more-note-on-uncaught-exception-handlers/ 46 /** 47 * Handles the given throwable object 48 * @param t The throwable object 49 */ 46 50 public void handle(Throwable t) { 47 51 handleException(t); 48 52 } 49 53 54 /** 55 * Handles the given exception 56 * @param e the exception 57 */ 50 58 public static void handleException(final Throwable e) { 51 59 if (handlingInProgress) … … 165 173 } 166 174 } 175 176 /** 177 * Determines if an exception is currently being handled 178 * @return {@code true} if an exception is currently being handled, {@code false} otherwise 179 */ 167 180 public static boolean exceptionHandlingInProgress() { 168 181 return handlingInProgress; … … 180 193 GZIPOutputStream gzip = new GZIPOutputStream(out); 181 194 gzip.write(debugText.getBytes("UTF-8")); 182 gzip.close();195 Utils.close(gzip); 183 196 184 197 return new URL("http://josm.openstreetmap.de/josmticket?" + -
trunk/src/org/openstreetmap/josm/tools/I18n.java
r5839 r5874 422 422 load(jar, jarTrans, true); 423 423 } 424 } 425 catch(IOException e) 426 { 427 } 428 finally 429 { 430 try 431 { 432 if(jar != null) 433 jar.close(); 434 if(fis != null) 435 fis.close(); 436 if(jarTrans != null) 437 jarTrans.close(); 438 if(fisTrans != null) 439 fisTrans.close(); 440 } 441 catch(IOException e) 442 { 443 } 424 } catch(IOException e) { 425 // Ignore 426 } finally { 427 Utils.close(jar); 428 Utils.close(fis); 429 Utils.close(jarTrans); 430 Utils.close(fisTrans); 444 431 } 445 432 } … … 482 469 // Ignore exception 483 470 } finally { 484 if (trStream != null) try {trStream.close();} catch(IOException e) {}485 if (enStream != null) try {enStream.close();} catch(IOException e) {}471 Utils.close(trStream); 472 Utils.close(enStream); 486 473 } 487 474 return false; -
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r5863 r5874 556 556 } 557 557 } finally { 558 if (is != null) { 559 is.close(); 560 } 558 Utils.close(is); 561 559 } 562 560 } … … 564 562 System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", archive.getName(), e.toString())); 565 563 } finally { 566 if (zipFile != null) { 567 try { 568 zipFile.close(); 569 } catch (IOException ex) { 570 } 571 } 564 Utils.close(zipFile); 572 565 } 573 566 return null; -
trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java
r5867 r5874 94 94 BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 95 95 String line = Utils.strip(input.readLine()); 96 input.close();96 Utils.close(input); 97 97 if (line != null && !line.isEmpty()) { 98 98 line = line.replaceAll("\"+",""); -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r5868 r5874 11 11 import java.awt.datatransfer.UnsupportedFlavorException; 12 12 import java.io.BufferedReader; 13 import java.io.Closeable; 13 14 import java.io.File; 15 import java.io.FileInputStream; 16 import java.io.FileOutputStream; 14 17 import java.io.IOException; 15 18 import java.io.InputStream; 16 19 import java.io.InputStreamReader; 17 20 import java.io.OutputStream; 18 import java.io.Reader;19 21 import java.io.UnsupportedEncodingException; 20 22 import java.net.HttpURLConnection; 21 23 import java.net.URL; 22 24 import java.net.URLConnection; 25 import java.nio.channels.FileChannel; 23 26 import java.security.MessageDigest; 24 27 import java.security.NoSuchAlgorithmException; … … 30 33 import java.util.Iterator; 31 34 import java.util.List; 35 import java.util.zip.ZipFile; 32 36 33 37 import org.openstreetmap.josm.Main; … … 237 241 } 238 242 243 /** 244 * Simple file copy function that will overwrite the target file.<br/> 245 * Taken from <a href="http://www.rgagnon.com/javadetails/java-0064.html">this article</a> (CC-NC-BY-SA) 246 * @param in The source file 247 * @param out The destination file 248 * @throws IOException If any I/O error occurs 249 */ 250 public static void copyFile(File in, File out) throws IOException { 251 // TODO: remove this function when we move to Java 7 (use Files.copy instead) 252 FileInputStream inStream = null; 253 FileOutputStream outStream = null; 254 try { 255 inStream = new FileInputStream(in); 256 outStream = new FileOutputStream(out); 257 FileChannel inChannel = inStream.getChannel(); 258 inChannel.transferTo(0, inChannel.size(), outStream.getChannel()); 259 } 260 catch (IOException e) { 261 throw e; 262 } 263 finally { 264 close(outStream); 265 close(inStream); 266 } 267 } 268 239 269 public static int copyStream(InputStream source, OutputStream destination) throws IOException { 240 270 int count = 0; … … 264 294 265 295 /** 266 * <p>Utility method for closing a n input stream.</p>296 * <p>Utility method for closing a {@link Closeable} object.</p> 267 297 * 268 * @param is the input stream. May be null.269 */ 270 public static void close( InputStream is){271 if ( is== null) return;298 * @param c the closeable object. May be null. 299 */ 300 public static void close(Closeable c) { 301 if (c == null) return; 272 302 try { 273 is.close();274 } catch(IOException e) {303 c.close(); 304 } catch(IOException e) { 275 305 // ignore 276 306 } 277 307 } 278 279 /** 280 * <p>Utility method for closing a n output stream.</p>308 309 /** 310 * <p>Utility method for closing a {@link ZipFile}.</p> 281 311 * 282 * @param os the output stream. May be null.283 */ 284 public static void close( OutputStream os){285 if ( os== null) return;312 * @param zip the zip file. May be null. 313 */ 314 public static void close(ZipFile zip) { 315 if (zip == null) return; 286 316 try { 287 os.close(); 288 } catch(IOException e){ 289 // ignore 290 } 291 } 292 293 /** 294 * <p>Utility method for closing a reader.</p> 295 * 296 * @param reader the reader. May be null. 297 */ 298 public static void close(Reader reader){ 299 if (reader == null) return; 300 try { 301 reader.close(); 302 } catch(IOException e){ 317 zip.close(); 318 } catch(IOException e) { 303 319 // ignore 304 320 } -
trunk/src/org/openstreetmap/josm/tools/WikiReader.java
r5868 r5874 4 4 import java.io.BufferedReader; 5 5 import java.io.IOException; 6 import java.io.InputStream;7 6 import java.net.URL; 8 7 … … 41 40 return readNormal(in); 42 41 } finally { 43 in.close();42 Utils.close(in); 44 43 } 45 44 } … … 63 62 return readFromTrac(in); 64 63 } finally { 65 in.close();64 Utils.close(in); 66 65 } 67 66 }
Note:
See TracChangeset
for help on using the changeset viewer.