1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.compress;
25
26 import static com.google.common.base.Preconditions.checkNotNull;
27
28 import java.io.BufferedInputStream;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.InputStream;
32 import java.io.IOException;
33
34 import javax.annotation.Nullable;
35
36 import com.google.common.io.Files;
37
38 import htsjdk.samtools.util.BlockCompressedInputStream;
39
40 import org.apache.commons.compress.compressors.bzip2.BZip2Utils;
41
42 import org.apache.commons.compress.compressors.gzip.GzipUtils;
43
44
45
46
47
48
49
50 public final class Compress
51 {
52
53
54
55
56 private Compress()
57 {
58
59 }
60
61
62
63
64
65
66
67
68
69 public static boolean isBgzfFilename(final String fileName)
70 {
71 checkNotNull(fileName);
72 return BgzfUtils.isCompressedFilename(fileName);
73 }
74
75
76
77
78
79
80
81 public static boolean isBgzfFile(@Nullable final File file)
82 {
83 if (file == null)
84 {
85 return false;
86 }
87 if (isBgzfFilename(file.getName()))
88 {
89 return true;
90 }
91
92 try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)))
93 {
94 return isBgzfInputStream(inputStream);
95 }
96 catch (IOException e)
97 {
98 return false;
99 }
100 }
101
102
103
104
105
106
107
108 public static boolean isBgzfInputStream(final InputStream inputStream)
109 {
110 checkNotNull(inputStream);
111 BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream);
112 try
113 {
114 return BlockCompressedInputStream.isValidFile(bufferedInputStream);
115 }
116 catch (IOException e)
117 {
118 return false;
119 }
120 }
121
122
123
124
125
126
127
128
129 public static boolean isGzipFilename(final String fileName)
130 {
131 checkNotNull(fileName);
132 return GzipUtils.isCompressedFilename(fileName);
133 }
134
135
136
137
138
139
140
141
142
143 public static boolean isGzipFile(@Nullable final File file)
144 {
145 if (file == null)
146 {
147 return false;
148 }
149 return isGzipFilename(file.getName());
150 }
151
152
153
154
155
156
157
158
159 public static boolean isGzipInputStream(final InputStream inputStream)
160 {
161 checkNotNull(inputStream);
162 InputStream in = inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream);
163 try
164 {
165 in.mark(2);
166 return in.read() == 31 && in.read() == 139;
167 }
168 catch (IOException e)
169 {
170 return false;
171 }
172 finally
173 {
174 try
175 {
176 in.reset();
177 }
178 catch (IOException e)
179 {
180
181 }
182 }
183 }
184
185
186
187
188
189
190
191
192 public static boolean isBzip2Filename(final String fileName)
193 {
194 return BZip2Utils.isCompressedFilename(fileName);
195 }
196
197
198
199
200
201
202
203 public static boolean isBzip2File(@Nullable final File file)
204 {
205 if (file == null)
206 {
207 return false;
208 }
209 return isBzip2Filename(file.getName());
210 }
211
212
213
214
215
216
217
218
219 public static boolean isBzip2InputStream(final InputStream inputStream)
220 {
221 checkNotNull(inputStream);
222 InputStream in = inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream);
223 try
224 {
225 in.mark(3);
226 return in.read() == 'B' && in.read() == 'Z' && in.read() == 'h';
227 }
228 catch (IOException e)
229 {
230 return false;
231 }
232 finally
233 {
234 try
235 {
236 in.reset();
237 }
238 catch (IOException e)
239 {
240
241 }
242 }
243 }
244 }