Search in sources :

Example 1 with ZeroCopyHttpOutputMessage

use of cn.taketoday.http.ZeroCopyHttpOutputMessage in project today-infrastructure by TAKETODAY.

the class ResourceHttpMessageWriter method zeroCopy.

private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
        try {
            File file = resource.getFile();
            long size = file.length();
            long pos = region != null ? region.getPosition() : 0;
            long count = region != null ? region.getCount() : size;
            if (logger.isDebugEnabled()) {
                String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
                logger.debug("{}Zero-copy {}[{}]", Hints.getLogPrefix(hints), formatted, resource);
            }
            return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
        } catch (IOException ex) {
        // should not happen
        }
    }
    return Optional.empty();
}
Also used : ZeroCopyHttpOutputMessage(cn.taketoday.http.ZeroCopyHttpOutputMessage) IOException(java.io.IOException) File(java.io.File)

Example 2 with ZeroCopyHttpOutputMessage

use of cn.taketoday.http.ZeroCopyHttpOutputMessage in project today-framework by TAKETODAY.

the class ResourceHttpMessageWriter method zeroCopy.

private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
        try {
            File file = resource.getFile();
            long size = file.length();
            long pos = region != null ? region.getPosition() : 0;
            long count = region != null ? region.getCount() : size;
            if (logger.isDebugEnabled()) {
                String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
                logger.debug("{}Zero-copy {}[{}]", Hints.getLogPrefix(hints), formatted, resource);
            }
            return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
        } catch (IOException ex) {
        // should not happen
        }
    }
    return Optional.empty();
}
Also used : ZeroCopyHttpOutputMessage(cn.taketoday.http.ZeroCopyHttpOutputMessage) IOException(java.io.IOException) File(java.io.File)

Aggregations

ZeroCopyHttpOutputMessage (cn.taketoday.http.ZeroCopyHttpOutputMessage)2 File (java.io.File)2 IOException (java.io.IOException)2