Design pattern-Decorator mode (Decorator pattern) Java IO class usage

Source: Internet
Author: User

Decorator Mode (Decorator pattern) Java IO class usage


This address: http://blog.csdn.net/caroline_wendy/article/details/26716823


Decorator Mode (decorator pattern) See: http://blog.csdn.net/caroline_wendy/article/details/26707033


the Java IO class is extended using decorator mode , where the FilterInputStream class is the base class for decorators (decorator) .

To implement other decorators (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 LowerC Aseinputstream (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=offs Et 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 stubint 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 ();}}}

format changes are implemented by decorating the detailed component Class FileInputStream.

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






Design pattern-Decorator mode (Decorator pattern) Java IO class usage

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.