Ignore:
Timestamp:
2014-05-14T00:51:20+02:00 (10 years ago)
Author:
Don-vip
Message:

code refactoring/cleanup/javadoc + fix bug in preset text comparator in menu

File:
1 edited

Legend:

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

    r7083 r7119  
    4545import java.util.regex.Pattern;
    4646import java.util.zip.GZIPInputStream;
     47import java.util.zip.ZipEntry;
    4748import java.util.zip.ZipFile;
     49import java.util.zip.ZipInputStream;
    4850
    4951import org.apache.tools.bzip2.CBZip2InputStream;
    5052import org.openstreetmap.josm.Main;
    5153import org.openstreetmap.josm.data.Version;
    52 import org.openstreetmap.josm.io.FileImporter;
    5354
    5455/**
     
    665666    public static InputStream openURLAndDecompress(final URL url, final boolean decompress) throws IOException {
    666667        final URLConnection connection = setupURLConnection(url.openConnection());
    667         if (decompress && "application/x-gzip".equals(connection.getHeaderField("Content-Type"))) {
    668             return new GZIPInputStream(connection.getInputStream());
    669         } else if (decompress && "application/x-bzip2".equals(connection.getHeaderField("Content-Type"))) {
    670             return FileImporter.getBZip2InputStream(new BufferedInputStream(connection.getInputStream()));
    671         } else {
    672             return connection.getInputStream();
    673         }
     668        final InputStream in = connection.getInputStream();
     669        if (decompress) {
     670            switch (connection.getHeaderField("Content-Type")) {
     671            case "application/zip":
     672                return getZipInputStream(in);
     673            case "application/x-gzip":
     674                return getGZipInputStream(in);
     675            case "application/x-bzip2":
     676                return getBZip2InputStream(in);
     677            }
     678        }
     679        return in;
     680    }
     681
     682    /**
     683     * Returns a Bzip2 input stream wrapping given input stream.
     684     * @param in The raw input stream
     685     * @return a Bzip2 input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
     686     * @throws IOException if the given input stream does not contain valid BZ2 header
     687     * @since 7119
     688     */
     689    public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {
     690        if (in == null) {
     691            return null;
     692        }
     693        BufferedInputStream bis = new BufferedInputStream(in);
     694        int b = bis.read();
     695        if (b != 'B')
     696            throw new IOException(tr("Invalid bz2 file."));
     697        b = bis.read();
     698        if (b != 'Z')
     699            throw new IOException(tr("Invalid bz2 file."));
     700        return new CBZip2InputStream(bis, /* see #9537 */ true);
     701    }
     702
     703    /**
     704     * Returns a Gzip input stream wrapping given input stream.
     705     * @param in The raw input stream
     706     * @return a Gzip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
     707     * @throws IOException if an I/O error has occurred
     708     * @since 7119
     709     */
     710    public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
     711        if (in == null) {
     712            return null;
     713        }
     714        return new GZIPInputStream(in);
     715    }
     716
     717    /**
     718     * Returns a Zip input stream wrapping given input stream.
     719     * @param in The raw input stream
     720     * @return a Zip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
     721     * @throws IOException if an I/O error has occurred
     722     * @since 7119
     723     */
     724    public static ZipInputStream getZipInputStream(InputStream in) throws IOException {
     725        if (in == null) {
     726            return null;
     727        }
     728        ZipInputStream zis = new ZipInputStream(in, StandardCharsets.UTF_8);
     729        // Positions the stream at the beginning of first entry
     730        ZipEntry ze = zis.getNextEntry();
     731        if (ze != null && Main.isDebugEnabled()) {
     732            Main.debug("Zip entry: "+ze.getName());
     733        }
     734        return zis;
    674735    }
    675736
Note: See TracChangeset for help on using the changeset viewer.