从Decorator,Adapter模式看Java/IO库(三)

2010年8月19日 由 yybean 留言 »

适配器模式的应用

适配器模式是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类型的对象形式的适配器模式,如下图所示:

www.360doc.com__63912_1604225_1  

查看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类型的对象形式的适配器模式,如下图所示:

www.360doc.com__63912_1604225_2

OutputStream原始流处理器中的适配器模式

同样地,在OutputStream类型中,所有的原始流处理器都是适配器类。

   ByteArrayOutputStream继承了OutputStream类型,同时持有一个对byte数组的引用。它一个byte数组的接口适配成OutputString类型的接口,因此也是一个对象形式的适配器模式的应用。

FileOutputStream是一个适配器类

FileOutputStream继承了OutputStream类型,同时持有一个对FileDiscriptor对象的引用。这是一个将FileDiscriptor接口适配成OutputStream接口形式的对象形适配器模式。

Reader原始流处理器中的适配器模式

Reader类型的原始流处理器都是适配器模式的应用。

StringReader是一个适配器类

StringReader类继承了Reader类型,持有一个对String对象的引用。它将String的接口适 配成Reader类型的接口,如下图所示:

www.360doc.com__63912_1604225_3

byte流到char流的适配

在Java I/O库中,使用比较频繁的要数InputStreamReader,OutputStreamWriter这两种类了,

InputStreamReader是从byte输入流到char输入流的一个适配器。下图所示就是 InputStreamReader与Reader和InputStream等类的结构图:

www.360doc.com__63912_1604225_4_000

当把InputStreamReader与任何InputStream的具体子类链接的时候,可以从InputStream的输出读入byte类型的数据,将之转换成为char类型的数据,如下图所示:

www.360doc.com__63912_1604225_5

查看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在线源码

http://www.360doc.com/content/08/0902/22/63912_1604225.shtml





发表评论