public class IOUtil {
/**
* inputStream转outputStream,输入流转输出流
*
* @param inputStream 输入流
* @return 返回输出流
* @throws IOException
*/
public static ByteArrayOutputStream parse(InputStream inputStream) throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int ch;
while ((ch = inputStream.read()) != -1) {
swapStream.write(ch);
}
return swapStream;
}
/**
* outputStream转inputStream,输出流转输入流
*
* @param out 输出流
* @return 输入流
*/
public static ByteArrayInputStream parse(OutputStream out) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream;
}
/**
* 关闭inputStream
*
* @param in inputStream
*/
public static void close(InputStream in) {
if (null != in) {
try {
in.close();
} catch (IOException e) {
log.error("close inputStream error", e);
}
}
}
/**
* 关闭reader
*
* @param reader reader
*/
public static void close(Reader reader) {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
log.error("close reader error", e);
}
}
}
/**
* 关闭OutputStream
*
* @param out OutputStream
*/
public static void close(OutputStream out) {
if (null != out) {
try {
out.close();
} catch (IOException e) {
log.error("close OutputStream error", e);
}
}
}
/**
* 关闭writer
*
* @param writer writer
*/
public static void close(Writer writer) {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
log.error("close writer error", e);
}
}
}
}
输入流转输出流
最新推荐文章于 2024-04-16 14:36:35 发布