四適配器模式的應用
適配器模式是Java I/O庫中第二個最爲重要的設計模式。
InputStream原始流處理器中的適配器模式
InputStream類型的原始流處理器是適配器模式的應用。
ByteArrayInputStream是一個適配器類
ByteArrayInputStream繼承了InputStream的接口,而封裝了一個byte數組。換言之,它將一個byte數組的接口適配成InputStream流處理器的接口。
我們知道Java語言支持四種類型:Java接口,Java類,Java數組,原始類型(即int,float等)。前三種是引用類型,類和數組的實例是對象,原始類型的值不是對象。
也即,Java語言的數組是像所有的其他對象一樣的對象,而不管數組中所存儲的元素類型是什麽。
這樣一來的話,ByteArrayInputStream就符合適配器模式的描述,是一個對象形式的適配器類。
FileInputStream是一個適配器類
在FileInputStream繼承了InputStrem類型,同時持有一個對FileDiscriptor的引用。這是將一個FileDiscriptor對象適配成InputStrem類型的對象形式的適配器模式,如下圖所示:
查看JDK1.4的源代碼我們可以看到:
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);
}
//其它代碼
}
StringBufferInputString繼承了InputString類型,同時持有一個對String對象的引用,這是一個將String對象適配成InputString類型的對象形式的適配器模式,如下圖所示:
OutputStream原始流處理器中的適配器模式
同樣地,在OutputStream類型中,所有的原始流處理器都是適配器類。
ByteArrayOutputStream繼承了OutputStream類型,同時持有一個對byte數組的引用。它一個byte數組的接口適配成OutputString類型的接口,因此也是一個對象形式的適配器模式的應用。
FileOutputStream是一個適配器類
FileOutputStream繼承了OutputStream類型,同時持有一個對FileDiscriptor對象的引用。這是一個將FileDiscriptor接口適配成OutputStream接口形式的對象形適配器模式。
Reader原始流處理器中的適配器模式
Reader類型的原始流處理器都是適配器模式的應用。
StringReader是一個適配器類
StringReader類繼承了Reader類型,持有一個對String對象的引用。它將String的接口適 配成Reader類型的接口,如下圖所示:
從byte流到char流的適配
在Java I/O庫中,使用比較頻繁的要數InputStreamReader,OutputStreamWriter這兩種類了,
InputStreamReader是從byte輸入流到char輸入流的一個適配器。下圖所示就是 InputStreamReader與Reader和InputStream等類的結構圖:
當把InputStreamReader與任何InputStream的具體子類鏈接的時候,可以從InputStream的輸出讀入byte類型的數據,將之轉換成爲char類型的數據,如下圖所示:
查看JDK1.4的InputStreamReader源代碼:
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);
}
//其它代碼
}
其中StreamDecoder是sun.nio.cs這個包裏的一個類
OutputStreamWriter是適配器類
同樣道理我們能得出OutputStringWriter是從OutputStream到Writer的適配器類。也就是說,與任何一個OutputStream的具體子類相鏈接時,OutputStringWriter可以將OutputStream類型的byte流適配成爲char流。
它的源代碼跟上面的InputStreamReader差不多,這就不貼出來,感興趣可以查看JDK1.4在線源碼
這本書後面還有個小例子,附有一些講解,我就不列出來了,有書的可以看看。
五 總結
在這三篇文章裏主要是汲及到三個知識點:
知識點一: Java I/O庫的四大等級結構
Java語言的I/O庫提供了四大等級結構:InputStream,OutputStream,Reader,Writer四個系列的類。InputStream和OutputStream處理8位字節流數據, Reader和Writer處理16位的字符流數據。InputStream和Reader處理輸入, OutputStream和Writer處理輸出。
知識點二: Decorator模式在Java I/O庫的應用
知識點三: Adapter模式在Java I/O庫的應用
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
四 適配器模式的應用
適配器模式是Java I/O庫中第二個最爲重要的設計模式。
InputStream原始流處理器中的適配器模式
InputStream類型的原始流處理器是適配器模式的應用。
ByteArrayInputStream是一個適配器類
ByteArrayInputStream繼承了InputStream的接口,而封裝了一個byte數組。換言之,它將一個byte數組的接口適配成InputStream流處理器的接口。
我們知道Java語言支持四種類型:Java接口,Java類,Java數組,原始類型(即int,float等)。前三種是引用類型,類和數組的實例是對象,原始類型的值不是對象。
也即,Java語言的數組是像所有的其他對象一樣的對象,而不管數組中所存儲的元素類型是什麽。
這樣一來的話,ByteArrayInputStream就符合適配器模式的描述,是一個對象形式的適配器類。
FileInputStream是一個適配器類
在FileInputStream繼承了InputStrem類型,同時持有一個對FileDiscriptor的引用。這是將一個FileDiscriptor對象適配成InputStrem類型的對象形式的適配器模式,如下圖所示:
[img]http://images.wangchao.net.cn/images/upload/images/lsdn/1190408552099.jpg[/img]
查看JDK1.4的源代碼我們可以看到:
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);
}
//其它代碼
}
StringBufferInputString繼承了InputString類型,同時持有一個對String對象的引用,這是一個將String對象適配成InputString類型的對象形式的適配器模式,如下圖所示:
[img]http://images.wangchao.net.cn/images/upload/images/lsdn/1190408552333.jpg[/img]
OutputStream原始流處理器中的適配器模式
同樣地,在OutputStream類型中,所有的原始流處理器都是適配器類。
ByteArrayOutputStream繼承了OutputStream類型,同時持有一個對byte數組的引用。它一個byte數組的接口適配成OutputString類型的接口,因此也是一個對象形式的適配器模式的應用。
FileOutputStream是一個適配器類
FileOutputStream繼承了OutputStream類型,同時持有一個對FileDiscriptor對象的引用。這是一個將FileDiscriptor接口適配成OutputStream接口形式的對象形適配器模式。
Reader原始流處理器中的適配器模式
Reader類型的原始流處理器都是適配器模式的應用。
StringReader是一個適配器類
StringReader類繼承了Reader類型,持有一個對String對象的引用。它將String的接口適 配成Reader類型的接口,如下圖所示:
[img]http://images.wangchao.net.cn/images/upload/images/lsdn/1190408552552.jpg[/img]
從byte流到char流的適配
在Java I/O庫中,使用比較頻繁的要數InputStreamReader,OutputStreamWriter這兩種類了,
InputStreamReader是從byte輸入流到char輸入流的一個適配器。下圖所示就是 InputStreamReader與Reader和InputStream等類的結構圖:
[img]http://images.wangchao.net.cn/images/upload/images/lsdn/1190408552786.jpg[/img]
當把InputStreamReader與任何InputStream的具體子類鏈接的時候,可以從InputStream的輸出讀入byte類型的數據,將之轉換成爲char類型的數據,如下圖所示:
[img]http://images.wangchao.net.cn/images/upload/images/lsdn/1190408552942.jpg[/img]
查看JDK1.4的InputStreamReader源代碼:
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);
}
//其它代碼
}
其中StreamDecoder是sun.nio.cs這個包裏的一個類
OutputStreamWriter是適配器類
同樣道理我們能得出OutputStringWriter是從OutputStream到Writer的適配器類。也就是說,與任何一個OutputStream的具體子類相鏈接時,OutputStringWriter可以將OutputStream類型的byte流適配成爲char流。
它的源代碼跟上面的InputStreamReader差不多,這就不貼出來,感興趣可以查看[url=http://javaresearch.gro.clinux.org/jdk140/index.html]JDK1.4在線源碼[/url]
這本書後面還有個小例子,附有一些講解,我就不列出來了,有書的可以看看。
五 總結
在這三篇文章裏主要是汲及到三個知識點:
知識點一: Java I/O庫的四大等級結構
Java語言的I/O庫提供了四大等級結構:InputStream,OutputStream,Reader,Writer四個系列的類。InputStream和OutputStream處理8位字節流數據, Reader和Writer處理16位的字符流數據。InputStream和Reader處理輸入, OutputStream和Writer處理輸出。
知識點二: Decorator模式在Java I/O庫的應用
知識點三: Adapter模式在Java I/O庫的應用