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