通过ByteArrayOutputStream将inputStream转变为outputStream
通过ByteArrayOutputStream将inputStream转变为outputStream
1.首先,先编写一个获取文件输入流的方法 ops()。
2. 之后在调用此方法获得输入流,并通过 castToBAOStream(Object obj,int bufferSize)进行转化。
3.最后,写出 转化成功的 输出流。
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
Java代码
public class Stream {
public static final int StreamFlushBufferSzie=100;//buffer size= 1K
public static ByteArrayOutputStream castToBAOStream(Object obj,int bufferSize){
// Log logger = LogFactory.getLog(CommonUtil.class);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
if(obj.getClass().isInstance(baos)){
return (ByteArrayOutputStream)obj;
}else{
InputStream fis;
try{
fis=(FileInputStream)obj;
try{
BufferedInputStream bis=new BufferedInputStream(fis);
baos=new ByteArrayOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(baos);
int ch;
int i=0;
while((ch=bis.read())!=-1){
bos.write(ch);
if(i++==bufferSize){
bos.flush();
i=0;
}
}
bos.flush(); //提交文件流,很关键
bis.close();
return baos;
}catch(ClassCastException e){
// logger.info("Stream object not a ByteArrayOutputStream or a FileInputStream:",e);
return null;
}catch(Exception e){
// logger.info("baos is null:",e);
 
补充:移动开发 , Android ,