Changeset 7033 in josm for trunk/src/org/openstreetmap/josm/data/imagery/WmsCache.java
- Timestamp:
- 2014-05-01T02:34:43+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.