java基础-IO(4)管道流 PipedOutputStream、PipedInputStream、PipedReader、PipedWriter

管道

提到管道,第一印象就是水管或者地下管道,一节接着一节,形如下图。
在这里插入图片描述

管道流

"流"从上一个管道 ——-> 下一个管道。又细分为字节流和字符流。
在这里插入图片描述


字节流(PipedOutputStream、PipedInputStream)
在这里插入图片描述
如果从上一个管道到下一个管道,只是走个过场岂不是没有任何意义,既然要处理就需要逗留,逗留就意味着需要暂存,字节流使用字节数组存储,字符流使用字符数据存储。
在这里插入图片描述


PipedOutputStream 核心源码:

PipedOutputStream extends OutputStream

//指向下一节"管道"
private PipedInputStream sink;

//向外写数据(流出当前管道)
write(*****);

//构造方法
public PipedOutputStream(PipedInputStream snk)  throws IOException {
    connect(snk);
 }

//连接下一个"管道"
public synchronized void connect(PipedInputStream snk) throws IOException {
  if (snk == null) {
        throw new NullPointerException();

//一个管道只能被一个管道连接
    } else if (sink != null || snk.connected) {  
        throw new IOException("Already connected");
    }
    sink = snk;
    snk.in = -1;
    snk.out = 0;
    
	//管道已被连接的标识
    snk.connected = true;
}

PipedInputStream 核心源码:


PipedInputStream extends InputStream

//数据暂存的地方
protected byte buffer[];

//数组默认大小
private static final int DEFAULT_PIPE_SIZE = 1024;

//是否已经被连接的标识
boolean connected = false;

//构造方法
public PipedInputStream() {
    initPipe(DEFAULT_PIPE_SIZE);
}

private void initPipe(int pipeSize) {
 if (pipeSize <= 0) {
        throw new IllegalArgumentException("Pipe Size <= 0");
     }
     
     buffer = new byte[pipeSize];
}

//自定义数组大小
public PipedInputStream(int pipeSize) {
    initPipe(pipeSize);
}

//指定上一个"管道是谁"
public PipedInputStream(PipedOutputStream src) throws IOException {
    this(src, DEFAULT_PIPE_SIZE);
}

//即指明上一个"管道是谁",又自定义数组大小
public PipedInputStream(PipedOutputStream src, int pipeSize)
        throws IOException {
     initPipe(pipeSize);
     connect(src);
}

在这里插入图片描述


案例:一个字符串从上一个管道写到下一个管道。
在这里插入图片描述

 public static void main(String[] args) throws IOException {

   PipedOutputStream po = new PipedOutputStream();
    PipedInputStream pi = new PipedInputStream();
    Thread t1 = new Thread(() -> {
        try {
            int b;
            while ((b = pi.read()) != -1) {
                System.out.print((char) b);
            }
            pi.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });


//链接管道
    po.connect(pi);

    String str1 = "hello world";
    byte[] bytes = str1.getBytes();

    Thread t2 = new Thread(() -> {

        try {
            po.write(bytes);
            po.flush();
            po.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    });

    t2.start();
    t1.start();

}

PipedReader、PipedWriter 字符管道类似于字节管道,只不过使用字符数组存储数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值