Ignore:
Timestamp:
2013-11-28T23:33:10+01:00 (10 years ago)
Author:
simon04
Message:

see #9341 - provide utility methods to open URL + decompress stream if needed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6410 r6421  
    1313import java.awt.datatransfer.Transferable;
    1414import java.awt.datatransfer.UnsupportedFlavorException;
     15import java.io.BufferedInputStream;
    1516import java.io.BufferedReader;
    1617import java.io.Closeable;
     
    3738import java.util.Iterator;
    3839import java.util.List;
     40import java.util.zip.GZIPInputStream;
    3941import java.util.zip.ZipFile;
    4042
     43import org.apache.tools.bzip2.CBZip2InputStream;
    4144import org.openstreetmap.josm.Main;
    4245import org.openstreetmap.josm.data.Version;
     46import org.openstreetmap.josm.io.FileImporter;
    4347
    4448/**
     
    638642     */
    639643    public static InputStream openURL(URL url) throws IOException {
    640         return setupURLConnection(url.openConnection()).getInputStream();
     644        return openURLAndDecompress(url, false);
     645    }
     646
     647    /**
     648     * Opens a connection to the given URL, sets the User-Agent property to JOSM's one, and decompresses stream if necessary.
     649     * @param url The url to open
     650     * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link CBZip2InputStream}
     651     *                   if the {@code Content-Type} header is set accordingly.
     652     * @return An stream for the given URL
     653     * @throws IOException if an I/O exception occurs.
     654     * @since 6421
     655     */
     656    public static InputStream openURLAndDecompress(final URL url, final boolean decompress) throws IOException {
     657        final URLConnection connection = setupURLConnection(url.openConnection());
     658        if (decompress && "application/x-gzip".equals(connection.getHeaderField("Content-Type"))) {
     659            return new GZIPInputStream(connection.getInputStream());
     660        } else if (decompress && "application/x-bzip2".equals(connection.getHeaderField("Content-Type"))) {
     661            return FileImporter.getBZip2InputStream(new BufferedInputStream(connection.getInputStream()));
     662        } else {
     663            return connection.getInputStream();
     664        }
    641665    }
    642666
     
    664688     */
    665689    public static BufferedReader openURLReader(URL url) throws IOException {
    666         return new BufferedReader(new InputStreamReader(openURL(url), "utf-8"));
     690        return openURLReaderAndDecompress(url, false);
     691    }
     692
     693    /**
     694     * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
     695     * @param url The url to open
     696     * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link CBZip2InputStream}
     697     *                   if the {@code Content-Type} header is set accordingly.
     698     * @return An buffered stream reader for the given URL (using UTF-8)
     699     * @throws IOException if an I/O exception occurs.
     700     * @since 6421
     701     */
     702    public static BufferedReader openURLReaderAndDecompress(final URL url, final boolean decompress) throws IOException {
     703        return new BufferedReader(new InputStreamReader(openURLAndDecompress(url, decompress), "utf-8"));
    667704    }
    668705
     
    670707     * Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive.
    671708     * @param httpURL The HTTP url to open (must use http:// or https://)
    672      * @param keepAlive
     709     * @param keepAlive whether not to set header {@code Connection=close}
    673710     * @return An open HTTP connection to the given URL
    674711     * @throws IOException if an I/O exception occurs.
Note: See TracChangeset for help on using the changeset viewer.