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

Last change on this file since 8961 was 8929, checked in by Don-vip, 8 years ago

javadoc fixes

  • Property svn:eol-style set to native
File size: 3.9 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 * @param in raw input stream
57 * @return un-compressing input stream
58 *
59 * @throws IOException if any I/O error occurs
60 */
61 public InputStream getUncompressedInputStream(InputStream in) throws IOException {
62 switch (this) {
63 case BZIP2:
64 return Utils.getBZip2InputStream(in);
65 case GZIP:
66 return Utils.getGZipInputStream(in);
67 case ZIP:
68 return Utils.getZipInputStream(in);
69 case NONE:
70 default:
71 return in;
72 }
73 }
74
75 /**
76 * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
77 * @param file file
78 * @return un-compressing input stream
79 * @throws IOException if any I/O error occurs
80 */
81 @SuppressWarnings("resource")
82 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
83 return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
84 }
85
86 /**
87 * Returns an un-compressing {@link InputStream} for the {@link URL} {@code url}.
88 * @param url URL
89 * @return un-compressing input stream
90 *
91 * @throws IOException if any I/O error occurs
92 */
93 public static InputStream getUncompressedURLInputStream(URL url) throws IOException {
94 return Utils.openURLAndDecompress(url, true);
95 }
96
97 /**
98 * Returns a compressing {@link OutputStream} for {@code out}.
99 * @param out raw output stream
100 * @return compressing output stream
101 *
102 * @throws IOException if any I/O error occurs
103 */
104 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
105 switch (this) {
106 case BZIP2:
107 return new BZip2CompressorOutputStream(out);
108 case GZIP:
109 return new GZIPOutputStream(out);
110 case ZIP:
111 return new ZipOutputStream(out, StandardCharsets.UTF_8);
112 case NONE:
113 default:
114 return out;
115 }
116 }
117
118 /**
119 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
120 * @param file file
121 * @return compressing output stream
122 *
123 * @throws IOException if any I/O error occurs
124 */
125 @SuppressWarnings("resource")
126 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
127 return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
128 }
129}
Note: See TracBrowser for help on using the repository browser.