@UnstableApi
public final class DataSourceUtil


Utility methods for DataSource.

Summary

Public methods

static void

Closes a DataSource, suppressing any IOException that may occur.

static byte[]
readExactly(DataSource dataSource, int length)

Reads length bytes from the specified opened DataSource, and returns a byte array containing the read data.

static byte[]
readToEnd(DataSource dataSource)

Reads data from the specified opened DataSource until it ends, and returns a byte array containing the read data.

Public methods

closeQuietly

public static void closeQuietly(@Nullable DataSource dataSource)

Closes a DataSource, suppressing any IOException that may occur.

Parameters
@Nullable DataSource dataSource

The DataSource to close.

readExactly

public static byte[] readExactly(DataSource dataSource, int length)

Reads length bytes from the specified opened DataSource, and returns a byte array containing the read data.

Parameters
DataSource dataSource

The source from which to read.

int length

The number of bytes to read.

Returns
byte[]

The read data.

Throws
java.io.IOException

If an error occurs reading from the source.

java.lang.IllegalStateException

If the end of the source was reached before length bytes could be read.

readToEnd

public static byte[] readToEnd(DataSource dataSource)

Reads data from the specified opened DataSource until it ends, and returns a byte array containing the read data.

Parameters
DataSource dataSource

The source from which to read.

Returns
byte[]

The concatenation of all read data.

Throws
java.io.IOException

If an error occurs reading from the source.