Why doesn't more Java code use PipedInputStream / PipedOutputStream?

前端 未结 9 2124
感动是毒
感动是毒 2020-12-02 12:56

I\'ve discovered this idiom recently, and I am wondering if there is something I am missing. I\'ve never seen it used. Nearly all Java code I\'ve worked with in the wild fav

9条回答
  •  伪装坚强ぢ
    2020-12-02 13:23

    I too only discovered the PipedInputStream/PipedOutputStream classes recently.

    I am developing an Eclipse plug-in that needs to execute commands on a remote server via SSH. I am using JSch and the Channel API reads from an input stream and writes to an output stream. But I need to feed commands through the input stream and read the responses from an output stream. Thats where PipedInput/OutputStream comes in.

    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    
    import com.jcraft.jsch.Channel;
    
    Channel channel;
    PipedInputStream channelInputStream = new PipedInputStream();
    PipedOutputStream channelOutputStream = new PipedOutputStream();
    
    channel.setInputStream(new PipedInputStream(this.channelOutputStream));
    channel.setOutputStream(new PipedOutputStream(this.channelInputStream));
    channel.connect();
    
    // Write to channelInputStream
    // Read from channelInputStream
    
    channel.disconnect();
    

提交回复
热议问题