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
public abstract class StreamingHttpFile<T extends Closeable> extends AbstractHttpFile
A skeletalHttpFile
that simplifies the streaming of potentially large content.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
StreamingHttpFile(MediaType contentType, Clock clock, boolean dateEnabled, boolean lastModifiedEnabled, BiFunction<String,HttpFileAttributes,String> entityTagFunction, HttpHeaders headers)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description CompletableFuture<AggregatedHttpFile>
aggregate(Executor fileReadExecutor)
Converts this file into anAggregatedHttpFile
.CompletableFuture<AggregatedHttpFile>
aggregateWithPooledObjects(Executor fileReadExecutor, ByteBufAllocator alloc)
Converts this file into anAggregatedHttpFile
.protected HttpResponse
doRead(HttpHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc)
Returns a newHttpResponse
which streams the content of the file which follows the specifiedHttpHeaders
.protected abstract T
newStream()
Opens a new stream which reads from the file.protected abstract int
read(T src, ByteBuf dst)
Reads the content ofsrc
intodst
.-
Methods inherited from class com.linecorp.armeria.server.file.AbstractHttpFile
asService, clock, contentType, generateEntityTag, headers, isDateEnabled, isLastModifiedEnabled, pathOrUri, read, 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 Detail
-
StreamingHttpFile
protected StreamingHttpFile(@Nullable MediaType contentType, Clock clock, boolean dateEnabled, boolean lastModifiedEnabled, @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 Detail
-
doRead
protected final HttpResponse doRead(HttpHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc) throws IOException
Description copied from class:AbstractHttpFile
Returns a newHttpResponse
which streams the content of the file which follows the specifiedHttpHeaders
.- Specified by:
doRead
in classAbstractHttpFile
- Parameters:
headers
- theHttpHeaders
of the responselength
- 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
public final CompletableFuture<AggregatedHttpFile> aggregate(Executor fileReadExecutor)
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
Converts this file into anAggregatedHttpFile
.AggregatedHttpFile.content()
will return a pooled object, 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
.
-
newStream
@Nullable protected abstract T newStream() throws IOException
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
protected abstract int read(T src, ByteBuf dst) throws IOException
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
-
-