How is the decoration mode used by the java io library reflected?

Source: Internet
Author: User
Tags uppercase letter

How is the decoration mode used by the java io library reflected?
Overview

There are about 85 classes in the I/O package of java, which is really complicated. Otherwise, these classes can be divided into the following four parts.

Input stream output stream
Byte stream InputStream OutputStream
Stream Reader Writer
In short, the corresponding four parts are neat, with FileInputStream and FileOutputStream, and BufferedReader and BufferedWriter.
For ease, let's take InputStream as an example to talk about the Design Pattern in the io library ------ decoration pattern
For more information about the decoration mode, see
Http://blog.csdn.net/dlf123321/article/details/9177335
This is my own reading < > Learning notes. Or you can go to HeadFirst directly.
Now let's look at the specific class diagram of io.

FileInputSteam and ByteArrayInputStream are various "coffee" and can be encapsulated. The former reads data from files and the latter reads data from byte arrays. FilterInputStream is the base class of the decoration class, And BufferedInputStream is a shell that can enclose the aforementioned "coffee", which contains an InputStream reference,
Now let's talk about FileInputSteam and BufferedInputStream.
FileInputSteam is used to upload an MTS queue to the file data source. When reading data, it is a byte read. If you think of the data source as a reservoir, using FileInputSteam is a drop of water from the bucket.
BufferedInputStream is used to mount a "Bucket" on the data source ". In the future, the small bucket will be operated directly (the bucket will first install water from the data source ). In this way, the efficiency is definitely high.
The specific implementation is complicated. Let's take a look at this
Http://icanfly.iteye.com/blog/1207397
The other decorations are similar.

The HeadFirst example is about how to use the decoration mode in javaio. I don't want to implement it too much. Now let's look at an example from HeadFirst. Converts an uppercase letter to a lowercase letter.
import java.io.*;public class LowerCaseInputStream extends FilterInputStream {    public LowerCaseInputStream(InputStream in) {        super(in);    }     public int read() throws IOException {        int c = super.read();        return (c == -1 ? c : Character.toLowerCase((char)c));    }            public int read(byte[] b, int offset, int len) throws IOException {        int result = super.read(b, offset, len);        for (int i = offset; i < offset+result; i++) {            b[i] = (byte)Character.toLowerCase((char)b[i]);        }        return result;    }}public class InputTest {    public static void main(String[] args) throws IOException {        int c;        try {            InputStream in =                new LowerCaseInputStream(                    new BufferedInputStream(                        new FileInputStream("test.txt")));            while((c = in.read()) >= 0) {                System.out.print((char)c);            }            in.close();        } catch (IOException e) {            e.printStackTrace();        }    }}


The content in the test.txt file is asdfwwwwwEEEEEE.
Finally, run InputTest. The output content is asdfwwwwweeeeee.




Reference http://blog.csdn.net/class281/article/details/24849275
Http://www.cnblogs.com/mengdd/archive/2013/02/12/2910302.html
Http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html

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.