Java Code Examples for org.springframework.core.io.Resource#isFile()

The following examples show how to use org.springframework.core.io.Resource#isFile() . 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: DataBufferUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Read the given {@code Resource} into a {@code Flux} of {@code DataBuffer}s
 * starting at the given position.
 * <p>If the resource is a file, it is read into an
 * {@code AsynchronousFileChannel} and turned to {@code Flux} via
 * {@link #readAsynchronousFileChannel(Callable, DataBufferFactory, int)} or else
 * fall back on {@link #readByteChannel(Callable, DataBufferFactory, int)}.
 * Closes the channel when the flux is terminated.
 * @param resource the resource to read from
 * @param position the position to start reading from
 * @param bufferFactory the factory to create data buffers with
 * @param bufferSize the maximum size of the data buffers
 * @return a Flux of data buffers read from the given channel
 */
public static Flux<DataBuffer> read(
		Resource resource, long position, DataBufferFactory bufferFactory, int bufferSize) {

	try {
		if (resource.isFile()) {
			File file = resource.getFile();
			return readAsynchronousFileChannel(
					() -> AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ),
					position, bufferFactory, bufferSize);
		}
	}
	catch (IOException ignore) {
		// fallback to resource.readableChannel(), below
	}
	Flux<DataBuffer> result = readByteChannel(resource::readableChannel, bufferFactory, bufferSize);
	return position == 0 ? result : skipUntilByteCount(result, position);
}
 
Example 2
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 3
Source File: DataBufferUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Read the given {@code Resource} into a {@code Flux} of {@code DataBuffer}s
 * starting at the given position.
 * <p>If the resource is a file, it is read into an
 * {@code AsynchronousFileChannel} and turned to {@code Flux} via
 * {@link #readAsynchronousFileChannel(Callable, DataBufferFactory, int)} or else
 * fall back on {@link #readByteChannel(Callable, DataBufferFactory, int)}.
 * Closes the channel when the flux is terminated.
 * @param resource the resource to read from
 * @param position the position to start reading from
 * @param dataBufferFactory the factory to create data buffers with
 * @param bufferSize the maximum size of the data buffers
 * @return a flux of data buffers read from the given channel
 */
public static Flux<DataBuffer> read(
		Resource resource, long position, DataBufferFactory dataBufferFactory, int bufferSize) {

	try {
		if (resource.isFile()) {
			File file = resource.getFile();
			return readAsynchronousFileChannel(
					() -> AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ),
					position, dataBufferFactory, bufferSize);
		}
	}
	catch (IOException ignore) {
		// fallback to resource.readableChannel(), below
	}

	Flux<DataBuffer> result = readByteChannel(resource::readableChannel, dataBufferFactory, bufferSize);
	return position == 0 ? result : skipUntilByteCount(result, position);
}
 
Example 4
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();
}