Interface SizeAwareDataSource

All Superinterfaces:
DataSource
All Known Implementing Classes:
ByteArrayDataSource, CachedFileDataSource

public interface SizeAwareDataSource extends DataSource
Optional extension interface that can be implemented by data sources that support a getSize method. This is used by DataHandlerUtils.toBlob(DataHandler) to implement the Blob.getSize() method. Note that the latter is able to determine the size of some types of DataSource instances that don't implement this extension interface.

Some implementations of this interface 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 Details

    • 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