Description
Opens the given File to read it.
License
Open Source License
Parameter
Parameter | Description |
---|
file | File represents a file that should be opened to read |
Exception
Parameter | Description |
---|
IOException | will be thrown if any I/O errors occur |
Return
InputStream
Declaration
public static InputStream openStream(final File file) throws IOException
Method Source Code
//package com.java2s;
/*/* ww w . j ava 2 s . co m*/
* MIT License
*
* Copyright (c) 2016 EPAM Systems
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.zip.GZIPInputStream;
public class Main {
/**
* {@code List} specifies collection of file extensions that indicates that a file
* with one of these extensions can be considered as compressed resource.
*/
private static final List<String> GZIP_EXTENSIONS = Collections
.unmodifiableList(Collections.singletonList(".gz"));
/**
* Opens the given {@code File} to read it. If file name indicates that it's compressed,
* it's decompressed automatically.
*
* @param file {@code File} represents a file that should be opened to read
* @return {@code InputStream}
* @throws IOException will be thrown if any I/O errors occur
*/
public static InputStream openStream(final File file) throws IOException {
InputStream is = new FileInputStream(file);
if (isGZIPFile(file.getName())) {
return new GZIPInputStream(is);
}
return is;
}
/**
* Returns <tt>true</tt> if and only if the given file name has en extension that indicates that
* it's a GZIP resource, otherwise returns <tt>false</tt>.
*
* @param fn {@code String} represents a file name that should be tested
* @return boolean
*/
public static boolean isGZIPFile(final String fn) {
for (String ext : GZIP_EXTENSIONS) {
if (fn.endsWith(ext)) {
return true;
}
}
return false;
}
}
Related
- openInputStream(File file, boolean refuseEmpty)
- openInputStream(String filePath)
- openInputStream(String infilename)
- openInputStream(String path)
- openStream(final Class clazz, final String path)
- openStream(InputStream in)