通过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代码 复制代码 收藏代码spinner.gif

  1. public class Stream {    
  2. public static final int StreamFlushBufferSzie=100;//buffer size= 1K  
  3. public static ByteArrayOutputStream castToBAOStream(Object obj,int bufferSize){    
  4. //          Log logger = LogFactory.getLog(CommonUtil.class);  
  5.             ByteArrayOutputStream baos=new ByteArrayOutputStream();    
  6. if(obj.getClass().isInstance(baos)){    
  7. return (ByteArrayOutputStream)obj;    
  8.             }else{    
  9.                 InputStream fis;    
  10. try{    
  11.                     fis=(FileInputStream)obj;    
  12. try{    
  13.                         BufferedInputStream bis=new BufferedInputStream(fis);       
  14.                         baos=new ByteArrayOutputStream();        
  15.                         BufferedOutputStream bos=new BufferedOutputStream(baos);      
  16. int ch;        
  17. int i=0;    
  18. while((ch=bis.read())!=-1){          
  19.                                 bos.write(ch);      
  20. if(i++==bufferSize){    
  21.                                     bos.flush();    
  22.                                     i=0;    
  23.                                 }    
  24.                         }        
  25.                         bos.flush();    //提交文件流,很关键  
  26.                         bis.close();       
  27. return baos;    
  28.                     }catch(ClassCastException e){    
  29. //                      logger.info("Stream object not a ByteArrayOutputStream or a FileInputStream:",e);  
  30. return null;    
  31.                     }catch(Exception e){    
  32. //                      logger.info("baos is null:",e);  
  33. return null;    
  34.                     }    
  35.                 }catch(ClassCastException e){    
  36.                     fis=(InflaterInputStream)obj;    
  37. try{    
  38.                         baos=new ByteArrayOutputStream();        
  39.                         BufferedOutputStream bos=new BufferedOutputStream(baos);      
  40. int ch;        
  41. int i=0;    
  42. //                      ii.read(b, off, len)  
  43. byte[] by = new byte[2048];    
  44. while((ch=fis.read(by))!=-1){          
  45.                                 bos.write(by,0,ch);      
  46. if(i++==bufferSize){    
  47.                                     bos.flush();    
  48.                                     i=0;    
  49.                                 }    
  50.                         }        
  51.                         bos.flush();    //提交文件流,很关键  
  52.                         fis.close();       
  53. //                      ZipUtil.getZipName().close();  
  54. return baos;    
  55.                     }catch(ClassCastException e1){    
  56. //                      logger.info("Stream object not a ByteArrayOutputStream or a FileInputStream:",e1);  
  57. return null;    
  58.                     }catch(Exception e1){    
  59. //                      logger.info("baos is null:",e1);  
  60. return null;    
  61.                     }    
  62.                 }    
  63.             }    
  64.         }    
  65. //产生一个文件输入流  
  66. public static InputStream ops() throws FileNotFoundException {    
  67.          File file = new File("d://lin.txt");       
  68.          FileInputStream fileInputStream = new FileInputStream(file);    
  69. return fileInputStream;    
  70.      }    
  71. public static void main(String[] args) throws IOException {    
  72. try {    
  73. //          OutputStream outputStream = ops();  
  74.             InputStream inputStream = ops();     
  75.             ByteArrayOutputStream baos = castToBAOStream(inputStream,StreamFlushBufferSzie);    
  76.                 InputStream is = new ByteArrayInputStream(baos.toByteArray());    
  77.                 File file=new File("d://new.txt");    
  78.                 FileOutputStream outb=new FileOutputStream(file);    
  79. int bytesRead;    
  80. byte[] buf = new byte[4 * 1024];    
  81. while((bytesRead = is.read(buf))!=-1){    
  82.                                outb.write(buf,0,bytesRead);    
  83.                            }    
  84.                            outb.flush();    
  85.                            outb.close();    
  86.                            is.close();    
  87.              System.out.println(inputStream);    
  88.         } catch (FileNotFoundException e) {    
  89. // TODO Auto-generated catch block  
  90.             e.printStackTrace();    
  91.         }    
  92.     }    
  93. }   
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); return null; } }catch(ClassCastException e){ fis=(InflaterInputStream)obj; try{ baos=new ByteArrayOutputStream(); BufferedOutputStream bos=new BufferedOutputStream(baos); int ch; int i=0; // ii.read(b, off, len) byte[] by = new byte[2048]; while((ch=fis.read(by))!=-1){ bos.write(by,0,ch); if(i++==bufferSize){ bos.flush(); i=0; } } bos.flush(); //提交文件流,很关键 fis.close(); // ZipUtil.getZipName().close(); return baos; }catch(ClassCastException e1){ // logger.info("Stream object not a ByteArrayOutputStream or a FileInputStream:",e1); return null; }catch(Exception e1){ // logger.info("baos is null:",e1); return null; } } } } //产生一个文件输入流 public static InputStream ops() throws FileNotFoundException { File file = new File("d://lin.txt"); FileInputStream fileInputStream = new FileInputStream(file); return fileInputStream; } public static void main(String[] args) throws IOException { try { // OutputStream outputStream = ops(); InputStream inputStream = ops(); ByteArrayOutputStream baos = castToBAOStream(inputStream,StreamFlushBufferSzie); InputStream is = new ByteArrayInputStream(baos.toByteArray()); File file=new File("d://new.txt"); FileOutputStream outb=new FileOutputStream(file); int bytesRead; byte[] buf = new byte[4 * 1024]; while((bytesRead = is.read(buf))!=-1){ outb.write(buf,0,bytesRead); } outb.flush(); outb.close(); is.close(); System.out.println(inputStream); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }