Spring 在 org.springframework.util 包提供了很多实用的工具类。今天关心一下Copy
相关的两个:
FileCopyUtils
FileCopyUtils
就是对StreamUtils
的copy
方法进行了封装,在每次复制完毕后关闭流
。(因为StreamUtils
在复制完成后流都是保持打开的。)
修饰符 | 返回值 | 方法 | 介绍 |
---|---|---|---|
static | void | copy(byte[] in, File out) | 将给定字节数组 的内容复制到给定输出文件 。 |
static | void | copy(byte[] in, OutputStream out) | 将给定字节数组 的内容复制到给定的OutputStream 。 |
static | int | copy(File in, File out) | 将给定输入File 的内容复制到给定输出File 。 |
static | int | copy(InputStream in, OutputStream out) | 将给定的InputStream 的内容复制到给定的OutputStream 。 |
static | int | copy(Reader in, Writer out) | 将给定Reade 的内容复制到给定Writer 。 |
static | void | copy(String in, Writer out) | 将给定String 的内容复制到给定Writer 。 |
static | byte[] | copyToByteArray(File in) | 将给定输入File 的内容复制到一个新的字节数组 中。 |
static | byte[] | copyToByteArray(InputStream in) | 将给定的InputStream 的内容复制到一个新的字节数组 中。 |
static | String | copyToString(Reader in) | 将给定Reader 的内容复制到String 中。 |
StreamUtils
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;
/**
* 处理流的简单实用方法。 此类中与 {@link FileCopyUtils} 的 copy 方法区别在于,不会关闭流。
* 所有 copy 方法的缓冲区大小都是 4096 字节。
*
* <p>主要用于Spring框架, 但对于应用级别代码也很有用。
* @see FileCopyUtils
*/
public abstract class StreamUtils {
/**
* 用于 copy 的默认缓冲区大小.
*/
public static final int BUFFER_SIZE = 4096;
private static final byte[] EMPTY_CONTENT = new byte[0];
/**
* 将给定的 {@link InputStream} 内容复制到新的 {@code byte[]} 中,完成后保持流的打开状态。
* @param in 作为数据来源的输入流(可能为 {@code null} 或 空)
* @return 一个接收了数据的新字节数组 byte[] (可能为 空)
* @throws IOException 可能抛IO异常
*/
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();
}
/**
* 将给定的 {@link InputStream} 内容复制到 {@link String} 中,完成后保持流的打开状态。
* @param in 作为数据来源的输入流(可能为 {@code null} 或 空)
* @param charset {@link Charset}解码所需字符集
* @return 一个接收了数据的字符串 String (可能为 空)
* @throws IOException 可能抛IO异常
*/
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();
}
/**
* 将给定的 {@link ByteArrayOutputStream} 内容复制到 {@link String} 中,完成后保持流的打开状态。
* <p>比 {@code new String(baos.toByteArray(), charset)} 更有效。
* @param baos 复制到字符串的数据来源 {@code ByteArrayOutputStream}
* @param charset 用于解码的字符集 {@link Charset}
* @return 复制得到的字符串 (可能为 空)
* @since 5.2.6
*/
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);
}
}
/**
* 将给定的 {@code byte[]} 字节数组内容复制到给定的 {@link OutputStream} 中,完成后保持流的打开状态。
* @param in 作为来源的字节数组
* @param out 复制到目标输出流
* @throws IOException 可能抛IO异常
*/
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();
}
/**
* 将给定的字符串 {@link String} 内容复制到给定的 {@link OutputStream} 中,完成后保持流的打开状态。
* @param in 数据来源:字符串
* @param charset 来源字符串对应的字符集
* @param out 复制到目标输出流
* @throws IOException 可能抛IO异常
*/
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();
}
/**
* 将给定的 {@link InputStream} 内容复制到给定的 {@link OutputStream} 中,完成后保持流的打开状态。
* @param in 数据来源输入流
* @param out 复制到目标输出流
* @return 复制了多少字节
* @throws IOException 可能抛IO异常
*/
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;
}
/**
* 将给定的 {@link InputStream} 内容复制到给定的 {@link OutputStream} 中,
* <p>如果指定的复制范围朝出了 {@code InputStream} 的大小,则返回实际复制了多少字节。
* <p>两个流完成后都保持打开状态。
* @param in 数据来源输入流
* @param out 复制到目标输出流
* @param start 开始复制的位置
* @param end 结束复制的位置
* @return 复制了多少字节
* @throws IOException 可能抛IO异常
* @since 4.3
*/
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);
}
/**
* 清除给定流中的剩余内容,完成后保持流的打开状态。
* @param in 需要清空的输入流
* @return 读取到多少字节
* @throws IOException 可能抛IO异常
* @since 4.3
*/
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;
}
/**
* 返回一个有效的输入流 {@link InputStream}
* @return 一个用空字节数组创建的 {@link ByteArrayInputStream}
* @since 4.2.2
*/
public static InputStream emptyInput() {
return new ByteArrayInputStream(EMPTY_CONTENT);
}
/**
* 将给定的 {@link InputStream} 装饰成一个 {@link InputStream#close() .close()} 无效的变种
* @param in 需要被装饰的输入流
* @return 一个调用 {@link InputStream#close() close()} 没效果的 InputStream 版本
*/
public static InputStream nonClosing(InputStream in) {
Assert.notNull(in, "No InputStream specified");
return new org.springframework.util.StreamUtils.NonClosingInputStream(in);
}
/**
* 将给定的 {@link OutputStream} 装饰成一个 {@link OutputStream#close() .close()} 无效的变种
* @param out 需要被装饰的输出流
* @return 一个调用 {@link OutputStream#close() close()} 没效果的 OutputStream 版本
*/
public static OutputStream nonClosing(OutputStream out) {
Assert.notNull(out, "No OutputStream specified");
return new org.springframework.util.StreamUtils.NonClosingOutputStream(out);
}
/**
* 静态内部类,重写了 close()
*/
private static class NonClosingInputStream extends FilterInputStream {
public NonClosingInputStream(InputStream in) {
super(in);
}
@Override
public void close() throws IOException {
}
}
/**
* 静态内部类,重写了 close()
*/
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