Interface SizeAwareDataSource

  • All Superinterfaces:
    DataSource
    All Known Implementing Classes:
    BlobDataSource, ByteArrayDataSource, CachedFileDataSource, EmptyDataSource

    public interface SizeAwareDataSource
    extends DataSource
    Optional extension interface that can be implemented by data sources that support a getSize method. Code working with data sources can use this this information to optimize certain operations. Note however that instead of checking of this interface directly, this kind of code should use DataSourceUtils.getSize(DataSource) because this method is able to determine the size of other types of data sources as well.

    Code using this interface should be aware that some implementations may be unable to guarantee 100% accuracy when determining the size of the data source. Situations where this can occur include:

    • The data source uses a network protocol that allows to get the size of the data but that doesn't guarantee accurateness.
    • Reading the data involves a decoding operation and the length of the resulting stream can't be determined precisely without performing the decoding operation. In this case the implementation of this interface may return a value based on an estimation.
    When reading the actual data, the code should always read until the end of the stream is reached (as indicated by the return value of the read methods of the InputStream class). It must be prepared to reach the end of the stream after a number of bytes that is lower or higher than the value returned by getSize().
    • Method Detail

      • getSize

        long getSize()
        Get the size of the data source. Implementations must return the number of bytes that can be read from the input stream returned by DataSource.getInputStream() before reaching the end of the stream. If the implementation is unable to determine the size, it must return -1.
        Returns:
        the size of the data source or -1 if the size is not known