Changeset 9411 in josm
- Timestamp:
- 2016-01-11T18:03:13+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
r9334 r9411 1319 1319 private final String url; 1320 1320 private final List<SourceProvider> sourceProviders; 1321 private BufferedReader reader;1322 private boolean canceled;1321 private transient CachedFile cachedFile; 1322 private transient boolean canceled; 1323 1323 private final List<ExtendedSourceEntry> sources = new ArrayList<>(); 1324 1324 … … 1332 1332 protected void cancel() { 1333 1333 canceled = true; 1334 Utils.close( reader);1334 Utils.close(cachedFile); 1335 1335 } 1336 1336 … … 1356 1356 @Override 1357 1357 protected void realRun() throws SAXException, IOException, OsmTransferException { 1358 String lang = LanguageInfo.getLanguageCodeXML();1359 1358 try { 1360 1359 sources.addAll(getDefault()); … … 1367 1366 } 1368 1367 } 1369 1370 InputStream stream = new CachedFile(url).getInputStream(); 1371 reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); 1368 readFile(); 1369 } catch (IOException e) { 1370 if (canceled) 1371 // ignore the exception and return 1372 return; 1373 OsmTransferException ex = new OsmTransferException(e); 1374 ex.setUrl(url); 1375 warn(ex); 1376 } 1377 } 1378 1379 protected void readFile() throws IOException { 1380 final String lang = LanguageInfo.getLanguageCodeXML(); 1381 cachedFile = new CachedFile(url); 1382 try (final BufferedReader reader = cachedFile.getContentReader()) { 1372 1383 1373 1384 String line; … … 1434 1445 } 1435 1446 } 1436 } catch (IOException e) {1437 if (canceled)1438 // ignore the exception and return1439 return;1440 OsmTransferException ex = new OsmTransferException(e);1441 ex.setUrl(url);1442 warn(ex);1443 return;1444 1447 } 1445 1448 } -
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r9353 r9411 5 5 6 6 import java.io.BufferedInputStream; 7 import java.io.BufferedReader; 8 import java.io.Closeable; 7 9 import java.io.File; 8 10 import java.io.FileInputStream; … … 41 43 * you can also get the mirrored copy with {@link #getFile()}. 42 44 */ 43 public class CachedFile {45 public class CachedFile implements Closeable { 44 46 45 47 /** … … 67 69 protected CachingStrategy cachingStrategy; 68 70 71 private transient HttpClient activeConnection; 69 72 protected File cacheFile; 70 73 protected boolean initialized; … … 197 200 } 198 201 return new FileInputStream(file); 202 } 203 204 /** 205 * Returns {@link #getInputStream()} wrapped in a buffered reader. 206 * <p/> 207 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}. 208 * 209 * @return buffered reader 210 * @throws IOException if any I/O error occurs 211 * @since 9411 212 */ 213 public BufferedReader getContentReader() throws IOException { 214 return new BufferedReader(UTFInputStreamReader.create(getInputStream())); 199 215 } 200 216 … … 413 429 destDirFile = new File(destDir, localPath + ".tmp"); 414 430 try { 415 final HttpClient.Response con = HttpClient.create(url)431 activeConnection = HttpClient.create(url) 416 432 .setAccept(httpAccept) 417 433 .setIfModifiedSince(ifModifiedSince == null ? 0L : ifModifiedSince) 418 .setHeaders(httpHeaders) 419 434 .setHeaders(httpHeaders); 435 final HttpClient.Response con = activeConnection.connect(); 420 436 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 421 437 if (Main.isDebugEnabled()) { … … 431 447 Files.copy(bis, destDirFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 432 448 } 449 activeConnection = null; 433 450 localFile = new File(destDir, localPath); 434 451 if (Main.platform.rename(destDirFile, localFile)) { … … 456 473 } 457 474 475 /** 476 * Attempts to disconnect an URL connection. 477 * @see HttpClient#disconnect() 478 * @since 9411 479 */ 480 @Override 481 public void close() { 482 if (activeConnection != null) { 483 activeConnection.disconnect(); 484 } 485 } 458 486 }
Note:
See TracChangeset
for help on using the changeset viewer.