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

com.github.yoojia.halo.DownloadTransferAdapter Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.github.yoojia.halo;

import java.io.File;
import java.io.FileInputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Path;

/**
 * @author YOOJIA.CHEN ([email protected])
 */
final class DownloadTransferAdapter {

    private final File inputFile;

    private DownloadTransferAdapter(Path path) {
        inputFile = path.toFile();
    }

    static DownloadTransferAdapter use(Path path) {
        return new DownloadTransferAdapter(path);
    }

    public void dispatch(HaloResponse response) throws Exception {
        try(FileChannel in = new FileInputStream(inputFile).getChannel();
            WritableByteChannel out = Channels.newChannel(response.httpResponse.getOutputStream())){
            in.transferTo(0, in.size(), out);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy