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

Last change on this file since 8597 was 8470, checked in by Don-vip, 9 years ago

javadoc fixes. Removed one duplicated method in exception handling

  • Property svn:eol-style set to native
File size: 3.5 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.net.URL;
11import java.nio.charset.StandardCharsets;
12import java.util.zip.GZIPOutputStream;
13import java.util.zip.ZipOutputStream;
14
15import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
16import org.openstreetmap.josm.tools.Utils;
17
18/**
19 * An enum representing the compression type of a resource.
20 */
21public 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 if any I/O error occurs
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 if any I/O error occurs
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 if any I/O error occurs
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 if any I/O error occurs
96 */
97 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
98 switch (this) {
99 case BZIP2:
100 return new BZip2CompressorOutputStream(out);
101 case GZIP:
102 return new GZIPOutputStream(out);
103 case ZIP:
104 return new ZipOutputStream(out, StandardCharsets.UTF_8);
105 case NONE:
106 default:
107 return out;
108 }
109 }
110
111 /**
112 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
113 *
114 * @throws IOException if any I/O error occurs
115 */
116 @SuppressWarnings("resource")
117 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
118 return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
119 }
120}
Note: See TracBrowser for help on using the repository browser.