設計模式 - 裝飾者模式(Decorator Pattern) Java的IO類 使用方法

裝飾者模式(Decorator Pattern) Java的IO類 使用方法


本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716823


裝飾者模式(decorator pattern)參見: http://blog.csdn.net/caroline_wendy/article/details/26707033


Java的IO類使用裝飾者模式進行擴展, 其中FilterInputStream類, 就是裝飾者(decorator)的基類.

實現其他裝飾者(decorator), 需要繼承FilterInputStream類.


代碼:

  1. /** 
  2.  * @time 2014年5月23日 
  3.  */  
  4. package decorator.io;  
  5.   
  6. import java.io.FilterInputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9.   
  10. /** 
  11.  * @author C.L.Wang 
  12.  * 
  13.  */  
  14. public class LowerCaseInputStream extends FilterInputStream {  
  15.   
  16.     public LowerCaseInputStream(InputStream in) {  
  17.         super(in);  
  18.     }  
  19.       
  20.     public int read() throws IOException {  
  21.         int c = super.read();  
  22.         return (c==-1 ? c : Character.toLowerCase((char)c));  
  23.     }  
  24.       
  25.     public int read(byte[] b, int offset, int len) throws IOException {  
  26.         int result = super.read(b, offset, len);  
  27.         for (int i=offset; i<offset+result; ++i) {  
  28.             b[i] = (byte)Character.toLowerCase((char)b[i]);  
  29.         }  
  30.         return result;  
  31.     }  
  32. }  
/**
 * @time 2014年5月23日
 */
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 result = super.read(b, offset, len);
		for (int i=offset; i<offset+result; ++i) {
			b[i] = (byte)Character.toLowerCase((char)b[i]);
		}
		return result;
	}
}

測試:

  1. /** 
  2.  * @time 2014年5月23日 
  3.  */  
  4. package decorator.io;  
  5.   
  6. import java.io.BufferedInputStream;  
  7. import java.io.FileInputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10.   
  11. /** 
  12.  * @author C.L.Wang 
  13.  * 
  14.  */  
  15. public class InputTest {  
  16.   
  17.     /** 
  18.      * @param args 
  19.      */  
  20.     public static void main(String[] args) throws IOException{  
  21.         // TODO Auto-generated method stub  
  22.         int c;  
  23.         try{  
  24.             InputStream in = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream("test.txt")));  
  25.             while ((c = in.read()) >= 0) {  
  26.                 System.out.print((char)c);  
  27.             }  
  28.             in.close();  
  29.         } catch (IOException e){  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章