Java IO Learning (iii) pipeline Introduction, source analysis and examples

Source: Internet
Author: User
Tags constructor thread

Introduction to Pipelines (PipedOutputStream and PipedInputStream), source analysis, and examples

In this chapter, we learn about Java piping.

Java Piping Introduction

In Java, PipedOutputStream and pipedinputstream are pipe output streams and pipe input streams respectively.

Their role is to allow multithreading to communicate between threads through the pipeline. PipedOutputStream and PipedInputStream must be used to support the use of piping communications.

When using duct communication, the approximate process is that we write the data to the PipedOutputStream in thread A, which is automatically sent to the pipedinputstream corresponding to the PipedOutputStream. It is then stored in a PipedInputStream buffer, at which point thread B reads the data in the PipedInputStream. Can be implemented, thread A and thread B communication.

PipedOutputStream and PipedInputStream Source analysis

The following introduces the source code of PipedOutputStream and PipedInputStream. Before reading their source code, it is recommended to look at the example behind the source code. To understand the role and usage of the pipeline, then look at the source code, it may be easier to understand.

In addition, PipedOutputStream's parent class OutputStream has been introduced in the Bytearrayoutputstream of Java IO series 03, source analysis and examples (including outputstream). Here is no longer the introduction of OutputStream.

PipedInputStream's parent class InputStream has been introduced in the introduction to the Bytearrayinputstream of Java IO series 02, source analysis and examples (including InputStream). InputStream is no longer introduced here.

1. PipedOutputStream source Analysis (based on jdk1.7.40)

Package java.io;
     
Import java.io.*; public class PipedOutputStream extends OutputStream {//PipedInputStream object private Pip for communication with PipedOutputStream
     
    Edinputstream sink; constructor that specifies the paired PipedInputStream public PipedOutputStream (PipedInputStream SNK) throws IOException {Connect (SNK
    );
    }//Constructor public PipedOutputStream () {}///pipe output stream and pipeline inflow are connected. Public synchronized void Connect (PipedInputStream snk) throws IOException {if (SNK = null) {throw n
        EW NullPointerException ();
        else if (sink!= null | | snk.connected) {throw new IOException ("Already connected");
        }//Set "pipeline inflow" sink = SNK;
        Initialize the read/write position of "pipeline inflow"/int is defined in PipedInputStream, representing the read-write position of "pipeline inflow" snk.in =-1;
        Initializes the read/write location of the pipe output stream.
        Out is defined in PipedInputStream, representing the read-write position of the "pipe output stream" snk.out = 0; Set the pipeline inflow and pipe output stream as connected//connected is PipedinputStream, used to indicate whether the pipeline inflow and pipeline output stream is connected snk.connected = true;
    //writes int type B to the pipe output stream.
            After writing B to the pipe output stream, it transmits B to the pipeline delivery public void write (int b) throws IOException {if (sink = null) {
        throw new IOException ("Pipe not Connected");
    } sink.receive (b);
    ///writes byte array B to the pipeline output stream. After the array B is written to the pipe output stream, it transmits it to the pipeline delivery 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;
    }//"Pipeline inflow" receives data sink.receive (b, off, Len);
    //Empty the pipe output stream.
This will call the "Pipeline Inflow" notifyall ();    The purpose is to let pipeline inflow discard the possession of the current resource, so that other waiting threads (the thread waiting to read the pipeline output stream) read the value of the pipe output stream.
                Public synchronized void Flush () throws IOException {if (sink!= null) {synchronized (sink) {
            Sink.notifyall ();
    }}//close pipeline output stream.
    When closed, Receivedlast () is called to notify the "pipeline inflow" that it has been closed.
        public void Close () throws IOException {if (sink!= null) {sink.receivedlast (); }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.