001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import java.io.File;
005import java.io.FileInputStream;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.io.InputStream;
009import java.io.OutputStream;
010import java.nio.charset.StandardCharsets;
011import java.util.zip.GZIPOutputStream;
012import java.util.zip.ZipOutputStream;
013
014import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
015import org.openstreetmap.josm.tools.Utils;
016
017/**
018 * An enum representing the compression type of a resource.
019 */
020public enum Compression {
021    /**
022     * no compression
023     */
024    NONE,
025    /**
026     * bzip2 compression
027     */
028    BZIP2,
029    /**
030     * gzip compression
031     */
032    GZIP,
033    /**
034     * zip compression
035     */
036    ZIP;
037
038    /**
039     * Determines the compression type depending on the suffix of {@code name}.
040     * @param name File name including extension
041     * @return the compression type
042     */
043    public static Compression byExtension(String name) {
044        return name != null && name.endsWith(".gz")
045                ? GZIP
046                : name != null && (name.endsWith(".bz2") || name.endsWith(".bz"))
047                ? BZIP2
048                : name != null && name.endsWith(".zip")
049                ? ZIP
050                : NONE;
051    }
052
053    /**
054     * Determines the compression type based on the content type (MIME type).
055     * @param contentType the content type
056     * @return the compression type
057     */
058    public static Compression forContentType(String contentType) {
059        switch (contentType) {
060        case "application/zip":
061            return ZIP;
062        case "application/x-gzip":
063            return GZIP;
064        case "application/x-bzip2":
065            return BZIP2;
066        default:
067            return NONE;
068        }
069    }
070
071    /**
072     * Returns an un-compressing {@link InputStream} for {@code in}.
073     * @param in raw input stream
074     * @return un-compressing input stream
075     *
076     * @throws IOException if any I/O error occurs
077     */
078    public InputStream getUncompressedInputStream(InputStream in) throws IOException {
079        switch (this) {
080            case BZIP2:
081                return Utils.getBZip2InputStream(in);
082            case GZIP:
083                return Utils.getGZipInputStream(in);
084            case ZIP:
085                return Utils.getZipInputStream(in);
086            case NONE:
087            default:
088                return in;
089        }
090    }
091
092    /**
093     * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
094     * @param file file
095     * @return un-compressing input stream
096     * @throws IOException if any I/O error occurs
097     */
098    @SuppressWarnings("resource")
099    public static InputStream getUncompressedFileInputStream(File file) throws IOException {
100        return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
101    }
102
103    /**
104     * Returns a compressing {@link OutputStream} for {@code out}.
105     * @param out raw output stream
106     * @return compressing output stream
107     *
108     * @throws IOException if any I/O error occurs
109     */
110    public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
111        switch (this) {
112            case BZIP2:
113                return new BZip2CompressorOutputStream(out);
114            case GZIP:
115                return new GZIPOutputStream(out);
116            case ZIP:
117                return new ZipOutputStream(out, StandardCharsets.UTF_8);
118            case NONE:
119            default:
120                return out;
121        }
122    }
123
124    /**
125     * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
126     * @param file file
127     * @return compressing output stream
128     *
129     * @throws IOException if any I/O error occurs
130     */
131    @SuppressWarnings("resource")
132    public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
133        return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
134    }
135}