Java: pipe OutputStream to InputStream








  • Method 1: use ByteArrayInputStream & ByteArrayOutputStream:
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
        // write
        writeDataToOutputStream(baos);
        
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        
        // read
        readDataFromInputStream(bais);
    
    
  • Method 2: use PipedInputStream & PipedOutputStream:
    
        PipedInputStream pis = new PipedInputStream();
        PipedOutputStream pos = new PipedOutputStream(pis);
        // write
        writeDataToOutputStream(pos);
    
        // read
        readDataFromInputStream(pis);
    
    

See Also




No comments:

Post a Comment