Changeset 7033 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2014-05-01T02:34:43+02:00 (10 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/AutosaveTask.java
r7029 r7033 157 157 try { 158 158 if (result.createNewFile()) { 159 try { 160 File pidFile = new File(autosaveDir, filename+".pid"); 161 PrintStream ps = new PrintStream(pidFile, "UTF-8"); 159 File pidFile = new File(autosaveDir, filename+".pid"); 160 try (PrintStream ps = new PrintStream(pidFile, "UTF-8")) { 162 161 ps.println(ManagementFactory.getRuntimeMXBean().getName()); 163 Utils.close(ps);164 162 } catch (Throwable t) { 165 163 Main.error(t); … … 305 303 File pidFile = getPidFile(file); 306 304 if (pidFile.exists()) { 307 try { 308 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pidFile), Utils.UTF_8)); 305 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pidFile), Utils.UTF_8))) { 309 306 try { 310 307 String jvmId = reader.readLine(); -
trunk/src/org/openstreetmap/josm/data/CustomConfigurator.java
r7027 r7033 427 427 String fileDir = file.getParentFile().getAbsolutePath(); 428 428 if (fileDir!=null) engine.eval("scriptDir='"+normalizeDirName(fileDir) +"';"); 429 openAndReadXML(new BufferedInputStream(new FileInputStream(file))); 429 try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { 430 openAndReadXML(is); 431 } 430 432 } catch (Exception ex) { 431 433 log("Error reading custom preferences: " + ex.getMessage()); … … 445 447 } catch (Exception ex) { 446 448 log("Error reading custom preferences: "+ex.getMessage()); 447 } finally {448 Utils.close(is);449 449 } 450 450 log("-- Reading complete --"); … … 460 460 engine.eval("homeDir='"+normalizeDirName(Main.pref.getPreferencesDir()) +"';"); 461 461 engine.eval("josmVersion="+Version.getInstance().getVersion()+";"); 462 String className = 462 String className = CustomConfigurator.class.getName(); 463 463 engine.eval("API.messageBox="+className+".messageBox"); 464 464 engine.eval("API.askText=function(text) { return String("+className+".askForText(text));}"); -
trunk/src/org/openstreetmap/josm/data/Preferences.java
r7027 r7033 11 11 import java.io.FileOutputStream; 12 12 import java.io.IOException; 13 import java.io.InputStream; 13 14 import java.io.InputStreamReader; 14 15 import java.io.OutputStreamWriter; … … 719 720 720 721 /** 721 * Called after every put. In case of a problem, do nothing but output the error 722 * in log. 722 * Called after every put. In case of a problem, do nothing but output the error in log. 723 723 */ 724 724 public void save() throws IOException { … … 736 736 } 737 737 738 finalPrintWriter out = new PrintWriter(new OutputStreamWriter(739 new FileOutputStream(prefFile + "_tmp"), Utils.UTF_8), false) ;740 out.print(toXML(false));741 Utils.close(out);738 try (PrintWriter out = new PrintWriter(new OutputStreamWriter( 739 new FileOutputStream(prefFile + "_tmp"), Utils.UTF_8), false)) { 740 out.print(toXML(false)); 741 } 742 742 743 743 File tmpFile = new File(prefFile + "_tmp"); … … 748 748 setCorrectPermissions(backupFile); 749 749 } 750 751 750 752 751 private void setCorrectPermissions(File file) { … … 761 760 settingsMap.clear(); 762 761 File pref = getPreferenceFile(); 763 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), Utils.UTF_8)); 764 try { 762 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), Utils.UTF_8))) { 765 763 validateXML(in); 766 Utils.close(in);767 in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), Utils.UTF_8));764 } 765 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), Utils.UTF_8))) { 768 766 fromXML(in); 769 } finally {770 Utils.close(in);771 767 } 772 768 updateSystemProperties(); … … 1359 1355 public void validateXML(Reader in) throws Exception { 1360 1356 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 1361 Schema schema = factory.newSchema(new StreamSource(new MirroredInputStream("resource://data/preferences.xsd"))); 1362 Validator validator = schema.newValidator(); 1363 validator.validate(new StreamSource(in)); 1357 try (InputStream xsdStream = new MirroredInputStream("resource://data/preferences.xsd")) { 1358 Schema schema = factory.newSchema(new StreamSource(xsdStream)); 1359 Validator validator = schema.newValidator(); 1360 validator.validate(new StreamSource(in)); 1361 } 1364 1362 } 1365 1363 -
trunk/src/org/openstreetmap/josm/data/Version.java
r7005 r7033 38 38 String s = null; 39 39 try { 40 BufferedReader in = Utils.openURLReader(resource);41 40 StringBuilder sb = new StringBuilder(); 42 try {41 try (BufferedReader in = Utils.openURLReader(resource)) { 43 42 for (String line = in.readLine(); line != null; line = in.readLine()) { 44 43 sb.append(line).append("\n"); 45 44 } 46 } finally {47 Utils.close(in);48 45 } 49 46 s = sb.toString(); -
trunk/src/org/openstreetmap/josm/data/imagery/WmsCache.java
r7028 r7033 111 111 private String getCacheDirectory(String url) { 112 112 String cacheDirName = null; 113 InputStream fis = null; 114 OutputStream fos = null; 115 try { 116 Properties layersIndex = new Properties(); 117 File layerIndexFile = new File(cacheDirPath(), LAYERS_INDEX_FILENAME); 118 try { 119 fis = new FileInputStream(layerIndexFile); 120 layersIndex.load(fis); 121 } catch (FileNotFoundException e) { 122 Main.error("Unable to load layers index for wms cache (file " + layerIndexFile + " not found)"); 113 Properties layersIndex = new Properties(); 114 File layerIndexFile = new File(cacheDirPath(), LAYERS_INDEX_FILENAME); 115 try (InputStream fis = new FileInputStream(layerIndexFile)) { 116 layersIndex.load(fis); 117 } catch (FileNotFoundException e) { 118 Main.error("Unable to load layers index for wms cache (file " + layerIndexFile + " not found)"); 119 } catch (IOException e) { 120 Main.error("Unable to load layers index for wms cache"); 121 Main.error(e); 122 } 123 124 for (Object propKey: layersIndex.keySet()) { 125 String s = (String)propKey; 126 if (url.equals(layersIndex.getProperty(s))) { 127 cacheDirName = s; 128 break; 129 } 130 } 131 132 if (cacheDirName == null) { 133 int counter = 0; 134 while (true) { 135 counter++; 136 if (!layersIndex.keySet().contains(String.valueOf(counter))) { 137 break; 138 } 139 } 140 cacheDirName = String.valueOf(counter); 141 layersIndex.setProperty(cacheDirName, url); 142 try (OutputStream fos = new FileOutputStream(layerIndexFile)) { 143 layersIndex.store(fos, ""); 123 144 } catch (IOException e) { 124 Main.error("Unable to load layersindex for wms cache");145 Main.error("Unable to save layer index for wms cache"); 125 146 Main.error(e); 126 147 } 127 128 for (Object propKey: layersIndex.keySet()) {129 String s = (String)propKey;130 if (url.equals(layersIndex.getProperty(s))) {131 cacheDirName = s;132 break;133 }134 }135 136 if (cacheDirName == null) {137 int counter = 0;138 while (true) {139 counter++;140 if (!layersIndex.keySet().contains(String.valueOf(counter))) {141 break;142 }143 }144 cacheDirName = String.valueOf(counter);145 layersIndex.setProperty(cacheDirName, url);146 try {147 fos = new FileOutputStream(layerIndexFile);148 layersIndex.store(fos, "");149 } catch (IOException e) {150 Main.error("Unable to save layer index for wms cache");151 Main.error(e);152 }153 }154 } finally {155 Utils.close(fos);156 Utils.close(fis);157 148 } 158 149 … … 181 172 WmsCacheType.class.getClassLoader()); 182 173 Unmarshaller unmarshaller = context.createUnmarshaller(); 183 WmsCacheType cacheEntries = (WmsCacheType)unmarshaller.unmarshal(new FileInputStream(indexFile)); 174 WmsCacheType cacheEntries; 175 try (InputStream is = new FileInputStream(indexFile)) { 176 cacheEntries = (WmsCacheType)unmarshaller.unmarshal(is); 177 } 184 178 totalFileSize = cacheEntries.getTotalFileSize(); 185 179 if (cacheEntries.getTileSize() != tileSize) { … … 292 286 WmsCacheType.class.getClassLoader()); 293 287 Marshaller marshaller = context.createMarshaller(); 294 marshaller.marshal(index, new FileOutputStream(new File(cacheDir, INDEX_FILENAME))); 288 try (OutputStream fos = new FileOutputStream(new File(cacheDir, INDEX_FILENAME))) { 289 marshaller.marshal(index, fos); 290 } 295 291 } catch (Exception e) { 296 292 Main.error("Failed to save wms-cache file"); … … 302 298 return new File(cacheDir, projection.cacheDirectory + "/" + entry.filename); 303 299 } 304 305 300 306 301 private BufferedImage loadImage(ProjectionEntries projectionEntries, CacheEntry entry) throws IOException { … … 524 519 totalFileSize += imageFile.length(); 525 520 } else { 526 OutputStream os = new BufferedOutputStream(new FileOutputStream(imageFile)); 527 try { 521 try (OutputStream os = new BufferedOutputStream(new FileOutputStream(imageFile))) { 528 522 totalFileSize += Utils.copyStream(imageData, os); 529 } finally {530 Utils.close(os);531 523 } 532 524 } -
trunk/src/org/openstreetmap/josm/data/projection/Projections.java
r7005 r7033 132 132 private static void loadInits() { 133 133 Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>"); 134 BufferedReader r = null; 135 try { 134 try ( 136 135 InputStream in = new MirroredInputStream("resource://data/projection/epsg"); 137 r = new BufferedReader(new InputStreamReader(in, Utils.UTF_8)); 136 BufferedReader r = new BufferedReader(new InputStreamReader(in, Utils.UTF_8)); 137 ) { 138 138 String line, lastline = ""; 139 139 while ((line = r.readLine()) != null) { 140 140 line = line.trim(); 141 141 if (!line.startsWith("#") && !line.isEmpty()) { 142 if (!lastline.startsWith("#")) throw new AssertionError( );142 if (!lastline.startsWith("#")) throw new AssertionError("EPSG file seems corrupted"); 143 143 String name = lastline.substring(1).trim(); 144 144 Matcher m = epsgPattern.matcher(line); … … 153 153 } catch (IOException ex) { 154 154 throw new RuntimeException(ex); 155 } finally {156 Utils.close(r);157 155 } 158 156 } -
trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFileWrapper.java
r6883 r7033 48 48 public NTV2GridShiftFile getShiftFile() { 49 49 if (instance == null) { 50 try { 51 InputStream is = new MirroredInputStream(gridFileName); 50 try (InputStream is = new MirroredInputStream(gridFileName)) { 52 51 instance = new NTV2GridShiftFile(); 53 52 instance.loadGridShiftFile(is, false); … … 58 57 return instance; 59 58 } 60 61 59 } -
trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java
r7005 r7033 171 171 File file = new File(getValidatorDir() + "ignorederrors"); 172 172 if (file.exists()) { 173 BufferedReader in = null; 174 try { 175 in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Utils.UTF_8)); 173 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Utils.UTF_8))) { 176 174 for (String line = in.readLine(); line != null; line = in.readLine()) { 177 175 ignoredErrors.add(line); … … 181 179 } catch (final IOException e) { 182 180 Main.error(e); 183 } finally {184 Utils.close(in);185 181 } 186 182 } … … 197 193 198 194 public static void saveIgnoredErrors() { 199 PrintWriter out = null; 200 try { 201 out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(getValidatorDir() + "ignorederrors"), Utils.UTF_8), false); 195 try (PrintWriter out = new PrintWriter(new OutputStreamWriter( 196 new FileOutputStream(getValidatorDir() + "ignorederrors"), Utils.UTF_8), false)) { 202 197 for (String e : ignoredErrors) { 203 198 out.println(e); … … 205 200 } catch (IOException e) { 206 201 Main.error(e); 207 } finally {208 Utils.close(out);209 202 } 210 203 } -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r7027 r7033 453 453 Main.info(tr("Adding {0} to tag checker", i)); 454 454 } 455 MirroredInputStream s = new MirroredInputStream(i); 456 try { 455 try (MirroredInputStream s = new MirroredInputStream(i)) { 457 456 addMapCSS(new BufferedReader(UTFInputStreamReader.create(s))); 458 } finally {459 Utils.close(s);460 457 } 461 458 } catch (IOException ex) { -
trunk/src/org/openstreetmap/josm/data/validation/tests/OpeningHourTest.java
r7005 r7033 5 5 6 6 import java.io.InputStreamReader; 7 import java.io.Reader; 7 8 import java.util.ArrayList; 8 9 import java.util.Arrays; … … 51 52 super.initialize(); 52 53 if (ENGINE != null) { 53 ENGINE.eval(new InputStreamReader(new MirroredInputStream("resource://data/validator/opening_hours.js"), Utils.UTF_8)); 54 // fake country/state to not get errors on holidays 55 ENGINE.eval("var nominatimJSON = {address: {state: 'Bayern', country_code: 'de'}};"); 56 ENGINE.eval( 57 "var oh = function (value, mode) {" + 58 " try {" + 59 " var r= new opening_hours(value, nominatimJSON, mode);" + 60 " r.getErrors = function() {return [];};" + 61 " return r;" + 62 " } catch(err) {" + 63 " return {" + 64 " getWarnings: function() {return [];}," + 65 " getErrors: function() {return [err.toString()]}" + 66 " };" + 67 " }" + 68 "};"); 54 try (Reader reader = new InputStreamReader( 55 new MirroredInputStream("resource://data/validator/opening_hours.js"), Utils.UTF_8)) { 56 ENGINE.eval(reader); 57 // fake country/state to not get errors on holidays 58 ENGINE.eval("var nominatimJSON = {address: {state: 'Bayern', country_code: 'de'}};"); 59 ENGINE.eval( 60 "var oh = function (value, mode) {" + 61 " try {" + 62 " var r= new opening_hours(value, nominatimJSON, mode);" + 63 " r.getErrors = function() {return [];};" + 64 " return r;" + 65 " } catch(err) {" + 66 " return {" + 67 " getWarnings: function() {return [];}," + 68 " getErrors: function() {return [err.toString()]}" + 69 " };" + 70 " }" + 71 "};"); 72 } 69 73 } else { 70 74 Main.warn("Unable to initialize OpeningHourTest because no JavaScript engine has been found"); -
trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
r7025 r7033 54 54 import org.openstreetmap.josm.tools.GBC; 55 55 import org.openstreetmap.josm.tools.MultiMap; 56 import org.openstreetmap.josm.tools.Utils;57 56 58 57 /** … … 165 164 String errorSources = ""; 166 165 for (String source : Main.pref.getCollection(PREF_SOURCES, DEFAULT_SOURCES)) { 167 BufferedReader reader = null; 168 try { 166 try ( 169 167 MirroredInputStream s = new MirroredInputStream(source); 170 reader = new BufferedReader(UTFInputStreamReader.create(s));171 168 BufferedReader reader = new BufferedReader(UTFInputStreamReader.create(s)); 169 ) { 172 170 String okValue = null; 173 171 boolean tagcheckerfile = false; … … 242 240 } catch (IOException e) { 243 241 errorSources += source + "\n"; 244 } finally {245 Utils.close(reader);246 242 } 247 243 }
Note:
See TracChangeset
for help on using the changeset viewer.