cn.novelweb.tool.upload.fastdfs.utils.IoUtils Maven / Gradle / Ivy
package cn.novelweb.tool.upload.fastdfs.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
/**
* IO工具
* 2020-02-03 16:30
*
* @author LiZW
**/
@Slf4j
public class IoUtils {
private static final int EOF = -1;
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
public static void closeQuietly(final Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (final IOException ioe) {
log.error("数据流关闭异常", ioe);
}
}
public static void closeQuietly(final Closeable... closeables) {
if (closeables == null) {
return;
}
for (final Closeable closeable : closeables) {
closeQuietly(closeable);
}
}
private static long copyLarge(final InputStream input, final OutputStream output, final byte[] buffer)
throws IOException {
long count = 0;
int n;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
private static long copyLarge(final InputStream input, final OutputStream output)
throws IOException {
return copy(input, output, DEFAULT_BUFFER_SIZE);
}
private static long copy(final InputStream input, final OutputStream output, final int bufferSize)
throws IOException {
return copyLarge(input, output, new byte[bufferSize]);
}
public static int copy(final InputStream input, final OutputStream output) throws IOException {
final long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
public static byte[] toByteArray(final InputStream input) throws IOException {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
return output.toByteArray();
}
}