public abstract class DataSource
extends java.lang.Object
An abstraction over the byte stream writing operations,
this class allows for a flexible way to write bytes to a BufferedSink
.
Generally used for specifying a data source when creating/uploading files.
create(File)
method can be used for reading data from a local file.
create(byte[])
method can be used for reading data from a byte array.
create(ByteString)
method can be used for reading data from Okio's immutable byte arrays.
writeTo(BufferedSink)
method.
Modifier and Type | Field and Description |
---|---|
static DataSource |
EMPTY
An empty
DataSource instance. |
Constructor and Description |
---|
DataSource() |
Modifier and Type | Method and Description |
---|---|
long |
contentLength()
Provide the data source length.
|
static DataSource |
create(byte[] data)
Create a
DataSource instance that reads from a byte array. |
static DataSource |
create(okio.ByteString data)
Create a
DataSource instance that reads from a ByteString . |
static DataSource |
create(java.io.File file)
Create a
DataSource instance that reads from a byte array. |
abstract void |
writeTo(okio.BufferedSink sink)
Write the data to a
BufferedSink . |
public static final DataSource EMPTY
DataSource
instance.
Can be used for creating empty files.
public long contentLength()
Override this method to provide the size of the data to be written, if known in advance, otherwise return -1.
public abstract void writeTo(okio.BufferedSink sink) throws java.io.IOException
BufferedSink
.sink
- BufferedSink
java.io.IOException
- on failed IO operationspublic static DataSource create(okio.ByteString data)
DataSource
instance that reads from a ByteString
.data
- a byte array. Must not be null.DataSource
that will read the given byte array.java.lang.IllegalArgumentException
- on a null data
argument.public static DataSource create(byte[] data)
DataSource
instance that reads from a byte array.data
- a byte array. Must not be null.DataSource
that will read the given byte array.java.lang.IllegalArgumentException
- on a null data
argument.public static DataSource create(java.io.File file)
DataSource
instance that reads from a byte array.file
- a file which will be read. Must not be null.DataSource
that will read the given file.java.lang.IllegalArgumentException
- on a null file
argument.