Java Code Examples for org.springframework.core.io.buffer.DataBufferUtils#read()

The following examples show how to use org.springframework.core.io.buffer.DataBufferUtils#read() . 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: ContentVersionStrategy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<String> getResourceVersion(Resource resource) {
	Flux<DataBuffer> flux =
			DataBufferUtils.read(resource, dataBufferFactory, StreamUtils.BUFFER_SIZE);
	return DataBufferUtils.join(flux)
			.map(buffer -> {
				byte[] result = new byte[buffer.readableByteCount()];
				buffer.read(result);
				DataBufferUtils.release(buffer);
				return DigestUtils.md5DigestAsHex(result);
			});
}
 
Example 2
Source File: WebClientIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-16246
public void shouldSendLargeTextFile() throws IOException {
	prepareResponse(response -> {});

	Resource resource = new ClassPathResource("largeTextFile.txt", getClass());
	byte[] expected = Files.readAllBytes(resource.getFile().toPath());
	Flux<DataBuffer> body = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 4096);

	this.webClient.post()
			.uri("/")
			.body(body, DataBuffer.class)
			.retrieve()
			.bodyToMono(Void.class)
			.block(Duration.ofSeconds(5));

	expectRequest(request -> {
		ByteArrayOutputStream actual = new ByteArrayOutputStream();
		try {
			request.getBody().copyTo(actual);
		}
		catch (IOException ex) {
			throw new IllegalStateException(ex);
		}
		assertEquals(expected.length, actual.size());
		assertEquals(hash(expected), hash(actual.toByteArray()));
	});
}
 
Example 3
Source File: ResourceRegionEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Flux<DataBuffer> writeResourceRegion(
		ResourceRegion region, DataBufferFactory bufferFactory, @Nullable Map<String, Object> hints) {

	Resource resource = region.getResource();
	long position = region.getPosition();
	long count = region.getCount();

	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		logger.debug(Hints.getLogPrefix(hints) +
				"Writing region " + position + "-" + (position + count) + " of [" + resource + "]");
	}

	Flux<DataBuffer> in = DataBufferUtils.read(resource, position, bufferFactory, this.bufferSize);
	return DataBufferUtils.takeUntilByteCount(in, count);
}
 
Example 4
Source File: ResourceEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory bufferFactory,
		ResolvableType type, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		String logPrefix = Hints.getLogPrefix(hints);
		logger.debug(logPrefix + "Writing [" + resource + "]");
	}
	return DataBufferUtils.read(resource, bufferFactory, this.bufferSize);
}
 
Example 5
Source File: ContentVersionStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<String> getResourceVersion(Resource resource) {
	Flux<DataBuffer> flux =
			DataBufferUtils.read(resource, dataBufferFactory, StreamUtils.BUFFER_SIZE);
	return DataBufferUtils.join(flux)
			.map(buffer -> {
				byte[] result = new byte[buffer.readableByteCount()];
				buffer.read(result);
				DataBufferUtils.release(buffer);
				return DigestUtils.md5DigestAsHex(result);
			});
}
 
Example 6
Source File: WebClientIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-16246
public void shouldSendLargeTextFile() throws IOException {
	prepareResponse(response -> {});

	Resource resource = new ClassPathResource("largeTextFile.txt", getClass());
	byte[] expected = Files.readAllBytes(resource.getFile().toPath());
	Flux<DataBuffer> body = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 4096);

	this.webClient.post()
			.uri("/")
			.body(body, DataBuffer.class)
			.retrieve()
			.bodyToMono(Void.class)
			.block(Duration.ofSeconds(5));

	expectRequest(request -> {
		ByteArrayOutputStream actual = new ByteArrayOutputStream();
		try {
			request.getBody().copyTo(actual);
		}
		catch (IOException ex) {
			throw new IllegalStateException(ex);
		}
		assertEquals(expected.length, actual.size());
		assertEquals(hash(expected), hash(actual.toByteArray()));
	});
}
 
Example 7
Source File: ResourceRegionEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Flux<DataBuffer> writeResourceRegion(
		ResourceRegion region, DataBufferFactory bufferFactory, @Nullable Map<String, Object> hints) {

	Resource resource = region.getResource();
	long position = region.getPosition();
	long count = region.getCount();

	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		logger.debug(Hints.getLogPrefix(hints) +
				"Writing region " + position + "-" + (position + count) + " of [" + resource + "]");
	}

	Flux<DataBuffer> in = DataBufferUtils.read(resource, position, bufferFactory, this.bufferSize);
	return DataBufferUtils.takeUntilByteCount(in, count);
}
 
Example 8
Source File: ResourceEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Flux<DataBuffer> encode(Resource resource, DataBufferFactory dataBufferFactory,
		ResolvableType type, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
		String logPrefix = Hints.getLogPrefix(hints);
		logger.debug(logPrefix + "Writing [" + resource + "]");
	}

	return DataBufferUtils.read(resource, dataBufferFactory, this.bufferSize);
}
 
Example 9
Source File: WebClientFeaturesController.java    From feign-reactive with Apache License 2.0 4 votes vote down vote up
@PostMapping(path = "/mirrorResourceReactiveWithZeroCopying")
@Override
public Flux<DataBuffer> mirrorResourceReactiveWithZeroCopying(@RequestBody Resource resource) {
    return DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 3);
}