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

Last change on this file since 6840 was 6716, checked in by simon04, 10 years ago

Allow to read/write .gpx.bz2 files, refactor compression handling

File size: 2.9 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;
15
16/**
17 * An enum representing the compression type of a resource.
18 */
19public enum Compression {
20 /**
21 * no compression
22 */
23 NONE,
24 /**
25 * bzip2 compression
26 */
27 BZIP2,
28 /**
29 * gzip compression
30 */
31 GZIP;
32
33 /**
34 * Determines the compression type depending on the suffix of {@code name}.
35 */
36 public static Compression byExtension(String name) {
37 return name != null && name.endsWith(".gz")
38 ? GZIP
39 : name != null && (name.endsWith(".bz2") || name.endsWith(".bz"))
40 ? BZIP2
41 : NONE;
42 }
43
44 /**
45 * Returns an un-compressing {@link InputStream} for {@code in}.
46 *
47 * @throws IOException
48 */
49 public InputStream getUncompressedInputStream(InputStream in) throws IOException {
50 switch (this) {
51 case BZIP2:
52 return FileImporter.getBZip2InputStream(in);
53 case GZIP:
54 return FileImporter.getGZipInputStream(in);
55 case NONE:
56 default:
57 return in;
58 }
59 }
60
61 /**
62 * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
63 *
64 * @throws IOException
65 */
66 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
67 return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
68 }
69
70 /**
71 * Returns an un-compressing {@link InputStream} for the {@link URL} {@code url}.
72 *
73 * @throws IOException
74 */
75 public static InputStream getUncompressedURLInputStream(URL url) throws IOException {
76 return Utils.openURLAndDecompress(url, true);
77 }
78
79 /**
80 * Returns a compressing {@link OutputStream} for {@code out}.
81 *
82 * @throws IOException
83 */
84 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
85 switch (this) {
86 case BZIP2:
87 out.write('B');
88 out.write('Z');
89 return new CBZip2OutputStream(out);
90 case GZIP:
91 return new GZIPOutputStream(out);
92 case NONE:
93 default:
94 return out;
95 }
96 }
97
98 /**
99 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
100 *
101 * @throws IOException
102 */
103 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
104 return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
105 }
106}
Note: See TracBrowser for help on using the repository browser.