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