All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datakernel.loader.StaticLoaderFileReader Maven / Gradle / Ivy

package io.datakernel.loader;

import io.datakernel.async.Promise;
import io.datakernel.bytebuf.ByteBuf;
import io.datakernel.bytebuf.ByteBufQueue;
import io.datakernel.csp.file.ChannelFileReader;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.Executor;

class StaticLoaderFileReader implements StaticLoader {
	private final Executor executor;
	private final Path root;

	private StaticLoaderFileReader(Executor executor, Path root) {
		this.executor = executor;
		this.root = root;
	}

	public static StaticLoader create(Executor executor, Path dir) {
		return new StaticLoaderFileReader(executor, dir);
	}

	@Override
	public Promise load(String path) {
		Path file = root.resolve(path).normalize();

		if (!file.startsWith(root)) {
			return Promise.ofException(NOT_FOUND_EXCEPTION);
		}

		return Promise.ofBlockingCallable(executor,
				() -> {
					if (Files.isRegularFile(file)) {
						return null;
					}
					if (Files.isDirectory(file)) {
						throw IS_A_DIRECTORY;
					} else {
						throw NOT_FOUND_EXCEPTION;
					}
				})
				.then($ -> ChannelFileReader.open(executor, file))
				.then(cfr -> cfr.toCollector(ByteBufQueue.collector()));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy