The adapter pattern of Java design mode and its role in Java

Source: Internet
Author: User

The adapter function is to say one interface to another interface, in the Java I/O class library There are many such requirements, such as the conversion of string data into byte data into a file, the byte data into stream data and so on.

Take the InputStreamReader and OutputStreamWriter classes as an example.

The InputStreamReader and OutputStreamWriter classes inherit the reader and writer interfaces respectively, but the object to create them must pass in a constructor with a inputstream and OutputStream

example, the InputStreamReader and OutputStreamWriter classes function to match inputstream and OutputStream to reader and writer.

The adapter is InputStreamReader, the source role is the instance object represented by InputStream, and the target interface is the reader class.

InputStream: Gets the byte input stream, Inputstream.read ("filename"),

Reader: Reading a stream of characters

InputStreamReader: A bridge from bytes to characters. InputStreamReader (Inputstream.read ("filename"));

Reader.read (InputStreamReader (InputStream in)), which can be changed from byte to character, printed and displayed.

Java.io.Reader and Java.io.InputStream make up the Java input class.

Reader is used to read 16-bit characters, which are Unicode-encoded characters, while InputStream is used to read ASCII characters and binary data.

Reader supports 16-bit Unicode character output,

The InputStream supports 8-bit character output.

Reader and InputStream are two sets of parallel independent hierarchies provided by the I/O library,

InputStreamReader class

InputStreamReader converts a byte stream to a character stream. is a bridge of bytes flowing to a character stream. If you do not specify a character set encoding, the decoding process uses the default character encoding for the platform, such as: GBK.
Construction Method :
InputStreamReader ISR = new InputStreamReader (InputStream in);//construct a InputStreamReader class with a default encoding set
InputStreamReader ISR = new InputStreamReader (InputStream in,string charsetname);//Constructs a InputStreamReader class that specifies the encoding set.

The parameter in object is obtained by inputstream in = system.in; Reads the data on the keyboard.
or inputstream in = new FileInputStream (String fileName);//reads the data from the file. You can see that FileInputStream is a subclass of InputStream.
main methods : int read ();//Read single character.
int read (char []cbuf);//designators The read word into the array. Returns the number of characters read.

Refer to the original link http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html

The Java design mode adapter pattern and the role in Java

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.