public class StreamUtils {
public static void readData(InputStream is, OutputStream os)
throws IOException {
if (is != null && os != null) {
BufferedInputStream in = new BufferedInputStream(is);
BufferedOutputStream out = new BufferedOutputStream(os);
int len = -1;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
out.close();
os.close();
in.close();
is.close();
}
}
public static byte[] getBytes(InputStream is) throws IOException {
byte[] bytes = null;
if (is != null) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
readData(is, os);
bytes = os.toByteArray();
}
return bytes;
}
public static void save(InputStream is, File savePath) throws IOException,
FileNotFoundException {
if (is != null && savePath != null) {
// 如果父目录不存在,则创建该目录
if (!savePath.getParentFile().exists()) {
savePath.getParentFile().mkdirs();
}
// 创建文件输出流,保存
FileOutputStream os = new FileOutputStream(savePath);
readData(is, os);
}
}
}
Android 缓存流(BufferedInputStream和BufferedOutputStream)
最新推荐文章于 2024-08-18 03:23:17 发布