Ignore:
Timestamp:
2016-01-11T18:03:13+01:00 (4 years ago)
Author:
simon04
Message:

see #8824 - Allow to cancel download of style/preset list in preferences

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r9353 r9411  
    55
    66import java.io.BufferedInputStream;
     7import java.io.BufferedReader;
     8import java.io.Closeable;
    79import java.io.File;
    810import java.io.FileInputStream;
     
    4143 * you can also get the mirrored copy with {@link #getFile()}.
    4244 */
    43 public class CachedFile {
     45public class CachedFile implements Closeable {
    4446
    4547    /**
     
    6769    protected CachingStrategy cachingStrategy;
    6870
     71    private transient HttpClient activeConnection;
    6972    protected File cacheFile;
    7073    protected boolean initialized;
     
    197200        }
    198201        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()));
    199215    }
    200216
     
    413429        destDirFile = new File(destDir, localPath + ".tmp");
    414430        try {
    415             final HttpClient.Response con = HttpClient.create(url)
     431            activeConnection = HttpClient.create(url)
    416432                    .setAccept(httpAccept)
    417433                    .setIfModifiedSince(ifModifiedSince == null ? 0L : ifModifiedSince)
    418                     .setHeaders(httpHeaders)
    419                     .connect();
     434                    .setHeaders(httpHeaders);
     435            final HttpClient.Response con = activeConnection.connect();
    420436            if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
    421437                if (Main.isDebugEnabled()) {
     
    431447                Files.copy(bis, destDirFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    432448            }
     449            activeConnection = null;
    433450            localFile = new File(destDir, localPath);
    434451            if (Main.platform.rename(destDirFile, localFile)) {
     
    456473    }
    457474
     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    }
    458486}
Note: See TracChangeset for help on using the changeset viewer.