Class TLcdInputStreamFactory
- All Implemented Interfaces:
ILcdInputStreamFactory
Source names ending in .zip or .gz are unzipped automatically.
The defaultAllowUserInteraction flag and the defaultUseCaches flag are passed on to any URL connection.
The passwordProvider can be set for URLs that require a password.
The returned streams are automatically wrapped with a buffered input stream, to avoid a performance penalty.
This InputStreamFactory asks by default the data over HTTP to be compressed.
If the server does not allow it, it falls back on non-compressed data.
This behaviour can be changed using setAcceptCompressedEncoding(boolean)
or by setting the com.luciad.io.TLcdInputStreamFactory.acceptHttpCompressedEncoding
system property to the desired behaviour.
-
Constructor Summary
ConstructorDescriptionCreates a new TLcdInputStreamFactory.TLcdInputStreamFactory
(int aBufferSize) Creates a new TLcdInputStreamFactory that creates input streams with the given buffer size. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canCreateInputStream
(String aSource) Checks whether this input stream factory can provide access toaSource
.createInputStream
(String aSourceName) Creates an input stream from the given source name.int
int
Returns the connect timeout value, or zero if the option is disabled (that is, a timeout of infinity).boolean
Indicates whether user interaction is allowed.Returns the request property value for the given key.boolean
Indicates whether default caching is turned on.static boolean
Returns whether the Java NIO package may be used to read data from files.Returns the password provider for HTTP connections that need a password.int
Returns setting for read timeout, or zero if the timeout option is disabled (that is, a timeout of infinity).boolean
Returns whether source names ending with .zip are unzipped automatically.boolean
Returns whether the Java NIO package will be used to read data from files.boolean
Deprecated.This method has been deprecated.static void
setAcceptCompressedEncoding
(boolean acceptCompressedEncoding) Sets the behaviour of allTLcdInputStreamFactory
instances regarding the request of compressed content.void
setBufferSize
(int aBufferSize) Sets the buffer size of input streams created by this factory.static void
setClassTraceOn
(boolean aClassTraceOn) Deprecated.This method has been deprecated.void
setConnectTimeout
(int aConnectTimeout) Sets a specified timeout value, in milliseconds, to be used when opening a URLConnection.void
setDefaultAllowUserInteraction
(boolean aDefaultAllowUserInteraction) Sets whether user interaction is allowed (used in URL connections).void
setDefaultRequestProperty
(String aKey, String aValue) Sets a default request property for connecting (used in URL connections).void
setDefaultUseCaches
(boolean aDefaultUseCaches) Sets whether to use default caching on connecting (used in URL connections).static void
setMayUseNIO
(boolean aMayUseNIO) Sets globally whether the Java NIO package may be used to read data from files.void
setPasswordProvider
(ILcdPasswordProvider aPasswordProvider) Sets the password provider for HTTP connections that need a password.void
setReadTimeout
(int aReadTimeout) Sets the read timeout to the given value in milliseconds.void
setTraceOn
(boolean aTraceOn) Deprecated.This method has been deprecated.void
setUnzipAutomatically
(boolean aUnzipAutomatically) Sets whether to unzip source names ending with .zip automatically, returning the contents of the first entry in the zip archive.void
setUseNIO
(boolean aUseNIO) Sets whether to use the Java NIO package to read data from files.
-
Constructor Details
-
TLcdInputStreamFactory
public TLcdInputStreamFactory()Creates a new TLcdInputStreamFactory. -
TLcdInputStreamFactory
public TLcdInputStreamFactory(int aBufferSize) Creates a new TLcdInputStreamFactory that creates input streams with the given buffer size.- Parameters:
aBufferSize
- The size of the buffer in bytes.
-
-
Method Details
-
setMayUseNIO
public static void setMayUseNIO(boolean aMayUseNIO) Sets globally whether the Java NIO package may be used to read data from files. The default istrue
. You may want to set this tofalse
if the NIO code is blocking the modification or removal of your files in Windows, for instance.- Parameters:
aMayUseNIO
- specifies whether or not the Java NIO package may be used.- See Also:
-
getMayUseNIO
public static boolean getMayUseNIO()Returns whether the Java NIO package may be used to read data from files.- Returns:
- true if the Java NIO package may be used.
- See Also:
-
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.
-
setAcceptCompressedEncoding
public static void setAcceptCompressedEncoding(boolean acceptCompressedEncoding) Sets the behaviour of allTLcdInputStreamFactory
instances regarding the request of compressed content. This is achieved using (or not) the "Accept-Encoding" field in the request's header.- Parameters:
acceptCompressedEncoding
- the new boolean value. True if TLcdInputStreamFactory should ask for compressed data (i.e. gzip, deflate, compress)- Since:
- 2018.0
-
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.Indicates whether tracing is on for this instance.- Returns:
- true if tracing is set to true for this instance or for all instances of this class.
-
setDefaultAllowUserInteraction
public void setDefaultAllowUserInteraction(boolean aDefaultAllowUserInteraction) Sets whether user interaction is allowed (used in URL connections). The default istrue
.- Parameters:
aDefaultAllowUserInteraction
- true to allow user interaction when creating a connection.- See Also:
-
getDefaultAllowUserInteraction
public boolean getDefaultAllowUserInteraction()Indicates whether user interaction is allowed.- Returns:
- true when user interaction is allowed.
- See Also:
-
setDefaultUseCaches
public void setDefaultUseCaches(boolean aDefaultUseCaches) Sets whether to use default caching on connecting (used in URL connections). The default istrue
.- Parameters:
aDefaultUseCaches
- true to use default caching on connecting.- See Also:
-
getDefaultUseCaches
public boolean getDefaultUseCaches()Indicates whether default caching is turned on.- Returns:
- true if default caching is turned on.
- See Also:
-
setDefaultRequestProperty
Sets a default request property for connecting (used in URL connections). If a property of this name already exists, its value is overwritten. If the value is null, the property is removed. By default, there aren't any properties set.- Parameters:
aKey
- the request property key.aValue
- the request property value.- See Also:
-
getDefaultRequestProperty
Returns the request property value for the given key. If no property has been set yet, null is returned.- Parameters:
aKey
- the request property key.- See Also:
-
setUseNIO
public void setUseNIO(boolean aUseNIO) Sets whether to use the Java NIO package to read data from files. The NIO package can significantly speed up reading large files. and if it is globally allowed. The default isfalse
.- Parameters:
aUseNIO
- specifies whether or not to use the Java NIO package.- See Also:
-
getUseNIO
public boolean getUseNIO()Returns whether the Java NIO package will be used to read data from files.- Returns:
- true if the Java NIO package will be used.
- See Also:
-
setUnzipAutomatically
public void setUnzipAutomatically(boolean aUnzipAutomatically) Sets whether to unzip source names ending with .zip automatically, returning the contents of the first entry in the zip archive. The default istrue
.- Parameters:
aUnzipAutomatically
-true
if source names ending with .zip must be unzipped automatically,false
otherwise.- See Also:
-
getUnzipAutomatically
public boolean getUnzipAutomatically()Returns whether source names ending with .zip are unzipped automatically.- Returns:
true
if source names ending with .zip are unzipped automatically,false
otherwise.- See Also:
-
setPasswordProvider
Sets the password provider for HTTP connections that need a password. It will only be used whenHttpURLConnection.HTTP_UNAUTHORIZED
is the response code of the initial connection attempt of the HTTP connection.If you want the credentials to be sent at the initial attempt, you need to use preemptive authentication:
TLcdInputStreamFactory inputStreamFactory = new TLcdInputStreamFactory(); String credentials = "username" + ":" + "password"; String encodedAuth = Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)); inputStreamFactory.setDefaultRequestProperty("Authorization", "Basic " + encodedAuth);
- Parameters:
aPasswordProvider
- the password provider for HTTP connections that need a password.- See Also:
-
getPasswordProvider
Returns the password provider for HTTP connections that need a password.- Returns:
- the password provider for HTTP connections that need a password.
- See Also:
-
getBufferSize
public int getBufferSize()- Returns:
- the buffer size of input streams created by this factory.
- Since:
- 2022.0
-
setBufferSize
public void setBufferSize(int aBufferSize) Sets the buffer size of input streams created by this factory. The default is 8192.- Parameters:
aBufferSize
- the buffer size. Must be larger than 0.- Since:
- 2022.0
-
setConnectTimeout
public void setConnectTimeout(int aConnectTimeout) Sets a specified timeout value, in milliseconds, to be used when opening a URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout. Not calling this method implies that the java default is used (that is, the connection timeout will not be set on the underlying URLConnection).- Parameters:
aConnectTimeout
- the timeout value in milliseconds- Throws:
IllegalArgumentException
- if the timeout parameter is negative- Since:
- 2016.1.34
- See Also:
-
getConnectTimeout
public int getConnectTimeout()Returns the connect timeout value, or zero if the option is disabled (that is, a timeout of infinity).- Returns:
- the connect timeout value in milliseconds
- Since:
- 2016.1.34
- See Also:
-
setReadTimeout
public void setReadTimeout(int aReadTimeout) Sets the read timeout to the given value in milliseconds. A non-zero value specifies the timeout when reading from an input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout. Not calling this method implies that the java default is used (that is, the read timeout will not be set on the underlying URLConnection).- Parameters:
aReadTimeout
- the timeout value in milliseconds- Throws:
IllegalArgumentException
- if the timeout parameter is negative- Since:
- 2016.1.34
- See Also:
-
getReadTimeout
public int getReadTimeout()Returns setting for read timeout, or zero if the timeout option is disabled (that is, a timeout of infinity).- Returns:
- the read timeout value in milliseconds
- Since:
- 2016.1.34
- See Also:
-
canCreateInputStream
Checks whether this input stream factory can provide access toaSource
. It is acceptable for this method to returntrue
for a source whilecreateInputStream
throws an exception for that same source.For performance reasons, we strongly recommend that implementations perform only a simple test. For example: check the structure of
aSource
to see if it is a URI with a specific scheme, but do not verify thataSource
really is accessible.The default implementation of this method returns
true
. Make sure to override this method if you can easily reject sources. This can help avoid pointless invocations ofcreateInputStream
which would have to throw exceptions for unsupported sources.The implementation in
TLcdInputStreamFactory
requiresaSource
to be non-empty and, if it appears to have a URL scheme, it must be possible to create aURL
from it.- Specified by:
canCreateInputStream
in interfaceILcdInputStreamFactory
- Parameters:
aSource
- the data source to be verified; typically a file name or a URL.- Returns:
true
if this input stream factory can likely access the source,false
otherwise.- See Also:
-
createInputStream
Creates an input stream from the given source name. This implementation also accepts files compressed with zip or gzip.- Specified by:
createInputStream
in interfaceILcdInputStreamFactory
- Parameters:
aSourceName
- the source from which data can be read.- Returns:
- an input stream providing the data stored at the location with the given source name.
- Throws:
IOException
- when accessing the location with the given source name failed.- See Also:
-