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

matrix.boot.common.utils.NIOStreamUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils;

import matrix.boot.common.exception.ServiceException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * NIO流处理工具
 * @author wangcheng
 * 2021/8/12
 **/
public class NIOStreamUtil {

    /**
     * 文件流输出文件流
     * @param fis 文件输入流
     * @param fos 文件输出流
     */
    public static void streamWriteStream(FileInputStream fis, FileOutputStream fos) {
        FileChannel fileInChannel;
        FileChannel fileOutChannel = null;
        try {
            fileInChannel = fis.getChannel();
            fileOutChannel = fos.getChannel();
            ByteBuffer buf = ByteBuffer.allocate(1000);
            while (fileInChannel.read(buf) != -1) {
                buf.flip();
                while(true) {
                    if (fileOutChannel.write(buf) == 0) break;
                }
                buf.clear();
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        } finally {
            BIOStreamUtil.closeStream(fileOutChannel);
            BIOStreamUtil.closeStream(fileOutChannel);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy