org.springframework.http.ZeroCopyHttpOutputMessage源码实例Demo

类org.springframework.http.ZeroCopyHttpOutputMessage源码实例Demo

下面列出了org.springframework.http.ZeroCopyHttpOutputMessage 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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 pos = region != null ? region.getPosition() : 0;
			long count = region != null ? region.getCount() : file.length();
			if (logger.isDebugEnabled()) {
				String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
				logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
			}
			return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
		}
		catch (IOException ex) {
			// should not happen
		}
	}
	return Optional.empty();
}
 
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 pos = region != null ? region.getPosition() : 0;
			long count = region != null ? region.getCount() : file.length();
			if (logger.isDebugEnabled()) {
				String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
				logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
			}
			return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
		}
		catch (IOException ex) {
			// should not happen
		}
	}
	return Optional.empty();
}
 
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
	try {
		ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
		Resource logo = new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class);
		File logoFile = logo.getFile();
		zeroCopyResponse.getHeaders().setContentType(MediaType.IMAGE_PNG);
		zeroCopyResponse.getHeaders().setContentLength(logoFile.length());
		return zeroCopyResponse.writeWith(logoFile, 0, logoFile.length());
	}
	catch (Throwable ex) {
		return Mono.error(ex);
	}
}
 
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
	try {
		ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
		Resource logo = new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class);
		File logoFile = logo.getFile();
		zeroCopyResponse.getHeaders().setContentType(MediaType.IMAGE_PNG);
		zeroCopyResponse.getHeaders().setContentLength(logoFile.length());
		return zeroCopyResponse.writeWith(logoFile, 0, logoFile.length());
	}
	catch (Throwable ex) {
		return Mono.error(ex);
	}
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法