com.github.dennisit.vplus.data.utils.IOUtils Maven / Gradle / Ivy
package com.github.dennisit.vplus.data.utils;
import lombok.experimental.UtilityClass;
import org.apache.commons.codec.Charsets;
import org.springframework.lang.Nullable;
import java.io.*;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.Collection;
@UtilityClass
public class IOUtils extends org.springframework.util.StreamUtils {
/**
* 默认缓存大小
*/
public final static int DEFAULT_BUFFER_SIZE = 1024;
public static void write(CharSequence data, OutputStream output, String encoding) throws IOException {
IOUtils.write(data, output, encoding);
}
/**
* 将Reader中的内容复制到Writer中
* 使用默认缓存大小
*
* @param reader Reader
* @param writer Writer
* @return 拷贝的字节数
* @throws IOException 异常
*/
public static int copy(Reader reader, Writer writer) throws IOException {
return copy(reader, writer, DEFAULT_BUFFER_SIZE);
}
/**
* 将Reader中的内容复制到Writer中
*
* @param reader Reader
* @param writer Writer
* @param bufferSize 缓存大小
* @return 传输的byte数
* @throws IOException 异常
*/
public static int copy(Reader reader, Writer writer, int bufferSize) throws IOException {
char[] buffer = new char[bufferSize];
int count = 0;
int readSize;
while ((readSize = reader.read(buffer, 0, bufferSize)) >= 0) {
writer.write(buffer, 0, readSize);
count += readSize;
writer.flush();
}
return count;
}
/**
* 拷贝流,使用默认Buffer大小
*
* @param in 输入流
* @param out 输出流
* @return 传输的byte数
* @throws IOException 异常
*/
public static int copy(InputStream in, OutputStream out) throws IOException {
return copy(in, out, DEFAULT_BUFFER_SIZE);
}
/**
* 拷贝流
*
* @param in 输入流
* @param out 输出流
* @param bufferSize 缓存大小
* @return 传输的byte数
* @throws IOException 异常
*/
public static int copy(InputStream in, OutputStream out, int bufferSize) throws IOException {
byte[] buffer = new byte[bufferSize];
int count = 0;
for (int n = -1; (n = in.read(buffer)) != -1; ) {
out.write(buffer, 0, n);
count += n;
out.flush();
}
return count;
}
/**
* 拷贝文件流,使用NIO
*
* @param in 输入
* @param out 输出
* @return 拷贝的字节数
* @throws IOException 异常
*/
public static long copy(FileInputStream in, FileOutputStream out) throws IOException {
FileChannel inChannel = in.getChannel();
FileChannel outChannel = out.getChannel();
return inChannel.transferTo(0, inChannel.size(), outChannel);
}
//-------------------------------------------------------------------------------------- Copy end
/**
* 获得一个文件读取器
*
* @param in 输入流
* @param charsetName 字符集名称
* @return BufferedReader对象
* @throws IOException 异常
*/
public static BufferedReader getReader(InputStream in, String charsetName) throws IOException {
return getReader(in, Charset.forName(charsetName));
}
/**
* 获得一个文件读取器
*
* @param in 输入流
* @param charset 字符集
* @return BufferedReader对象
* @throws IOException 异常
*/
public static BufferedReader getReader(InputStream in, Charset charset) throws IOException {
if (null == in) {
return null;
}
InputStreamReader reader = null;
if (null == charset) {
reader = new InputStreamReader(in);
} else {
reader = new InputStreamReader(in, charset);
}
return new BufferedReader(reader);
}
/**
* 从Reader中读取String
*
* @param reader Reader
* @return String
* @throws IOException 异常
*/
public static String read(Reader reader) throws IOException {
final StringBuilder builder = new StringBuilder();
final CharBuffer buffer = CharBuffer.allocate(DEFAULT_BUFFER_SIZE);
while (-1 != reader.read(buffer)) {
builder.append(buffer.flip().toString());
}
return builder.toString();
}
public static > T readLines(InputStream in, String charset, T collection) throws IOException {
// 从返回的内容中读取所需内容
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
String line = null;
while ((line = reader.readLine()) != null) {
collection.add(line);
}
return collection;
}
/**
* String 转为 流
*
* @param content 内容
* @param charset 编码
* @return 字节流
*/
public static ByteArrayInputStream toStream(String content, String charset) {
if (content == null) {
return null;
}
byte[] data = null;
try {
data = StringUtils.isBlank(charset) ? content.getBytes() : content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Invalid charset" + charset, e);
}
return new ByteArrayInputStream(data);
}
/**
* 打印内容,调用系统的System.out.println方法
*
* @param content 内容,会调用toString方法, 当内容中有 {} 表示变量占位符
* @param param 参数
*/
public static void echo(Object content, Object... param) {
if (content == null) {
System.out.println(content);
}
System.out.println(StringUtils.format(content.toString(), param));
}
/**
* 关闭
*
* @param closeable 被关闭的对象
*/
public static void close(Closeable closeable) {
if (closeable == null) return;
try {
closeable.close();
} catch (IOException e) {
}
}
/**
* InputStream to String utf-8
*
* @param input the InputStream
to read from
* @return the requested String
*/
public static String readToString(InputStream input) {
return readToString(input, Charsets.UTF_8);
}
/**
* InputStream to String
*
* @param input the InputStream
to read from
* @param charset the Charset
* @return the requested String
*/
public static String readToString(@Nullable InputStream input, Charset charset) {
try {
return copyToString(input, charset);
} catch (IOException e) {
throw Exceptions.unchecked(e);
} finally {
closeQuietly(input);
}
}
public static byte[] readToByteArray(@Nullable InputStream input) {
try {
return copyToByteArray(input);
} catch (IOException e) {
throw Exceptions.unchecked(e);
} finally {
closeQuietly(input);
}
}
/**
* Writes chars from a String
to bytes on an
* OutputStream
using the specified character encoding.
*
* This method uses {@link String#getBytes(String)}.
*
*
* @param data the String
to write, null ignored
* @param output the OutputStream
to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
*/
public static void write(@Nullable final String data, final OutputStream output, final Charset encoding) throws IOException {
if (data != null) {
output.write(data.getBytes(encoding));
}
}
/**
* closeQuietly
*
* @param closeable 自动关闭
*/
public static void closeQuietly(@Nullable Closeable closeable) {
if (closeable == null) {
return;
}
if (closeable instanceof Flushable) {
try {
((Flushable) closeable).flush();
} catch (IOException ignored) {
// ignore
}
}
try {
closeable.close();
} catch (IOException ignored) {
// ignore
}
}
}