Package com.luciad.io
Class TLcdIOUtil
java.lang.Object
com.luciad.io.TLcdIOUtil
IO utility class to facilitate the handling of URLs, absolute file paths or paths relative to the
classpath. This class has methods to:
retrieve an NIO Path
for an absolute file or file URI, or a file path relative to the class path- Detect if a source name should be handled as a file String or URL. For this, create a new TLcdIOUtil instance,
configure a source
, and check thegetFileName()
andgetURL()
methods. extract the directory
path from a source.extract the file name
without the path from a source.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic URI
Returns a URI for the given source.static boolean
Returns the property AsFileFirst.static String
getDirectoryPath
(String aFullPathName) Returns the path of a source (URL or file).Returns the file name value.static String
getFileName
(String aFullPathName) Return the file name of a source (URL or file) without the path.static String
getFilenameWithoutExtension
(String aFullPathName, String... aExtensions) Return the file name of a source (URL or file) without the path.getURL()
Returns the URL value.static boolean
Return the property UseCacheDefault.static boolean
Returns the property UseNIO.boolean
Deprecated.This method has been deprecated.static Path
Returns an NIO Path for the given file source.Deprecated.TLcdInputStreamFactory offers more configuration options and better performancestatic void
setAsFileFirst
(boolean aAsFileFirst) Sets the property AsFileFirst.static void
setClassTraceOn
(boolean aClassTraceOn) Deprecated.This method has been deprecated.void
setSourceName
(String aSourceName) Set the source where the data should be loaded from.void
setTraceOn
(boolean aTraceOn) Deprecated.This method has been deprecated.static void
setUseCacheDefault
(boolean aUseCacheDefault) Sets the property UseCacheDefault.static void
setUseNIO
(boolean aUseNIO) Sets the property UseNIO.static byte[]
toByteArray
(InputStream aInputStream) Converts the givenInputStream
to a byte array.
-
Constructor Details
-
TLcdIOUtil
public TLcdIOUtil()Creates a IO utility.
-
-
Method Details
-
setClassTraceOn
public static void setClassTraceOn(boolean aClassTraceOn) Deprecated.This method has been deprecated. It is recommended to use the standard Java logging framework directly.Enables tracing for all instances of this class. If the argument istrue
then all log messages are recorded, otherwise only the informative, warning and error messages are recorded.- Parameters:
aClassTraceOn
- if true then all log messages are recorded, otherwise only the informative, warning and error messages are recorded.
-
setTraceOn
public void setTraceOn(boolean aTraceOn) Deprecated.This method has been deprecated. It is recommended to use the standard Java logging framework directly.Enables tracing for this class instance. Calling this method with eithertrue
orfalse
as argument automatically turns off tracing for all other class instances for whichsetTraceOn
has not been called. If the argument isfalse
then only the informative, warning and error log messages are recorded.- Parameters:
aTraceOn
- if true then all log messages are recorded for this instance. If false, then only the informative, warning and error log messages are recorded.
-
isTraceOn
public boolean isTraceOn()Deprecated.This method has been deprecated. It is recommended to use the standard Java logging framework directly.Returnstrue
if tracing is enabled for this class.- Returns:
- true if tracing is enabled for this class, false otherwise.
-
resolve
Returns an NIO Path for the given file source. This works for an absolute file path, a file URI, or a path relative to the classpath, but not for a JAR resource. Paths relative to the working directory are not resolved.- Parameters:
aSourceName
- a file URI, absolute file path or path relative to the classpath- Returns:
- a Path, or null if the source could not be parsed as a file path or valid file URI
- Since:
- 2017.0
-
createURI
Returns a URI for the given source. The source does not need to exist for this method to work.
This method works for a variety of inputs. For example:
- absolute file paths
- URLs (file, http, etc...)
- paths relative to the class path or the current directory
It also does an effort to correct incorrect input strings, for example:
- file:////xxx
- file:/mnt/server/xxx
- Parameters:
aSourceName
- the source name- Returns:
- a URI, or
null
if the URI could not be create. - Since:
- 2017.1
-
setSourceName
Set the source where the data should be loaded from. This can be a URL, an absolute file path or a path relative to the classpath.- Parameters:
aSourceName
- the source to retrieve data from.- Throws:
NullPointerException
- when the source is null.
-
retrieveInputStream
Deprecated.TLcdInputStreamFactory offers more configuration options and better performanceReturns an input stream for theconfigured source
. Source names ending in .zip or .gz are unzipped automatically.The caller of this method is responsible to close the input stream returned by this method when it is no longer needed. Failing to do so may lock system resources which will eventually break down the system.
- Returns:
- an InputStream to the resource set in setSourceName.
- Throws:
IOException
- when an I/O error occurs.- See Also:
-
getURL
Returns the URL value.- Returns:
- not null if the source name set was a URL, or could be retrieved through the classpath. null if the source name was interpreted as a file.
- See Also:
-
getFileName
Returns the file name value.- Returns:
- null if the source name was interpreted as a URL, derived from the source name otherwise.
- See Also:
-
getDirectoryPath
Returns the path of a source (URL or file). This is the counterpart for the static method getFileName(). The result includes the last file separator, if there are any.- Parameters:
aFullPathName
- the full path to a source.- Returns:
- the path of a source (URL or file).
- See Also:
-
getFileName
Return the file name of a source (URL or file) without the path. This is the part of the input string that comes after the last occurrence of a file separator. A file separator can be both/
and\
.- Parameters:
aFullPathName
- the full path to a source.- Returns:
- the file name of a source (URL or file) without the path.
- See Also:
-
getFilenameWithoutExtension
Return the file name of a source (URL or file) without the path.- Parameters:
aFullPathName
- the full path to a source.aExtensions
- The possible extensions for the format, if available. The casing does not matter. It doesn't matter either whether the extension starts with a "." or not. None of these extensions should benull
. Specify no extensions when the extensions are unknown.- Returns:
- the file name of a source (URL or file) without the path.
- Since:
- 2017.0
- See Also:
-
setUseCacheDefault
public static void setUseCacheDefault(boolean aUseCacheDefault) Sets the property UseCacheDefault. This property ensures using the cache of a browser for URL connections. Whether or not this cache is actually used depends on the implementation of the JVM (e.g. in web browsers). The default istrue
.- Parameters:
aUseCacheDefault
- specifies whether or not to use the cache.- See Also:
-
getUseCacheDefault
public static boolean getUseCacheDefault()Return the property UseCacheDefault.- Returns:
- whether to use the cache for URL connections.
- See Also:
-
setAsFileFirst
public static void setAsFileFirst(boolean aAsFileFirst) Sets the property AsFileFirst. This property ensures that IOUtil will try to open sources as files first, thus possibly avoiding costly URL/HTTP connections. The default isfalse
.- Parameters:
aAsFileFirst
- specifies whether or not to attempt to open the source name as a file first.- See Also:
-
getAsFileFirst
public static boolean getAsFileFirst()Returns the property AsFileFirst.- Returns:
- whether to address sources a files first.
- See Also:
-
setUseNIO
public static void setUseNIO(boolean aUseNIO) Sets the property UseNIO. This property specifies whether IOUtil can use the Java NIO package to read data from files. The NIO package can significantly speed up reading large files. The default isfalse
.- Parameters:
aUseNIO
- specifies whether or not to use the Java NIO package.- See Also:
-
getUseNIO
public static boolean getUseNIO()Returns the property UseNIO.- Returns:
- whether the Java NIO package can be used.
- See Also:
-
toByteArray
Converts the givenInputStream
to a byte array. Note that this method does not close the givenInputStream
. This is up to the caller of this method.- Parameters:
aInputStream
- the input stream to convert to a byte array- Returns:
- a byte array with the contents of the given
InputStream
- Throws:
IOException
-