Class Reader
java.lang.Object
org.opentrafficsim.base.compressedfiles.Reader
public final class Reader
extends java.lang.Object
Reader for compressed files.
Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
- Version:
- $Revision$, $LastChangedDate$, by $Author$, initial version Oct 25, 2018
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Method Summary
Modifier and Type Method Description static CompressionType
autoDetectCompressionType(java.lang.String fileName)
Determine the type of compression used in a file.static java.io.InputStream
createInputStream(java.lang.String fileName)
Construct a InputStream for a compressed data file.static java.io.InputStream
createInputStream(java.lang.String fileName, CompressionType compressionType)
Construct a InputStream for a compressed data file.static boolean
isGZipCompressed(byte[] bytes)
Determine if bytes match the GZip compression signature.
-
Method Details
-
createInputStream
public static java.io.InputStream createInputStream(java.lang.String fileName, CompressionType compressionType) throws java.io.IOExceptionConstruct a InputStream for a compressed data file.- Parameters:
fileName
- String; the name of the filecompressionType
- CompressionType; the expected type of the data compression in the file- Returns:
- InputStream that can yield the expanded content of the file.
- Throws:
java.io.IOException
- when the file could not be read
-
createInputStream
public static java.io.InputStream createInputStream(java.lang.String fileName) throws java.io.IOExceptionConstruct a InputStream for a compressed data file. The type of compression is auto-detected.- Parameters:
fileName
- String; the name of the file- Returns:
- InputStream that can yield the expanded content of the file.
- Throws:
java.io.IOException
- when the file can not be opened or read
-
autoDetectCompressionType
public static CompressionType autoDetectCompressionType(java.lang.String fileName) throws java.io.IOExceptionDetermine the type of compression used in a file.Derived from http://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped.
Gzip inflate an inputStream (if it is indeed gzip compressed), otherwise return an InputStream that yields the same data as the input argument.- Parameters:
fileName
- String; the name of the file to check- Returns:
- InputStream yielding the inflated data
- Throws:
java.io.IOException
- when errors occur reading the signature bytes
-
isGZipCompressed
public static boolean isGZipCompressed(byte[] bytes) throws java.io.IOExceptionDetermine if bytes match the GZip compression signature. Derived from http://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped.
Determines if a byte array is compressed. The java.util.zip GZip implementation does not expose the GZip header so it is difficult to determine if a string is compressed.- Parameters:
bytes
- byte[]; at least 2 bytes from the start of the stream to determine compression type- Returns:
- boolean; true if the data appears to be GZip compressed; false otherwise
- Throws:
java.io.IOException
- if the byte array couldn't be read
-