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

Last change on this file since 11114 was 10223, checked in by Don-vip, 8 years ago

findbugs: DP_DO_INSIDE_DO_PRIVILEGED + UWF_UNWRITTEN_FIELD + RC_REF_COMPARISON + OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE

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