- FileCopyUtils
- StreamUtils
- 参考资料
Spring 在 org.springframework.util 包提供了很多实用的工具类。今天关心一下Copy 相关的两个:
FileCopyUtilsFileCopyUtils就是对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中。 |
| ——— | ——— | ——————————————————— |
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



