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

Last change on this file since 7070 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import org.apache.tools.bzip2.CBZip2OutputStream;
5import org.openstreetmap.josm.tools.Utils;
6
7import java.io.File;
8import java.io.FileInputStream;
9import java.io.FileOutputStream;
10import java.io.IOException;
11import java.io.InputStream;
12import java.io.OutputStream;
13import java.net.URL;
14import java.util.zip.GZIPOutputStream;
15import java.util.zip.ZipOutputStream;
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 * Returns an un-compressing {@link InputStream} for {@code in}.
55 *
56 * @throws IOException
57 */
58 public InputStream getUncompressedInputStream(InputStream in) throws IOException {
59 switch (this) {
60 case BZIP2:
61 return FileImporter.getBZip2InputStream(in);
62 case GZIP:
63 return FileImporter.getGZipInputStream(in);
64 case ZIP:
65 return FileImporter.getZipInputStream(in);
66 case NONE:
67 default:
68 return in;
69 }
70 }
71
72 /**
73 * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
74 *
75 * @throws IOException
76 */
77 @SuppressWarnings("resource")
78 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
79 return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
80 }
81
82 /**
83 * Returns an un-compressing {@link InputStream} for the {@link URL} {@code url}.
84 *
85 * @throws IOException
86 */
87 public static InputStream getUncompressedURLInputStream(URL url) throws IOException {
88 return Utils.openURLAndDecompress(url, true);
89 }
90
91 /**
92 * Returns a compressing {@link OutputStream} for {@code out}.
93 *
94 * @throws IOException
95 */
96 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
97 switch (this) {
98 case BZIP2:
99 out.write('B');
100 out.write('Z');
101 return new CBZip2OutputStream(out);
102 case GZIP:
103 return new GZIPOutputStream(out);
104 case ZIP:
105 return new ZipOutputStream(out);
106 case NONE:
107 default:
108 return out;
109 }
110 }
111
112 /**
113 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
114 *
115 * @throws IOException
116 */
117 @SuppressWarnings("resource")
118 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
119 return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
120 }
121}
Note: See TracBrowser for help on using the repository browser.