Java basics-introduction to some objects in Java io (1)

Source: Internet
Author: User

Java Io can be divided into two categories: byte stream object and byte stream object. Byte stream objects read data in bytes. The delimiter stream is read in characters. In general, the efficiency of the compaction stream is higher. But why is there a word throttling object?

This is because there are many forms of stream objects. If it is only text data, you can simply use a streaming object. However, most of the time we operate on data such as images, movies, and MP3, we can only use words to throttle objects. This is because the streaming object will be encoded and decoded using the encoding format specified by the current system during the operation. Therefore, you cannot operate on non-text data.

It is impossible to finish all Java I/O objects in a short time. It is not necessary. The common ones are familiar to you. There is no need to repeat it. I will introduce several stream objects that are unique and have specific application environments.

Pipedinputstream and pipedoutputstream

Pipedinputstream

The input stream of the pipeline should be connected to the output stream of the pipeline. The input stream of the pipeline provides all data bytes to be written to the output stream of the pipeline. Generally, data is transmitted by a thread fromPipedInputStreamRead the object and write it to the correspondingPipedOutputStream. We do not recommend that you try to use a single thread for these two objects, because the thread may be deadlocked. The input stream of the MPs queue contains a buffer. You can separate read and write operations within the buffer range. If the thread that provides data bytes to the output stream of the connection pipe no longer existsDamaged. -------- Java documentation

Pipedoutputstream

You can connect the MPs queue output stream to the MPs queue input stream to create a MPs queue. The output stream of the MPs queue is the sender of the MPs queue. Generally, data is written by a thread.PipedOutputStreamObject, and connected by other threads fromPipedInputStreamRead. We do not recommend that you try to use a single thread for these two objects, because this may cause a deadlock in the thread. If a thread is reading data bytes from the input stream of the connection pipeline but the thread is no longer in the active state, the pipeline is considered in the destroyed state. ----- Java
Document

Notice a sentence in the document:"We do not recommend that you try to use a single thread for these two objects, because this may cause a deadlock in the thread.That is to say, these two objects should be used in a multi-threaded environment. With multiple threads, we can use them for inter-thread communication. Pipedoutputstream is responsible for sending information, while pipedinputstream is responsible for receiving information. If the sender's information is not sent, the receiver will be in a blocking status and will wait.

Demo:

Pipedinputstream

/** *Apr 5, 2013 *Copyright(c)JackWang *All rights reserve *@Author <a href="mailto:wangchengjack@163.com">JackWang</a>*/package com.myjava.function;import java.io.IOException;import java.io.PipedInputStream;public class PipedIn implements Runnable{PipedInputStream pipIn = new PipedInputStream();/** * @param pipIn */public PipedIn(PipedInputStream pipIn) {this.pipIn = pipIn;}@Overridepublic void run() {byte[] buf = new byte[1024];try {int len = pipIn.read(buf);System.out.println(new String(buf,0,len));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {pipIn.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

Pipedoutputstream

/*** Apr 5, 2013 * copyright (c) jackwang * all rights reserve * @ author <a href = "mailto: wangchengjack@163.com "> jackwang </a> */package COM. myjava. function; import Java. io. ioexception; import Java. io. pipedoutputstream; public class pipedout implements runnable {pipedoutputstream pipout = new pipedoutputstream ();/*** @ Param pipout */Public pipedout (pipedoutputstream pipout) {This. pipout = pipout;} @ overridepu BLIC void run () {string STR = "hey, I'm here !!! "; Try {thread. sleep (5000); // simulate thread blocking pipout. write (Str. getbytes ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {try {pipout. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}

Test

/*** Apr 5, 2013 * copyright (c) jackwang * all rights reserve * @ author <a href = "mailto: wangchengjack@163.com "> jackwang </a> */package COM. myjava. function; import Java. io. ioexception; import Java. io. pipedinputstream; import Java. io. pipedoutputstream; public class pipediotest {/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {pipedinputstream in = new pipedinputstream (); // pipeline input stream pipedoutputstream out = new pipedoutputstream (); // pipeline output stream in. connect (out); // connect the pipeline pipedin pin = new pipedin (in); pipedout pout = new pipedout (out); New thread (PIN ). start (); New thread (pout ). start ();}}

In this example, thread. Sleep (5000) is used to simulate thread blocking.

------------------------------------------------------------------------------

Related Article

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.