1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.OutputStream;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.util.zip.GZIPOutputStream;
|
---|
13 | import java.util.zip.ZipOutputStream;
|
---|
14 |
|
---|
15 | import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * An enum representing the compression type of a resource.
|
---|
20 | */
|
---|
21 | public enum Compression {
|
---|
22 | /**
|
---|
23 | * no compression
|
---|
24 | */
|
---|
25 | NONE,
|
---|
26 | /**
|
---|
27 | * bzip2 compression
|
---|
28 | */
|
---|
29 | BZIP2,
|
---|
30 | /**
|
---|
31 | * gzip compression
|
---|
32 | */
|
---|
33 | GZIP,
|
---|
34 | /**
|
---|
35 | * zip compression
|
---|
36 | */
|
---|
37 | ZIP;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Determines the compression type depending on the suffix of {@code name}.
|
---|
41 | * @param name File name including extension
|
---|
42 | * @return the compression type
|
---|
43 | */
|
---|
44 | public static Compression byExtension(String name) {
|
---|
45 | return name != null && name.endsWith(".gz")
|
---|
46 | ? GZIP
|
---|
47 | : name != null && (name.endsWith(".bz2") || name.endsWith(".bz"))
|
---|
48 | ? BZIP2
|
---|
49 | : name != null && name.endsWith(".zip")
|
---|
50 | ? ZIP
|
---|
51 | : NONE;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Determines the compression type based on the content type (MIME type).
|
---|
56 | * @param contentType the content type
|
---|
57 | * @return the compression type
|
---|
58 | */
|
---|
59 | public static Compression forContentType(String contentType) {
|
---|
60 | switch (contentType) {
|
---|
61 | case "application/zip":
|
---|
62 | return ZIP;
|
---|
63 | case "application/x-gzip":
|
---|
64 | return GZIP;
|
---|
65 | case "application/x-bzip2":
|
---|
66 | return BZIP2;
|
---|
67 | default:
|
---|
68 | return NONE;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Returns an un-compressing {@link InputStream} for {@code in}.
|
---|
74 | * @param in raw input stream
|
---|
75 | * @return un-compressing input stream
|
---|
76 | *
|
---|
77 | * @throws IOException if any I/O error occurs
|
---|
78 | */
|
---|
79 | public InputStream getUncompressedInputStream(InputStream in) throws IOException {
|
---|
80 | switch (this) {
|
---|
81 | case BZIP2:
|
---|
82 | return Utils.getBZip2InputStream(in);
|
---|
83 | case GZIP:
|
---|
84 | return Utils.getGZipInputStream(in);
|
---|
85 | case ZIP:
|
---|
86 | return Utils.getZipInputStream(in);
|
---|
87 | case NONE:
|
---|
88 | default:
|
---|
89 | return in;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
|
---|
95 | * @param file file
|
---|
96 | * @return un-compressing input stream
|
---|
97 | * @throws IOException if any I/O error occurs
|
---|
98 | */
|
---|
99 | @SuppressWarnings("resource")
|
---|
100 | public static InputStream getUncompressedFileInputStream(File file) throws IOException {
|
---|
101 | return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Returns an un-compressing {@link InputStream} for the {@link URL} {@code url}.
|
---|
106 | * @param url URL
|
---|
107 | * @return un-compressing input stream
|
---|
108 | *
|
---|
109 | * @throws IOException if any I/O error occurs
|
---|
110 | * @deprecated Use {@link org.openstreetmap.josm.tools.HttpClient} instead
|
---|
111 | */
|
---|
112 | @Deprecated
|
---|
113 | public static InputStream getUncompressedURLInputStream(URL url) throws IOException {
|
---|
114 | return Utils.openURLAndDecompress(url, true);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Returns a compressing {@link OutputStream} for {@code out}.
|
---|
119 | * @param out raw output stream
|
---|
120 | * @return compressing output stream
|
---|
121 | *
|
---|
122 | * @throws IOException if any I/O error occurs
|
---|
123 | */
|
---|
124 | public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
|
---|
125 | switch (this) {
|
---|
126 | case BZIP2:
|
---|
127 | return new BZip2CompressorOutputStream(out);
|
---|
128 | case GZIP:
|
---|
129 | return new GZIPOutputStream(out);
|
---|
130 | case ZIP:
|
---|
131 | return new ZipOutputStream(out, StandardCharsets.UTF_8);
|
---|
132 | case NONE:
|
---|
133 | default:
|
---|
134 | return out;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
|
---|
140 | * @param file file
|
---|
141 | * @return compressing output stream
|
---|
142 | *
|
---|
143 | * @throws IOException if any I/O error occurs
|
---|
144 | */
|
---|
145 | @SuppressWarnings("resource")
|
---|
146 | public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
|
---|
147 | return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
|
---|
148 | }
|
---|
149 | }
|
---|