public class FileDataSource extends Object implements DataSource
Cipher
is provided.
An instance is used by the server, unless another custom object is provided
with HttpServer.setDataSource(DataSource)
.
If decryption is needed, use HttpServer.setCipher(Cipher)
or HttpServer.setCipherFactory(CipherFactory)
to communicate a cipher to the data source.
Constructor and Description |
---|
FileDataSource() |
Modifier and Type | Method and Description |
---|---|
long |
getContentLength()
Returns the length of data to deliver,
that is the length of the file minus the offset,
unless a cipher with a padding scheme is used.
|
long |
getContentSize()
Returns the length of the file, unless a cipher with a padding scheme is used.
|
String |
getContentType()
Returns the content type of the file.
|
InputStream |
getInputStream()
Returns a content stream of the resource.
|
long |
getOffset()
Returns the offset in the file.
|
String |
getUriString()
Returns the URI of the data source.
|
boolean |
isExisting()
Tests whether the file exists.
|
boolean |
isPartial()
Tells whether the content to be delivered will be partial.
|
boolean |
isReadable()
Tests whether the file can be read.
|
void |
setCipher(Cipher cipher)
Sets the
Cipher to use if the stream has to be decrypted. |
void |
setCipherFactory(CipherFactory cipherFactory)
Sets the factory instance, able to deliver ciphers when needed.
|
void |
setSource(URI uri,
long offset)
Sets the properties of the data source.
|
protected InputStream |
toInputStream(AssetFileDescriptor afd)
A internal step called in
getInputStream() to get a stream out of the file. |
protected InputStream |
toInputStream(File file)
A internal step called in
getInputStream() to get a stream out of the file. |
public void setCipher(Cipher cipher)
DataSource
Cipher
to use if the stream has to be decrypted.
The information may also be set through HttpServer.setCipher(Cipher)
.setCipher
in interface DataSource
cipher
- The cipher needed to decrypt the stream.public void setCipherFactory(CipherFactory cipherFactory)
DataSource
HttpServer.setCipherFactory(CipherFactory)
.setCipherFactory
in interface DataSource
cipherFactory
- The factory to deliver ciphers on demand.public void setSource(URI uri, long offset)
setSource
in interface DataSource
uri
- The URI as provided in the HTTP request line.offset
- The offset provided in a Range
request header, or 0 by default.public String getUriString()
DataSource
getUriString
in interface DataSource
null
if called before setSource()
by mistake.public boolean isExisting()
isExisting
in interface DataSource
true
if and only if the resource really exists.public boolean isReadable()
isReadable
in interface DataSource
true
if and only if the resource can be accessed for reading.protected InputStream toInputStream(File file) throws FileNotFoundException
getInputStream()
to get a stream out of the file.
It is mainly designed for debugging: overriding only this method is enough to get a hook on the flow,
and still benefit from the implemented features (decryption, offset management).file
- The file to get a stream from.FileNotFoundException
- If the file does not exist.protected InputStream toInputStream(AssetFileDescriptor afd) throws IOException
getInputStream()
to get a stream out of the file.
It is mainly designed for debugging: overriding only this method is enough to get a hook on the flow,
and still benefit from the implemented features (decryption, offset management).afd
- The file descriptor of the Zip or Asset entry to get a stream from.IOException
- If the stream cannot be created.public InputStream getInputStream() throws IOException
DataSource
getInputStream
in interface DataSource
IOException
- If the stream cannot be created or skipped, or if some other I/O error occurs.public boolean isPartial()
DataSource
offset
is set.isPartial
in interface DataSource
true
if the content to deliver is only a part of the full resource.public long getContentSize()
getContentSize
in interface DataSource
public long getOffset()
getOffset
in interface DataSource
public long getContentLength()
getContentLength
in interface DataSource
public String getContentType()
Example: for a mp4 file, returns "video/mp4".
getContentType
in interface DataSource
null
if the type is unknown.