栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 学习笔记 - Spring工具类:FileCopyUtils、StreamUtils

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 学习笔记 - Spring工具类:FileCopyUtils、StreamUtils

Java 学习笔记 - Spring工具类:FileCopyUtils、StreamUtils
  • FileCopyUtils
  • StreamUtils
  • 参考资料

Spring 在 org.springframework.util 包提供了很多实用的工具类。今天关心一下Copy 相关的两个:

FileCopyUtils

FileCopyUtils就是对StreamUtils的copy方法进行了封装,在每次复制完毕后关闭流。(因为StreamUtils在复制完成后流都是保持打开的。)

修饰符返回值方法介绍
staticvoidcopy(byte[] in, File out)将给定字节数组的内容复制到给定输出文件。
staticvoidcopy(byte[] in, OutputStream out)将给定字节数组的内容复制到给定的OutputStream。
staticintcopy(File in, File out)将给定输入File的内容复制到给定输出File。
staticintcopy(InputStream in, OutputStream out)将给定的InputStream的内容复制到给定的OutputStream。
staticintcopy(Reader in, Writer out)将给定Reade的内容复制到给定Writer。
staticvoidcopy(String in, Writer out)将给定String的内容复制到给定Writer。
staticbyte[]copyToByteArray(File in)将给定输入File的内容复制到一个新的字节数组中。
staticbyte[]copyToByteArray(InputStream in)将给定的InputStream的内容复制到一个新的字节数组中。
staticStringcopyToString(Reader in)将给定Reader的内容复制到String中。
—————————————————————————
StreamUtils
package org.springframework.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;


public abstract class StreamUtils {

    
    public static final int BUFFER_SIZE = 4096;

    private static final byte[] EMPTY_CONTENT = new byte[0];


    
    public static byte[] copyToByteArray(@Nullable InputStream in) throws IOException {
        if (in == null) {
            return new byte[0];
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
        copy(in, out);
        return out.toByteArray();
    }

    
    public static String copyToString(@Nullable InputStream in, Charset charset) throws IOException {
        if (in == null) {
            return "";
        }

        StringBuilder out = new StringBuilder(BUFFER_SIZE);
        InputStreamReader reader = new InputStreamReader(in, charset);
        char[] buffer = new char[BUFFER_SIZE];
        int charsRead;
        while ((charsRead = reader.read(buffer)) != -1) {
            out.append(buffer, 0, charsRead);
        }
        return out.toString();
    }

    
    public static String copyToString(ByteArrayOutputStream baos, Charset charset) {
        Assert.notNull(baos, "No ByteArrayOutputStream specified");
        Assert.notNull(charset, "No Charset specified");
        try {
            // 在 Java 10+ 也可以换成 toString(Charset)
            return baos.toString(charset.name());
        }
        catch (UnsupportedEncodingException ex) {
            // 人祸:字符集名字有误
            throw new IllegalArgumentException("Invalid charset name: " + charset, ex);
        }
    }

    
    public static void copy(byte[] in, OutputStream out) throws IOException {
        Assert.notNull(in, "No input byte array specified");
        Assert.notNull(out, "No OutputStream specified");

        out.write(in);
        out.flush();
    }

    
    public static void copy(String in, Charset charset, OutputStream out) throws IOException {
        Assert.notNull(in, "No input String specified");
        Assert.notNull(charset, "No Charset specified");
        Assert.notNull(out, "No OutputStream specified");

        Writer writer = new OutputStreamWriter(out, charset);
        writer.write(in);
        writer.flush();
    }

    
    public static int copy(InputStream in, OutputStream out) throws IOException {
        Assert.notNull(in, "No InputStream specified");
        Assert.notNull(out, "No OutputStream specified");

        int byteCount = 0;
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        out.flush();
        return byteCount;
    }

    
    public static long copyRange(InputStream in, OutputStream out, long start, long end) throws IOException {
        Assert.notNull(in, "No InputStream specified");
        Assert.notNull(out, "No OutputStream specified");

        long skipped = in.skip(start);
        if (skipped < start) {
            throw new IOException("Skipped only " + skipped + " bytes out of " + start + " required");
        }

        long bytesToCopy = end - start + 1;
        byte[] buffer = new byte[(int) Math.min(org.springframework.util.StreamUtils.BUFFER_SIZE, bytesToCopy)];
        while (bytesToCopy > 0) {
            int bytesRead = in.read(buffer);
            if (bytesRead == -1) {
                break;
            }
            else if (bytesRead <= bytesToCopy) {
                out.write(buffer, 0, bytesRead);
                bytesToCopy -= bytesRead;
            }
            else {
                out.write(buffer, 0, (int) bytesToCopy);
                bytesToCopy = 0;
            }
        }
        return (end - start + 1 - bytesToCopy);
    }

    
    public static int drain(InputStream in) throws IOException {
        Assert.notNull(in, "No InputStream specified");
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;
        int byteCount = 0;
        while ((bytesRead = in.read(buffer)) != -1) {
            byteCount += bytesRead;
        }
        return byteCount;
    }

    
    public static InputStream emptyInput() {
        return new ByteArrayInputStream(EMPTY_CONTENT);
    }

    
    public static InputStream nonClosing(InputStream in) {
        Assert.notNull(in, "No InputStream specified");
        return new org.springframework.util.StreamUtils.NonClosingInputStream(in);
    }

    
    public static OutputStream nonClosing(OutputStream out) {
        Assert.notNull(out, "No OutputStream specified");
        return new org.springframework.util.StreamUtils.NonClosingOutputStream(out);
    }

    
    private static class NonClosingInputStream extends FilterInputStream {
        public NonClosingInputStream(InputStream in) {
            super(in);
        }
        @Override
        public void close() throws IOException {
        }
    }
    
    private static class NonClosingOutputStream extends FilterOutputStream {
        public NonClosingOutputStream(OutputStream out) {
            super(out);
        }
        @Override
        public void write(byte[] b, int off, int let) throws IOException {
            // 为了提高性能,重写这个方法是至关重要的 
            this.out.write(b, off, let);
        }
        @Override
        public void close() throws IOException {
        }
    }

}

参考资料

org.springframework.util.FileCopyUtils
org.springframework.util.StreamUtils

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号