package com.wen;
import java.io.*;
/**
* 提供 IO 操作的工具类
* @version 1.0
* @author explorer
*/
public final class StreamUtil {
/**
* 将输入流复制到输出流
* @param is
* @param os
* @throws IOException
*/
public final static void write(InputStream is, OutputStream os) throws
IOException {
BufferedInputStream bis = new BufferedInputStream(is, 1024);
int c = bis.read();
while (c != -1) {
os.write(c);
c = bis.read();
}
os.flush();
}
/**
* 将输入流按指定的编码转成字符串
* @param is
* @param encoding
* @return
* @throws IOException
*/
public final static String toString(InputStream is, String encoding) throws
IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
write(is, bos);
if (encoding == null) {
return new String(bos.toByteArray());
}
else {
return new String(bos.toByteArray(), encoding);
}
}
/**
* 将输入流按默认的编码转成字符串
* @param is
* @return
* @throws IOException
*/
public final static String toString(InputStream is) throws IOException {
return toString(is, null);
}
/**
* 关闭输入流,忽略异常
* @param is
*/
public final static void closeQuitely(InputStream is) {
try {
if (is != null) {
is.close();
}
}
catch (IOException e) {
}
}
/**
* 关闭输出流,忽略异常
* @param os
*/
public final static void closeQuitely(OutputStream os) {
try {
if (os != null) {
os.close();
}
}
catch (IOException e) {
}
}
/**
* 半闭输入输出流,忽略异常
* @param is
* @param os
*/
public final static void closeQuitely(InputStream is, OutputStream os) {
closeQuitely(is);
closeQuitely(os);
}
}