JAVA Web项目中常用工具类之 IoUtil
Posted 洛阳泰山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA Web项目中常用工具类之 IoUtil相关的知识,希望对你有一定的参考价值。
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;
public class IoUtil extends StreamUtils {
public IoUtil() {
}
public static void closeQuietly(@Nullable Closeable closeable) {
if (closeable != null) {
if (closeable instanceof Flushable) {
try {
((Flushable)closeable).flush();
} catch (IOException var3) {
}
}
try {
closeable.close();
} catch (IOException var2) {
}
}
}
public static String readToString(InputStream input) {
return readToString(input, Charsets.UTF_8);
}
public static String readToString(@Nullable InputStream input, Charset charset) {
String var2;
try {
var2 = copyToString(input, charset);
} catch (IOException var6) {
throw Exceptions.unchecked(var6);
} finally {
closeQuietly(input);
}
return var2;
}
public static byte[] readToByteArray(@Nullable InputStream input) {
byte[] var1;
try {
var1 = copyToByteArray(input);
} catch (IOException var5) {
throw Exceptions.unchecked(var5);
} finally {
closeQuietly(input);
}
return var1;
}
public static void write(@Nullable final String data, final OutputStream output, final Charset encoding) throws IOException {
if (data != null) {
output.write(data.getBytes(encoding));
}
}
}
以上是关于JAVA Web项目中常用工具类之 IoUtil的主要内容,如果未能解决你的问题,请参考以下文章