Write the java byte stream input and output streams by yourself.

Source: Internet
Author: User

Write the java byte stream input and output streams by yourself.

A Data Stream is a collection of continuous data. It is like a water flow in a water pipe. Water is supplied at 1.1 points at one end of the water pipe, what we see at the other end of the pipe is a continuous flow of water.

"A stream is the source or endpoint of data stored on a disk or other peripheral devices ."

1) Data Stream:

 

A group of ordered data sequences of bytes with the starting and ending points. Including the input stream and output stream.

 

 

2) Input Stream ):

 

The program reads the data source from the input stream. Data sources include external sources (keyboards, files, networks, etc ...), That is, the communication channel that reads the data source into the program

 

 

3) output stream:

 

The program writes data to the output stream. Output Data in the program to the outside world (display, printer, file, network ...) .

 

 

The purpose of data flow is to make the output input independent from the device. 1. input stream code
Import java. io. IOException; import java. io. inputStream; // byte array input stream public class MyByteInputStream extends InputStream {private byte [] buf; // The array private int bufLength that stores the data stream; // record the length of the buf array private int pos; // The subscript position of the data already stored is private int readPos = 0; // record the position where the current data stream reads public MyByteInputStream (int I) {buf = new byte [32]; bufLength = 32; pos = 0;} // construct the input stream (directly store the stream data to be input) public MyByteInputStream (byte [] B) {if (B! = Null & B. length> 0) {int copyLength = B. length; buf = new byte [copyLength]; System. arraycopy (B, 0, buf, 0, copyLength); // copy the array content bufLength = copyLength; pos = copyLength;} else {buf = new byte [32]; bufLength = 32; pos = 0 ;}/ ** if data exists, the corresponding buf [readPos] is returned; otherwise,-1 */public int read () is returned () throws IOException {if (pos> 0 & readPos <= (pos-1) {readPos = readPos + 1; return buf [readPos-1];} return-1 ;}}

2. output stream code

Import java. io. outputStream; public class MyByteOutStream extends OutputStream {private byte [] buf; // output stream private int length; // stores the length of the output stream private int pos; // write location public MyByteOutStream () {buf = new byte [32]; length = 32; pos = 0;} public MyByteOutStream (int size) {if (size> 0) {buf = new byte [size]; length = size; pos = 0;} else {buf = new byte [32]; length = 32; pos = 0 ;}} /*** write character B to the byte stream. if the stream space is insufficient, expand ** @ param B */public void write (int B) {if (pos <length) {buf [pos] = (byte) B; pos = pos + 1;} else {// TODO: extended byte stream buf [] size}/*** will output stream copy ** @ return */public byte [] toByteArray () {if (pos> 0) {byte [] B = new byte [pos]; System. arraycopy (buf, 0, B, 0, pos); return B;} return null ;}}

 

3. Test class
Import java. io. IOException; public class MyTest {public static void main (String [] args) {String inputStr = "Test input stream! "; String outStr =" Test out strem! "; // Custom input stream MyByteInputStream myByteInputStream = new MyByteInputStream (inputStr. getBytes (); try {for (int I; (I = myByteInputStream. read ())! =-1;) {System. out. print (Character. toString (char) I);} catch (IOException e) {e. printStackTrace ();} System. out. println (""); // custom output stream MyByteOutStream myByteOutStream = new MyByteOutStream (100); byte [] B = outStr. getBytes (); for (int I = 0; I <B. length; I ++) {myByteOutStream. write (B [I]);} byte [] outb = myByteOutStream. toByteArray (); for (int I = 0; I <outb. length; I ++) {System. out. print (Character. toString (char) outb [I]);} System. out. println ("");}}

 

Ps: Thank you for your advice ~

 

 

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.