通过本文主要向大家介绍了pipedinputstream,pipedoutputstream,fileoutputstream,outputstream,dataoutputstream等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
java 管道介绍
在java中,PipedOutputStream和PipedInputStream分别是管道输出流和管道输入流。
它们的作用是让多线程可以通过管道进行线程间的通讯。在使用管道通信时,必须将PipedOutputStream和PipedInputStream配套使用。
使用管道通信时,大致的流程是:我们在线程A中向PipedOutputStream中写入数据,这些数据会自动的发送到与PipedOutputStream对应的PipedInputStream中,进而存储在PipedInputStream的缓冲中;此时,线程B通过读取PipedInputStream中的数据。就可以实现,线程A和线程B的通信。
PipedOutputStream和PipedInputStream源码分析
下面介绍PipedOutputStream和PipedInputStream的源码。在阅读它们的源码之前,建议先看看源码后面的示例。待理解管道的作用和用法之后,再看源码,可能更容易理解。
1. PipedOutputStream 源码分析(基于jdk1.7.40)
package java.io; import java.io.*; public class PipedOutputStream extends OutputStream { // 与PipedOutputStream通信的PipedInputStream对象 private PipedInputStream sink; // 构造函数,指定配对的PipedInputStream public PipedOutputStream(PipedInputStream snk) throws IOException { connect(snk); } // 构造函数 public PipedOutputStream() { } // 将“管道输出流” 和 “管道输入流”连接。 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; // 初始化“管道输入流”的读写位置 // int是PipedInputStream中定义的,代表“管道输入流”的读写位置 snk.in = -1; // 初始化“管道输出流”的读写位置。 // out是PipedInputStream中定义的,代表“管道输出流”的读写位置 snk.out = 0; // 设置“管道输入流”和“管道输出流”为已连接状态 // connected是PipedInputStream中定义的,用于表示“管道输入流与管道输出流”是否已经连接 snk.connected = true; } // 将int类型b写入“管道输出流”中。 // 将b写入“管道输出流”之后,它会将b传输给“管道输入流” public void write(int b) throws IOException { if (sink == null) { throw new IOException("Pipe not connected"); } sink.receive(b); } // 将字节数组b写入“管道输出流”中。 // 将数组b写入“管道输出流”之后,它会将其传输给“管道输入流” public void write(byte b[], int off, int len) throws IOException { if (sink == null) { throw new IOException("Pipe not connected"); } else if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } // “管道输入流”接收数据 sink.receive(b, off, len); } // 清空“管道输出流”。 // 这里会调用“管道输入流”的notifyAll(); // 目的是让“管道输入流”放弃对当前资源的占有,让其它的等待线程(等待读取管道输出流的线程)读取“管道输出流”的值。 public synchronized void flush() throws IOException { if (sink != null) { synchronized (sink) { sink.notifyAll(); } } } // 关闭“管道输出流”。 // 关闭之后,会调用receivedLast()通知“管道输入流”它已经关闭。 public void close() throws IOException { if (sink != null) { sink.receivedLast(); } } }</div>
2. PipedInputStream 源码分析(基于jdk1.7.40)
package java.io; public class PipedInputStream extends InputStream { // “管道输出流”是否关闭的标记 boolean closedByWriter = false; // “管道输入流”是否关闭的标记 volatile boolean closedByReader = false; // “管道输入流”与“管道输出流”是否连接的标记 // 它在PipedOutputStream的connect()连接函数中被设置为true boolean connected = false; Thread readSide; // 读取“管道”数据的线程 Thread writeSide; // 向“管道”写入数据的线程 // “管道”的默认大小 private static final int DEFAULT_PIPE_SIZE = 1024; protected static final int PIPE_SIZE = DEFAULT_PIPE_SIZE; // 缓冲区 protected byte buffer[]; //下一个写入字节的位置。in==out代表满,说明“写入的数据”全部被读取了。 protected int in = -1; //下一个读取字节的位置。in==out代表满,说明“写入的数据”全部被读取了。 protected int out = 0; // 构造函数:指定与“管道输入流”关联的“管道输出流” public PipedInputStream(PipedOutputStream src) throws IOException { this(src, DEFAULT_PIPE_SIZE); } // 构造函数:指定与“管道输入流”关联的“管道输出流”,以及“缓冲区大小” public PipedInputStream(PipedOutputStream src, int pipeSize) throws IOException { initPipe(pipeSize); connect(src); } // 构造函数:默认缓冲区大小是1024字节 public PipedInputStream() { initPipe(DEFAULT_PIPE_SIZE); } // 构造函数:指定缓冲区大小是pipeSize public PipedInputStream(int pipeSize) { initPipe(pipeSize); } // 初始化“管道”:新建缓冲区大小 private void initPipe(int pipeSize) { if (pipeSize <= 0) { throw new IllegalArgumentException("Pipe Size <= 0"); } buffer = new byte[pipeSize]; } // 将“管道输入流”和“管道输出流”绑定。 // 实际上,这里调用的是PipedOutputStream的connect()函数 public void connect(PipedOutputStream src) throws IOException { src.connect(this); } // 接收int类型的数据b。 // 它只会在PipedOutputStream的write(int b)中会被调用 protected synchronized void receive(int b) throws IOException { // 检查管道状态 checkStateForReceive(); // 获取“写入管道”的线程 writeSide = Thread.currentThread(); // 若“写入管道”的数据正好全部被读取完,则等待。 if (in == out) awaitSpace(); if (in < 0) { in = 0; out = 0; } // 将b保存到缓冲区 buffer[in++] = (byte)(b & 0xFF); if (in >= buffer.length) { in = 0; } } // 接收字节数组b。 synchronized void receive(byte b[], int off, int len) throws IOException { // 检查管道状态 checkStateForReceive(); // 获取“写入管道”的线程 writeSide = Thread.currentThread(); int bytesToTransfer = len; while (bytesToTransfer > 0) { // 若“写入管道”的数据正好全部被读取完,则等待。 if (in == out) awaitSpace(); int nextTransferAmount = 0; // 如果“管道中被读取的数据,少于写入管道的数据”; // 则设置nextTransferAmount=“buffer.length - in” if (out < in) { nextTransferAmount = buffer.length - in; } else if (in < out) { // 如果“管道中被读取的数据,大于/等于写入管道的数据”,则执行后面的操作 // 若in==-1(即管道的写入数据等于被读取数据),此时nextTransferAmount = buffer.length - in; // 否则,nextTransferAmount = out - in; if (in == -1) { in = out = 0; nextTransferAmount = buffer.length - in; } else { nextTransferAmount = out - in; } } if (nextTransferAmount > bytesToTransfer) nextTransferAmount = bytesToTransfer; // assert断言的作用是,若nextTransferAmount <= 0,则终止程序。 assert(nextTransferAmount > 0); // 将数据写入到缓冲中 System.arraycopy(b, off, buffer, in, nextTransferAmount); bytesToTransfer -= nextTransferAmount; off += nextTransferAmount; in += nextTransferAmount; if (in >= buffer.length) { in = 0; } } } // 检查管道状态 private void checkStateForReceive() throws IOException { if (!connected) { throw new IOException("Pipe not connected"); } else if (closedByWriter || closedByReader) { throw new IOException("Pipe closed"); } else if (readSide !=