package com.qianwen.core.tool.utils;
|
|
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;
|
|
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/IoUtil.class */
|
public class IoUtil extends StreamUtils {
|
public static void closeQuietly(@Nullable Closeable closeable) {
|
if (closeable == null) {
|
return;
|
}
|
if (closeable instanceof Flushable) {
|
try {
|
((Flushable) closeable).flush();
|
} catch (IOException e) {
|
}
|
}
|
try {
|
closeable.close();
|
} catch (IOException e2) {
|
}
|
}
|
|
public static String readToString(InputStream input) {
|
return readToString(input, Charsets.UTF_8);
|
}
|
|
public static String readToString(@Nullable InputStream input, Charset charset) {
|
try {
|
try {
|
String copyToString = copyToString(input, charset);
|
closeQuietly(input);
|
return copyToString;
|
} catch (IOException e) {
|
throw Exceptions.unchecked(e);
|
}
|
} catch (Throwable th) {
|
closeQuietly(input);
|
throw th;
|
}
|
}
|
|
public static byte[] readToByteArray(@Nullable InputStream input) {
|
try {
|
try {
|
byte[] copyToByteArray = copyToByteArray(input);
|
closeQuietly(input);
|
return copyToByteArray;
|
} catch (IOException e) {
|
throw Exceptions.unchecked(e);
|
}
|
} catch (Throwable th) {
|
closeQuietly(input);
|
throw th;
|
}
|
}
|
|
public static void write(@Nullable final String data, final OutputStream output, final Charset encoding) throws IOException {
|
if (data != null) {
|
output.write(data.getBytes(encoding));
|
}
|
}
|
}
|