Package com.linecorp.armeria.server.file
Class StreamingHttpFile<T extends Closeable>
java.lang.Object
com.linecorp.armeria.server.file.AbstractHttpFile
com.linecorp.armeria.server.file.StreamingHttpFile<T>
- Type Parameters:
T
- the type of the stream where the file content is read from, e.g.InputStream
.
- All Implemented Interfaces:
HttpFile
A skeletal
HttpFile
that simplifies the streaming of potentially large content.-
Constructor Summary
ModifierConstructorDescriptionprotected
StreamingHttpFile(@Nullable MediaType contentType, Clock clock, boolean dateEnabled, boolean lastModifiedEnabled, @Nullable BiFunction<String,HttpFileAttributes,String> entityTagFunction, HttpHeaders headers)
Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionConverts this file into anAggregatedHttpFile
.aggregateWithPooledObjects(Executor fileReadExecutor, ByteBufAllocator alloc)
(Advanced users only) Converts this file into anAggregatedHttpFile
.protected HttpResponse
doRead(ResponseHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc)
Returns a newHttpResponse
which streams the content of the file which follows the specifiedResponseHeaders
.protected abstract T
Opens a new stream which reads from the file.protected abstract int
Reads the content ofsrc
intodst
.Methods inherited from class com.linecorp.armeria.server.file.AbstractHttpFile
additionalHeaders, asService, clock, contentType, generateEntityTag, isDateEnabled, isLastModifiedEnabled, pathOrUri, read, readHeaders, readHeaders
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.server.file.HttpFile
readAttributes
-
Constructor Details
-
StreamingHttpFile
protected StreamingHttpFile(@Nullable @Nullable MediaType contentType, Clock clock, boolean dateEnabled, boolean lastModifiedEnabled, @Nullable @Nullable BiFunction<String,HttpFileAttributes,String> entityTagFunction, HttpHeaders headers)Creates a new instance.- Parameters:
contentType
- theMediaType
of the file which will be used as the"content-type"
header value.null
to disable setting the"content-type"
header.clock
- theClock
which provides the current date and timedateEnabled
- whether to set the"date"
header automaticallylastModifiedEnabled
- whether to add the"last-modified"
header automaticallyentityTagFunction
- theBiFunction
that generates an entity tag from the file's attributes.null
to disable setting the"etag"
header.headers
- the additional headers to set
-
-
Method Details
-
doRead
protected final HttpResponse doRead(ResponseHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc) throws IOExceptionDescription copied from class:AbstractHttpFile
Returns a newHttpResponse
which streams the content of the file which follows the specifiedResponseHeaders
.- Specified by:
doRead
in classAbstractHttpFile
- Parameters:
headers
- theResponseHeaders
length
- the content length. The returnedHttpResponse
must stream only as many bytes as this value.fileReadExecutor
- theExecutor
which should be used for performing a blocking file I/Oalloc
- theByteBufAllocator
which should be used for allocating an input buffer- Returns:
- the
HttpResponse
, ornull
if the file does not exist. - Throws:
IOException
- if failed to open the file. Note that an I/O error which occurred during content streaming will be notified via the returnedHttpResponse
's error notification mechanism.
-
aggregate
Description copied from interface:HttpFile
Converts this file into anAggregatedHttpFile
.- Parameters:
fileReadExecutor
- theExecutor
which will perform the read operations against the file- Returns:
- a
CompletableFuture
which will complete when the aggregation process is finished, or aCompletableFuture
successfully completed withthis
, if this file is already anAggregatedHttpFile
.
-
aggregateWithPooledObjects
public final CompletableFuture<AggregatedHttpFile> aggregateWithPooledObjects(Executor fileReadExecutor, ByteBufAllocator alloc)Description copied from interface:HttpFile
(Advanced users only) Converts this file into anAggregatedHttpFile
.AggregatedHttpFile.content()
will return a pooledHttpData
, and the caller must ensure to release it. If you don't know what this means, useHttpFile.aggregate(Executor)
.- Parameters:
fileReadExecutor
- theExecutor
which will perform the read operations against the filealloc
- theByteBufAllocator
which will allocate the content buffer- Returns:
- a
CompletableFuture
which will complete when the aggregation process is finished, or aCompletableFuture
successfully completed withthis
, if this file is already anAggregatedHttpFile
. - See Also:
PooledObjects
-
newStream
Opens a new stream which reads from the file.- Returns:
- the new stream, or
null
if the file does not exist. - Throws:
IOException
- if failed to open a new stream
-
read
Reads the content ofsrc
intodst
.- Returns:
- the number of read bytes, or
-1
if reached at the end of the file - Throws:
IOException
- if failed to read the content
-