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

org.webpieces.webserver.impl.filereaders.ChunkFileSystemReader Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package org.webpieces.webserver.impl.filereaders;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ChunkFileSystemReader implements ChunkReader {

	private static final Logger log = LoggerFactory.getLogger(ChunkFileSystemReader.class);

	private AsynchronousFileChannel asyncFile;
	private Path file;
	
	public ChunkFileSystemReader(AsynchronousFileChannel asyncFile, Path file) {
		this.asyncFile = asyncFile;
		this.file = file;
	}
	
	@Override
	public String toString() {
		return "ChunkFileSystemReader="+file;
	}
	
	@Override
	public CompletableFuture read(ByteBuffer buf, String filePathForLogging, int position) {
		CompletableFuture future = new CompletableFuture();
		
		int remaining = buf.remaining();
    
		CompletionHandler handler = new CompletionHandler() {
			@Override
			public void completed(Integer result, String attachment) {
				if(result.intValue() == -1 && remaining != buf.remaining()) {
					future.completeExceptionally(new XFileReadException("Async reader returned -1 but apparently wrote some data too.  buf="+buf+" remainingPrevious="+remaining));
				} else {
					future.complete(result);
				}
			}
			@Override
			public void failed(Throwable exc, String attachment) {
				log.error("Failed to read file="+file, exc);
				future.completeExceptionally(new XFileReadException("Failed to read file="+filePathForLogging, exc));
			}
		};
		asyncFile.read(buf, position, "attachment", handler);

		return future;
	}

	public void close() throws IOException {
		asyncFile.close();
	}

	public long length() {
		return file.toFile().length();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy