Package com.luciad.io

Class TLcdInputStreamFactory

java.lang.Object
com.luciad.io.TLcdInputStreamFactory
All Implemented Interfaces:
ILcdInputStreamFactory

@LcdService(service=ILcdInputStreamFactory.class, priority=20000) public class TLcdInputStreamFactory extends Object implements ILcdInputStreamFactory
This class implements an ILcdInputStreamFactory that can create streams, given a URL, the file name of a file in the class path, or a local file name.

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

    Constructors
    Constructor
    Description
    Creates a new TLcdInputStreamFactory.
    TLcdInputStreamFactory(int aBufferSize)
    Creates a new TLcdInputStreamFactory that creates input streams with the given buffer size.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks whether this input stream factory can provide access to aSource.
    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 all TLcdInputStreamFactory 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
    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
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 is true. You may want to set this to false 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 is true 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 all TLcdInputStreamFactory 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 either true or false as argument automatically turns off tracing for all other class instances for which setTraceOn has not been called. If the argument is false 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 is true.
      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 is true.
      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

      public void setDefaultRequestProperty(String aKey, String aValue)
      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

      public String getDefaultRequestProperty(String aKey)
      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 is false.
      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 is true.
      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

      public void setPasswordProvider(ILcdPasswordProvider aPasswordProvider)
      Sets the password provider for HTTP connections that need a password. It will only be used when HttpURLConnection.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

      public ILcdPasswordProvider 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

      public boolean canCreateInputStream(String aSource)
      Checks whether this input stream factory can provide access to aSource. It is acceptable for this method to return true for a source while createInputStream 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 that aSource 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 of createInputStream which would have to throw exceptions for unsupported sources.

      The implementation in TLcdInputStreamFactory requires aSource to be non-empty and, if it appears to have a URL scheme, it must be possible to create a URL from it.

      Specified by:
      canCreateInputStream in interface ILcdInputStreamFactory
      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

      public InputStream createInputStream(String aSourceName) throws IOException
      Creates an input stream from the given source name. This implementation also accepts files compressed with zip or gzip.
      Specified by:
      createInputStream in interface ILcdInputStreamFactory
      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: