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

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

enable new PMD rule AvoidFileStream - see https://pmd.github.io/pmd-6.0.0/pmd_rules_java_performance.html#avoidfilestream / https://bugs.openjdk.java.net/browse/JDK-8080225 for details

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