Changeset 6421 in josm


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

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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Version.java

    r6317 r6421  
    236236        }
    237237        String result = "JOSM/1.5 ("+ s+" "+LanguageInfo.getJOSMLocaleCode()+")";
    238         if (includeOsDetails) {
     238        if (includeOsDetails && Main.platform != null) {
    239239            result += " " + Main.platform.getOSDescription();
    240240        }
  • 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.
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r6410 r6421  
    44import org.junit.Assert;
    55import org.junit.Test;
     6import org.openstreetmap.josm.Main;
     7import org.openstreetmap.josm.data.Preferences;
     8
     9import java.io.BufferedReader;
     10import java.net.URL;
    611
    712/**
     
    6671        Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
    6772    }
     73
     74    @Test
     75    public void testOpenUrlGzip() throws Exception {
     76        Main.pref = new Preferences();
     77        final BufferedReader x = Utils.openURLReaderAndDecompress(new URL("http://www.openstreetmap.org/trace/1613906/data"), true);
     78        Assert.assertTrue(x.readLine().startsWith("<?xml version="));
     79        x.close();
     80    }
     81
     82    @Test
     83    public void testOpenUrlBzip() throws Exception {
     84        Main.pref = new Preferences();
     85        final BufferedReader x = Utils.openURLReaderAndDecompress(new URL("http://www.openstreetmap.org/trace/785544/data"), true);
     86        Assert.assertTrue(x.readLine().startsWith("<?xml version="));
     87        x.close();
     88    }
    6889}
Note: See TracChangeset for help on using the changeset viewer.