Adorner mode (decorator pattern) Java IO class using method

Source: Internet
Author: User
Tags root directory

The Java IO class expands using the adorner pattern, where the FilterInputStream class is the base class for the adorner (decorator).

To implement other adorners (decorator), you need to inherit the FilterInputStream class.

Code:

/** 
 * @time May 23, 2014 * * *
package decorator.io;  
      
Import Java.io.FilterInputStream;  
Import java.io.IOException;  
Import Java.io.InputStream;  
      
/** 
 * @author C.l.wang * */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 = Super.read (b, offset, len);  
        for (int i=offset; i<offset+result; ++i) {  
            b[i] = (byte) character.tolowercase ((char) b[i]);  
        return result;  
    }  

Test:

/** 
 * @time May 23, 2014 * * *
package decorator.io;  
      
Import Java.io.BufferedInputStream;  
Import Java.io.FileInputStream;  
Import java.io.IOException;  
Import Java.io.InputStream;  
      
/** 
 * @author C.l.wang * */Public
class Inputtest {  
      
    /** 
     * @param 
     args
    * * public static void Main (string[] args) throws ioexception{  
        //TODO auto-generated method stub  
        int C;  
        try{  
            InputStream in = new Lowercaseinputstream (New Bufferedinputstream ("FileInputStream"));  
            while ((c = in.read ()) >= 0) {  
                System.out.print ((char) c);  
            In.close ();  
        } catch (IOException e) {  
            e.printstacktrace ();}}}  
      

By decorating the concrete component class FileInputStream, realize the format change.

Note: The default read path for Java files is the root directory of the project.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

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.