1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.FileOutputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.io.OutputStream;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.util.zip.GZIPOutputStream;
|
---|
13 | import java.util.zip.ZipOutputStream;
|
---|
14 |
|
---|
15 | import org.apache.tools.bzip2.CBZip2OutputStream;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * An enum representing the compression type of a resource.
|
---|
20 | */
|
---|
21 | public enum Compression {
|
---|
22 | /**
|
---|
23 | * no compression
|
---|
24 | */
|
---|
25 | NONE,
|
---|
26 | /**
|
---|
27 | * bzip2 compression
|
---|
28 | */
|
---|
29 | BZIP2,
|
---|
30 | /**
|
---|
31 | * gzip compression
|
---|
32 | */
|
---|
33 | GZIP,
|
---|
34 | /**
|
---|
35 | * zip compression
|
---|
36 | */
|
---|
37 | ZIP;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Determines the compression type depending on the suffix of {@code name}.
|
---|
41 | * @param name File name including extension
|
---|
42 | * @return the compression type
|
---|
43 | */
|
---|
44 | public static Compression byExtension(String name) {
|
---|
45 | return name != null && name.endsWith(".gz")
|
---|
46 | ? GZIP
|
---|
47 | : name != null && (name.endsWith(".bz2") || name.endsWith(".bz"))
|
---|
48 | ? BZIP2
|
---|
49 | : name != null && name.endsWith(".zip")
|
---|
50 | ? ZIP
|
---|
51 | : NONE;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Returns an un-compressing {@link InputStream} for {@code in}.
|
---|
56 | *
|
---|
57 | * @throws IOException
|
---|
58 | */
|
---|
59 | public InputStream getUncompressedInputStream(InputStream in) throws IOException {
|
---|
60 | switch (this) {
|
---|
61 | case BZIP2:
|
---|
62 | return Utils.getBZip2InputStream(in);
|
---|
63 | case GZIP:
|
---|
64 | return Utils.getGZipInputStream(in);
|
---|
65 | case ZIP:
|
---|
66 | return Utils.getZipInputStream(in);
|
---|
67 | case NONE:
|
---|
68 | default:
|
---|
69 | return in;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
|
---|
75 | *
|
---|
76 | * @throws IOException
|
---|
77 | */
|
---|
78 | @SuppressWarnings("resource")
|
---|
79 | public static InputStream getUncompressedFileInputStream(File file) throws IOException {
|
---|
80 | return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Returns an un-compressing {@link InputStream} for the {@link URL} {@code url}.
|
---|
85 | *
|
---|
86 | * @throws IOException
|
---|
87 | */
|
---|
88 | public static InputStream getUncompressedURLInputStream(URL url) throws IOException {
|
---|
89 | return Utils.openURLAndDecompress(url, true);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Returns a compressing {@link OutputStream} for {@code out}.
|
---|
94 | *
|
---|
95 | * @throws IOException
|
---|
96 | */
|
---|
97 | public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
|
---|
98 | switch (this) {
|
---|
99 | case BZIP2:
|
---|
100 | out.write('B');
|
---|
101 | out.write('Z');
|
---|
102 | return new CBZip2OutputStream(out);
|
---|
103 | case GZIP:
|
---|
104 | return new GZIPOutputStream(out);
|
---|
105 | case ZIP:
|
---|
106 | return new ZipOutputStream(out, StandardCharsets.UTF_8);
|
---|
107 | case NONE:
|
---|
108 | default:
|
---|
109 | return out;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
|
---|
115 | *
|
---|
116 | * @throws IOException
|
---|
117 | */
|
---|
118 | @SuppressWarnings("resource")
|
---|
119 | public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
|
---|
120 | return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
|
---|
121 | }
|
---|
122 | }
|
---|