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

Last change on this file since 10716 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
RevLine 
[6716]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;
[7089]10import java.nio.charset.StandardCharsets;
[6716]11import java.util.zip.GZIPOutputStream;
[6882]12import java.util.zip.ZipOutputStream;
[6716]13
[7867]14import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
[7089]15import org.openstreetmap.josm.tools.Utils;
16
[6716]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 */
[6882]32 GZIP,
33 /**
34 * zip compression
35 */
36 ZIP;
[6716]37
38 /**
39 * Determines the compression type depending on the suffix of {@code name}.
[6882]40 * @param name File name including extension
41 * @return the compression type
[6716]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
[6882]48 : name != null && name.endsWith(".zip")
49 ? ZIP
[6716]50 : NONE;
51 }
52
53 /**
[9169]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 /**
[6716]72 * Returns an un-compressing {@link InputStream} for {@code in}.
[8929]73 * @param in raw input stream
74 * @return un-compressing input stream
[6716]75 *
[8470]76 * @throws IOException if any I/O error occurs
[6716]77 */
78 public InputStream getUncompressedInputStream(InputStream in) throws IOException {
79 switch (this) {
80 case BZIP2:
[7119]81 return Utils.getBZip2InputStream(in);
[6716]82 case GZIP:
[7119]83 return Utils.getGZipInputStream(in);
[6882]84 case ZIP:
[7119]85 return Utils.getZipInputStream(in);
[6716]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}.
[8929]94 * @param file file
95 * @return un-compressing input stream
[8470]96 * @throws IOException if any I/O error occurs
[6716]97 */
98 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
[10223]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 }
[6716]106 }
107
108 /**
109 * Returns a compressing {@link OutputStream} for {@code out}.
[8929]110 * @param out raw output stream
111 * @return compressing output stream
[6716]112 *
[8470]113 * @throws IOException if any I/O error occurs
[6716]114 */
115 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
116 switch (this) {
117 case BZIP2:
[7867]118 return new BZip2CompressorOutputStream(out);
[6716]119 case GZIP:
120 return new GZIPOutputStream(out);
[6882]121 case ZIP:
[7089]122 return new ZipOutputStream(out, StandardCharsets.UTF_8);
[6716]123 case NONE:
124 default:
125 return out;
126 }
127 }
128
129 /**
130 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
[8929]131 * @param file file
132 * @return compressing output stream
[6716]133 *
[8470]134 * @throws IOException if any I/O error occurs
[6716]135 */
136 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
[10223]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 }
[6716]144 }
145}
Note: See TracBrowser for help on using the repository browser.