您的位置:首页 > 编程语言 > Java开发

Java字节流:FilterInputStream FilterOutputStream

2016-11-13 10:12 363 查看
-----------------------------------------------------------------------------------
  FilterInputStream、FilterOutputStream 过滤器字节输入流、输出流,这里用到了装饰器模式,它的主要用途在于给一个对象动态的添加功能。
  当我们在创建FilterInputStream、FilterOutputStream这两个类的实例时需要传入一个InputStream、OutPutStream的子类,比如:当构造FilterOutputStream时传递进去的是FileOutputStream,而FileOutputStream和FilterOutputStream实现的是同一个抽象类OutputStream,那么FilterOutputStrean对FileOutputStream的装饰对于客户端来说就是透明的,可以在FileOutputStream的方法执行之前或之后加上一些额外的操作来达到装饰的效果。
  FilterInputStream、FilterOutputStream 仅仅是对InputStream、OutputStream中所有方法进行了重写,并且只是调用传入的InputStream、OutputStream子类的方法,话句话说就是没有对传入的低级字节输入流进行任何的装饰,它们的作用是为所有字节输入流的装饰类提供一个标准、一个类似于接口的作用,具体的装饰功能由FilterInputStream、FilterOutputStream的子类来完成。
-----------------------------------------------------------------------------------
FilterInputStream
类声明:public class FilterInputStream extends InputStream
位于java.io包下
官方对其说明:
  A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality. The class FilterInputStream itself simply overrides all methods of InputStream with versions that pass all requests to the contained input stream. Subclasses of FilterInputStream may further override some of these methods and may also provide additional methods and fields.
  (简单翻译:FilterInputStream包含其他一些输入流,它将这些流用作其基本数据源,它可以直接传输数据或提供一些额外的功能。FilterInputStream类本身只是简单地重写那些将所有请求传递给所包含输入流的InputStream的所有方法。FilterInputStream的子类可进一步重写这些方法中的一些方法,并且还可以提供一些额外的方法和字段。)

主要字段:
  protected InputStream in; //要过滤的输入流

构造方法:
  protected FilterInputStream(InputStream in)

主要方法:
  - int available(): 返回输入流中还可以读取的字节个数.
  - void close(): 关闭此输入流并释放与该流有关的系统资源.
  - void mark(int readlimit): 在此输入流中标记当前的位置.
  - boolean markSupported(): 检测此输入流是否支持mark和reset.
  - int read(): 从输入流中读取数据的下一个字节.
  - int read(byte[] b): 从输入流中读取一定数量的字节,并将其存储在字节数组b中
  - int read(byte[] b,int off,int len): 从输入流中读取len个字节,并将其存储在字节数组b中off位置开始的地方
  - void reset(): 将此流重新定位到最后一次对此输入流调用mark方法时的位置.
  - long skip(long n): 跳过和丢弃此输入流中n个字节的数据.

源代码如下:

1 package java.io;
2
3 /**
4  * This class is the superclass of all classes that filter output
5  * streams. These streams sit on top of an already existing output
6  * stream (the <i>underlying</i> output stream) which it uses as its
7  * basic sink of data, but possibly transforming the data along the
8  * way or providing additional functionality.
9  * <p>
10  * The class <code>FilterOutputStream</code> itself simply overrides
11  * all methods of <code>OutputStream</code> with versions that pass
12  * all requests to the underlying output stream. Subclasses of
13  * <code>FilterOutputStream</code> may further override some of these
14  * methods as well as provide additional methods and fields.
15  *
16  * @author  Jonathan Payne
17  * @since   JDK1.0
18  */
19 public
20 class FilterOutputStream extends OutputStream {
21     /**
22      * The underlying output stream to be filtered.
23      */
24     protected OutputStream out;
25
26     /**
27      * Creates an output stream filter built on top of the specified
28      * underlying output stream.
29      *
30      * @param   out   the underlying output stream to be assigned to
31      *                the field <tt>this.out</tt> for later use, or
32      *                <code>null</code> if this instance is to be
33      *                created without an underlying stream.
34      */
35     public FilterOutputStream(OutputStream out) {
36         this.out = out;
37     }
38
39     /**
40      * Writes the specified <code>byte</code> to this output stream.
41      * <p>
42      * The <code>write</code> method of <code>FilterOutputStream</code>
43      * calls the <code>write</code> method of its underlying output stream,
44      * that is, it performs <tt>out.write(b)</tt>.
45      * <p>
46      * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
47      *
48      * @param      b   the <code>byte</code>.
49      * @exception  IOException  if an I/O error occurs.
50      */
51     public void write(int b) throws IOException {
52         out.write(b);
53     }
54
55     /**
56      * Writes <code>b.length</code> bytes to this output stream.
57      * <p>
58      * The <code>write</code> method of <code>FilterOutputStream</code>
59      * calls its <code>write</code> method of three arguments with the
60      * arguments <code>b</code>, <code>0</code>, and
61      * <code>b.length</code>.
62      * <p>
63      * Note that this method does not call the one-argument
64      * <code>write</code> method of its underlying stream with the single
65      * argument <code>b</code>.
66      *
67      * @param      b   the data to be written.
68      * @exception  IOException  if an I/O error occurs.
69      * @see        java.io.FilterOutputStream#write(byte[], int, int)
70      */
71     public void write(byte b[]) throws IOException {
72         write(b, 0, b.length);
73     }
74
75     /**
76      * Writes <code>len</code> bytes from the specified
77      * <code>byte</code> array starting at offset <code>off</code> to
78      * this output stream.
79      * <p>
80      * The <code>write</code> method of <code>FilterOutputStream</code>
81      * calls the <code>write</code> method of one argument on each
82      * <code>byte</code> to output.
83      * <p>
84      * Note that this method does not call the <code>write</code> method
85      * of its underlying input stream with the same arguments. Subclasses
86      * of <code>FilterOutputStream</code> should provide a more efficient
87      * implementation of this method.
88      *
89      * @param      b     the data.
90      * @param      off   the start offset in the data.
91      * @param      len   the number of bytes to write.
92      * @exception  IOException  if an I/O error occurs.
93      * @see        java.io.FilterOutputStream#write(int)
94      */
95     public void write(byte b[], int off, int len) throws IOException {
96         if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
97             throw new IndexOutOfBoundsException();
98
99         for (int i = 0 ; i < len ; i++) {
100             write(b[off + i]);
101         }
102     }
103
104     /**
105      * Flushes this output stream and forces any buffered output bytes
106      * to be written out to the stream.
107      * <p>
108      * The <code>flush</code> method of <code>FilterOutputStream</code>
109      * calls the <code>flush</code> method of its underlying output stream.
110      *
111      * @exception  IOException  if an I/O error occurs.
112      * @see        java.io.FilterOutputStream#out
113      */
114     public void flush() throws IOException {
115         out.flush();
116     }
117
118     /**
119      * Closes this output stream and releases any system resources
120      * associated with the stream.
121      * <p>
122      * The <code>close</code> method of <code>FilterOutputStream</code>
123      * calls its <code>flush</code> method, and then calls the
124      * <code>close</code> method of its underlying output stream.
125      *
126      * @exception  IOException  if an I/O error occurs.
127      * @see        java.io.FilterOutputStream#flush()
128      * @see        java.io.FilterOutputStream#out
129      */
130     public void close() throws IOException {
131         try {
132           flush();
133         } catch (IOException ignored) {
134         }
135         out.close();
136     }
137 }


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐