View Java/IO library from decorator and adapter mode (3)

Source: Internet
Author: User

Application in four-adapter Mode  The adapter mode is the second most important design mode in the Java I/O library.   Inputstream Adapter mode in original stream processorThe inputstream Type Raw stream processor is an application in the adapter mode. Bytearrayinputstream is an adapter class bytearrayinputstream that inherits the inputstream interface and encapsulates a byte array. In other words, it adapts a byte array interface to an inputstream stream processor interface. We know that JAVA supports four types: Java interfaces, Java classes, Java arrays, and primitive types (INT, float, etc ). The first three types are reference types. The instances of classes and arrays are objects, and the values of the original type are not objects. That is, the Java array is the same as all other objects, regardless of the element type stored in the array. In this way, bytearrayinputstream conforms to the adapter mode description and is an object-form adapter class. Fileinputstream Is an adapter class  Fileinputstream inherits the inputstrem type and holds a reference to filediscriptor. This is an adapter mode that adapts a filediscriptor object to an inputstrem object, as shown in:



View the source code of jdk1.4. We can see that:
 Public class{ FileInputStream extends InputStream
 /* File Descriptor - handle to the open file */
   private FileDescriptor fd;
   public FileInputStream(FileDescriptor fdObj) {
     SecurityManager security = System.getSecurityManager();
     if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkRead(fdObj);
}
fd = fdObj;
 }
 public FileInputStream(File file) throws FileNotFoundException {
String name = file.getPath();
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
fd = new FileDescriptor();
open(name);
}
// Other code
Stringbufferinputstring inherits the inputstring type and holds a reference to the string object. This is an adapter mode that adapts the string object to the inputstring type, as shown in: Outputstream Adapter mode in original stream processor  Similarly, in the outputstream type, all the original stream processors are adapter classes. Bytearrayoutputstream inherits the outputstream type and holds a reference to the byte array. Its byte array interface is adapted to the outputstring type interface, so it is also an object-form adapter mode application. Fileoutputstream Is an adapter classFileoutputstream inherits the outputstream type and holds a reference to the filediscriptor object. This is an object-form adapter mode that adapts the filediscriptor interface to the outputstream interface form. Reader Adapter mode in original stream processor  Reader-Type Raw stream processors are used in adapter mode. Stringreader Is an adapter class  The stringreader class inherits the reader type and holds a reference to the string object. It matches the string interface with the reader interface, as shown in: Slave Byte Flow Char Stream adaptationIn the Java I/O library, inputstreamreader and outputstreamwriter are frequently used. inputstreamreader is an adapter that flows from byte input to Char input streams. Shows the structure of inputstreamreader, reader, inputstream, and other classes. When inputstreamreader is linked to a specific subclass of any inputstream, data of the byte type can be read from the output of inputstream, convert data of the char type, as shown in: view the inputstreamreader source code of jdk1.4:
public class InputStreamReader extends Reader {
 private final StreamDecoder sd;
 /**
 * Create an InputStreamReader that uses the default charset.
  *
 * @param in   An InputStream
 */
public InputStreamReader(InputStream in) {
super(in);
 try {
 sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
 } catch (UnsupportedEncodingException e) {
 // The default encoding should always be available
throw new Error(e);
}
// Other code
}
Streamdecoder is a class in the sun. NiO. CS package.
Outputstreamwriter is an adapter class.
Similarly, we can conclude that outputstringwriter is an adapter class from outputstream to writer. That is to say, when linked to any specific outputstream subclass, outputstringwriter can adapt the byte stream of the outputstream type to a char stream.
The source code of jdk1.4 is similar to that of inputstreamreader. If you are interested, check the online source code of jdk1.4.
This book is followed by a small example with some explanations. I will not list it. You can check it if you have any books.
Summary
In these three articles, there are three knowledge points:
  Knowledge Point 1: Four hierarchical structures of the Java I/O LibraryThe Java I/O Library provides four levels: inputstream, outputstream, reader, and writer. Inputstream and outputstream PROCESS 8-bit stream data. Reader and writer process 16-bit bytes stream data. Inputstream and reader process input, and outputstream and writer process output.   Knowledge Point 2: application of the decorator mode in the Java I/O Library   Knowledge 3: Application of adapter mode in Java I/O Library
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.