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

管道

提到管道,第一印象就是水管或者地下管道,一节接着一节,形如下图。

管道流

"流"从上一个管道 -------> 下一个管道。又细分为字节流和字符流。


字节流(PipedOutputStream、PipedInputStream)

如果从上一个管道到下一个管道,只是走个过场岂不是没有任何意义,既然要处理就需要逗留,逗留就意味着需要暂存,字节流使用字节数组存储,字符流使用字符数据存储。


PipedOutputStream 核心源码:

java 复制代码
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 核心源码:

java 复制代码
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);
}

案例:一个字符串从上一个管道写到下一个管道。

java 复制代码
 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 字符管道类似于字节管道,只不过使用字符数组存储数据。

相关推荐
DCTANT10 分钟前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.19 分钟前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超28 分钟前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice35 分钟前
对象的finalization机制Test
java·开发语言·jvm
思则变1 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang1 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
¥-oriented1 小时前
【C#中路径相关的概念】
开发语言·c#
CoderCodingNo2 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法
恋猫de小郭2 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
望获linux2 小时前
【实时Linux实战系列】CPU 隔离与屏蔽技术
java·linux·运维·服务器·操作系统·开源软件·嵌入式软件