source: josm/trunk/src/org/openstreetmap/josm/io/Compression.java@ 14159

Last change on this file since 14159 was 13838, checked in by Don-vip, 6 years ago

fix #16316 - catch InvalidPathException

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.OutputStream;
8import java.nio.charset.StandardCharsets;
9import java.nio.file.Files;
10import java.nio.file.InvalidPathException;
11import java.util.zip.GZIPInputStream;
12import java.util.zip.GZIPOutputStream;
13import java.util.zip.ZipEntry;
14import java.util.zip.ZipInputStream;
15import java.util.zip.ZipOutputStream;
16
17import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
18import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
19import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
20import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
21import org.openstreetmap.josm.tools.Logging;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * An enum representing the compression type of a resource.
26 */
27public enum Compression {
28 /**
29 * no compression
30 */
31 NONE,
32 /**
33 * bzip2 compression
34 */
35 BZIP2,
36 /**
37 * gzip compression
38 */
39 GZIP,
40 /**
41 * zip compression
42 */
43 ZIP,
44 /**
45 * xz compression
46 */
47 XZ;
48
49 /**
50 * Determines the compression type depending on the suffix of {@code name}.
51 * @param name File name including extension
52 * @return the compression type
53 */
54 public static Compression byExtension(String name) {
55 return name != null && name.endsWith(".gz")
56 ? GZIP
57 : name != null && (name.endsWith(".bz2") || name.endsWith(".bz"))
58 ? BZIP2
59 : name != null && name.endsWith(".zip")
60 ? ZIP
61 : name != null && name.endsWith(".xz")
62 ? XZ
63 : NONE;
64 }
65
66 /**
67 * Determines the compression type based on the content type (MIME type).
68 * @param contentType the content type
69 * @return the compression type
70 */
71 public static Compression forContentType(String contentType) {
72 switch (contentType) {
73 case "application/zip":
74 return ZIP;
75 case "application/x-gzip":
76 return GZIP;
77 case "application/x-bzip2":
78 return BZIP2;
79 case "application/x-xz":
80 return XZ;
81 default:
82 return NONE;
83 }
84 }
85
86 /**
87 * Returns an un-compressing {@link InputStream} for {@code in}.
88 * @param in raw input stream
89 * @return un-compressing input stream
90 *
91 * @throws IOException if any I/O error occurs
92 */
93 public InputStream getUncompressedInputStream(InputStream in) throws IOException {
94 switch (this) {
95 case BZIP2:
96 return getBZip2InputStream(in);
97 case GZIP:
98 return getGZipInputStream(in);
99 case ZIP:
100 return getZipInputStream(in);
101 case XZ:
102 return getXZInputStream(in);
103 case NONE:
104 default:
105 return in;
106 }
107 }
108
109 /**
110 * Returns a XZ input stream wrapping given input stream.
111 * @param in The raw input stream
112 * @return a XZ input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
113 * @throws IOException if the given input stream does not contain valid BZ2 header
114 * @since 13350
115 */
116 public static XZCompressorInputStream getXZInputStream(InputStream in) throws IOException {
117 if (in == null) {
118 return null;
119 }
120 return new XZCompressorInputStream(in, true);
121 }
122
123 /**
124 * Returns a Bzip2 input stream wrapping given input stream.
125 * @param in The raw input stream
126 * @return a Bzip2 input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
127 * @throws IOException if the given input stream does not contain valid BZ2 header
128 * @since 12772 (moved from {@link Utils}, there since 7867)
129 */
130 public static BZip2CompressorInputStream getBZip2InputStream(InputStream in) throws IOException {
131 if (in == null) {
132 return null;
133 }
134 return new BZip2CompressorInputStream(in, /* see #9537 */ true);
135 }
136
137 /**
138 * Returns a Gzip input stream wrapping given input stream.
139 * @param in The raw input stream
140 * @return a Gzip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
141 * @throws IOException if an I/O error has occurred
142 * @since 12772 (moved from {@link Utils}, there since 7119)
143 */
144 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
145 if (in == null) {
146 return null;
147 }
148 return new GZIPInputStream(in);
149 }
150
151 /**
152 * Returns a Zip input stream wrapping given input stream.
153 * @param in The raw input stream
154 * @return a Zip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
155 * @throws IOException if an I/O error has occurred
156 * @since 12772 (moved from {@link Utils}, there since 7119)
157 */
158 public static ZipInputStream getZipInputStream(InputStream in) throws IOException {
159 if (in == null) {
160 return null;
161 }
162 ZipInputStream zis = new ZipInputStream(in, StandardCharsets.UTF_8);
163 // Positions the stream at the beginning of first entry
164 ZipEntry ze = zis.getNextEntry();
165 if (ze != null && Logging.isDebugEnabled()) {
166 Logging.debug("Zip entry: {0}", ze.getName());
167 }
168 return zis;
169 }
170
171 /**
172 * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
173 * @param file file
174 * @return un-compressing input stream
175 * @throws IOException if any I/O error occurs
176 */
177 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
178 InputStream in = Files.newInputStream(file.toPath());
179 try {
180 return byExtension(file.getName()).getUncompressedInputStream(in);
181 } catch (IOException e) {
182 Utils.close(in);
183 throw e;
184 }
185 }
186
187 /**
188 * Returns a compressing {@link OutputStream} for {@code out}.
189 * @param out raw output stream
190 * @return compressing output stream
191 *
192 * @throws IOException if any I/O error occurs
193 */
194 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
195 switch (this) {
196 case BZIP2:
197 return new BZip2CompressorOutputStream(out);
198 case GZIP:
199 return new GZIPOutputStream(out);
200 case ZIP:
201 return new ZipOutputStream(out, StandardCharsets.UTF_8);
202 case XZ:
203 return new XZCompressorOutputStream(out);
204 case NONE:
205 default:
206 return out;
207 }
208 }
209
210 /**
211 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
212 * @param file file
213 * @return compressing output stream
214 *
215 * @throws IOException if any I/O error occurs
216 * @throws InvalidPathException if a Path object cannot be constructed from the abstract path
217 */
218 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
219 OutputStream out = Files.newOutputStream(file.toPath());
220 try {
221 return byExtension(file.getName()).getCompressedOutputStream(out);
222 } catch (IOException e) {
223 Utils.close(out);
224 throw e;
225 }
226 }
227}
Note: See TracBrowser for help on using the repository browser.