org.springframework.http.ZeroCopyHttpOutputMessage Java Examples

The following examples show how to use org.springframework.http.ZeroCopyHttpOutputMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceHttpMessageWriter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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();
}
 
Example #2
Source File: ResourceHttpMessageWriter.java    From java-technology-stack with MIT License 6 votes vote down vote up
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();
}
 
Example #3
Source File: ZeroCopyIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
	}
}
 
Example #4
Source File: ZeroCopyIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
	}
}