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

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

remove deprecated stuff

  • Property svn:eol-style set to native
File size: 4.0 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 @SuppressWarnings("resource")
99 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
100 return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
101 }
102
103 /**
104 * Returns a compressing {@link OutputStream} for {@code out}.
105 * @param out raw output stream
106 * @return compressing output stream
107 *
108 * @throws IOException if any I/O error occurs
109 */
110 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
111 switch (this) {
112 case BZIP2:
113 return new BZip2CompressorOutputStream(out);
114 case GZIP:
115 return new GZIPOutputStream(out);
116 case ZIP:
117 return new ZipOutputStream(out, StandardCharsets.UTF_8);
118 case NONE:
119 default:
120 return out;
121 }
122 }
123
124 /**
125 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
126 * @param file file
127 * @return compressing output stream
128 *
129 * @throws IOException if any I/O error occurs
130 */
131 @SuppressWarnings("resource")
132 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
133 return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
134 }
135}
Note: See TracBrowser for help on using the repository browser.