Repository navigation
Expand file tree
/
Copy pathInputStreamProvider.java
More file actions
295 lines (227 loc) · 6.77 KB
/
Copy pathInputStreamProvider.java
File metadata and controls
295 lines (227 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
* Created on Dec 28, 2005
*
*/
package org.biojava.nbio.core.util;
import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
/** A class that provides an InputStream from a File. The file can be compressed or uncompressed.
*
* Currently supported
* compressions:
* <ul>
* <li>Gzip (extension .gz)</li>
* <li>Zip (extension .zip) in this case a stream to the first entry in the zip file is returned </li>
* <li>Jar (extension .jar) same as .Zip; only stream to first entry is returned </li>
* <li>Z (extension .Z) compressed using the unix compress command </li>
* <li>for any other extension, no compression is assumed </li>
* </ul>
*
*
* @author Andreas Prlic
* @since 1.5
* @version %I% %G%
*
*/
public class InputStreamProvider {
//private final static Logger logger = LoggerFactory.getLogger(InputStreamProvider.class);
/**
* The magic number found at the start of a GZIP stream.
*/
public static final int GZIP_MAGIC = 0x1f8b;
public static final String CACHE_PROPERTY = "biojava.cache.files";
private boolean cacheRawFiles ;
public InputStreamProvider() {
super();
cacheRawFiles = false;
String prop = System.getProperty(CACHE_PROPERTY);
if ( prop != null && prop.equals("true")) {
cacheRawFiles = true;
}
}
/**
* Get an InputStream for given file path.
* The caller is responsible for closing the stream or otherwise
* a resource leak can occur.
* @param pathToFile the path of the file.
* @return an InputStream for the file located at the path.
* @throws IOException
*/
public InputStream getInputStream(String pathToFile)
throws IOException
{
File f = new File(pathToFile);
return getInputStream(f);
}
/** open the file and read the magic number from the beginning
* this is used to determine the compression type
*
* @param in an input stream to read from
* @return the magic number
* @throws IOException
*/
private int getMagicNumber(InputStream in)
throws IOException {
int t = in.read();
if (t < 0) throw new EOFException("Failed to read magic number");
int magic = (t & 0xff) << 8;
t = in.read();
if (t < 0) throw new EOFException("Failed to read magic number");
magic += t & 0xff;
return magic;
}
public InputStream getInputStream(URL u)
throws IOException{
int magic = 0;
InputStream inStream = u.openStream();
magic = getMagicNumber(inStream);
inStream.close();
if (magic == UncompressInputStream.LZW_MAGIC ) {
// a Z compressed file
return openCompressedURL(u);
} else if (magic == GZIP_MAGIC ) {
return openGZIPURL(u);
} else if ( u.getPath().endsWith(".gz")) {
return openGZIPURL(u);
} else if ( u.getPath().endsWith(".Z")) {
// unix compressed
return openCompressedURL(u);
} else {
inStream = u.openStream();
return inStream;
}
}
/**
* Get an InputStream for the file.
* The caller is responsible for closing the stream or otherwise
* a resource leak can occur.
* @param f a File
* @return an InputStream for the file
* @throws IOException
*/
public InputStream getInputStream(File f)
throws IOException
{
// use the magic numbers to determine the compression type,
// use file extension only as 2nd choice
int magic = 0;
InputStream test = getInputStreamFromFile(f);
magic = getMagicNumber(test);
test.close();
InputStream inputStream = null;
String fileName = f.getName();
if (magic == UncompressInputStream.LZW_MAGIC ) {
// a Z compressed file
return openCompressedFile(f);
}
else if (magic == GZIP_MAGIC ) {
return openGZIPFile(f);
}
else if ( fileName.endsWith(".gz")) {
return openGZIPFile(f);
}
else if ( fileName.endsWith(".zip")){
ZipFile zipfile = new ZipFile(f);
// stream to first entry is returned ...
ZipEntry entry;
Enumeration<? extends ZipEntry> e = zipfile.entries();
if ( e.hasMoreElements()){
entry = e.nextElement();
inputStream = zipfile.getInputStream(entry);
} else {
throw new IOException ("Zip file has no entries");
}
}
else if ( fileName.endsWith(".jar")) {
JarFile jarFile = new JarFile(f);
// stream to first entry is returned
JarEntry entry;
Enumeration<JarEntry> e = jarFile.entries();
if ( e.hasMoreElements()){
entry = e.nextElement();
inputStream = jarFile.getInputStream(entry);
} else {
throw new IOException ("Jar file has no entries");
}
}
else if ( fileName.endsWith(".Z")) {
// unix compressed
return openCompressedFile(f);
}
else {
// no particular extension found, assume that it is an uncompressed file
inputStream = getInputStreamFromFile(f);
}
return inputStream;
}
/**
* Wrapper for new FileInputStream. if System.property biojava.cache.files is set, will try to load files from memory cache.
*
* @param f
* @return
* @throws FileNotFoundException
*/
private InputStream getInputStreamFromFile(File f) throws FileNotFoundException{
InputStream stream = null;
if ( cacheRawFiles ){
stream = FlatFileCache.getInputStream(f.getAbsolutePath());
if ( stream == null){
FlatFileCache.addToCache(f.getAbsolutePath(),f);
stream = FlatFileCache.getInputStream(f.getAbsolutePath());
}
}
if ( stream == null)
stream = new FileInputStream(f);
return stream;
}
private InputStream openCompressedFile(File f)
throws IOException{
InputStream is = getInputStreamFromFile(f);
InputStream inputStream = new UncompressInputStream(is);
return inputStream;
}
private InputStream openCompressedURL(URL u)
throws IOException{
InputStream is = u.openStream();
InputStream inputStream = new UncompressInputStream(is);
return inputStream;
}
private InputStream openGZIPFile(File f)
throws IOException{
InputStream is = getInputStreamFromFile(f);
InputStream inputStream = new GZIPInputStream(is);
return inputStream;
}
private InputStream openGZIPURL(URL u)
throws IOException{
InputStream is = u.openStream();
InputStream inputStream = new GZIPInputStream(is);
return inputStream;
}
}