Java-IO之FilterInputStream和FilterOuptStream

原文地址:http://blog.csdn.net/qq924862077/article/details/52728835
FilterInputStream的作用是用来封装其他的输入流,并为它们提供了额外的功能,它的常用的子类有BufferedInputStream和DataInputStream。FilterOutputStream的作用是用来封装其他的输出流,并为它们提供额外的功能,主要包括BufferedOutputStream,DataOutputStream和PrintStream。

基于JDK8的FilterInputStream的源码:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class FilterInputStream extends InputStream {  
  2.     /** 
  3.      * The input stream to be filtered. 
  4.      */  
  5.     //InputStream  
  6.     protected volatile InputStream in;  
  7.   
  8.     /** 
  9.      * Creates a <code>FilterInputStream</code> 
  10.      * by assigning the  argument <code>in</code> 
  11.      * to the field <code>this.in</code> so as 
  12.      * to remember it for later use. 
  13.      * 
  14.      * @param   in   the underlying input stream, or <code>null</code> if 
  15.      *          this instance is to be created without an underlying stream. 
  16.      */  
  17.   
  18.     protected FilterInputStream(InputStream in) {  
  19.         this.in = in;  
  20.     }  
  21.     //从输入流读取下一个字节  
  22.     public int read() throws IOException {  
  23.         return in.read();  
  24.     }  
  25.     //从输入流中读取len长度的字节  
  26.     public int read(byte b[]) throws IOException {  
  27.         return read(b, 0, b.length);  
  28.     }  
  29.     public int read(byte b[], int off, int len) throws IOException {  
  30.         return in.read(b, off, len);  
  31.     }  
  32.     //跳过长度  
  33.     public long skip(long n) throws IOException {  
  34.         return in.skip(n);  
  35.     }  
  36.     //是否还有数据  
  37.     public int available() throws IOException {  
  38.         return in.available();  
  39.     }  
  40.     //关闭资源  
  41.     public void close() throws IOException {  
  42.         in.close();  
  43.     }  
  44.     //标记在输入流的当前位置  
  45.     public synchronized void mark(int readlimit) {  
  46.         in.mark(readlimit);  
  47.     }  
  48.     //重置上次位置  
  49.     public synchronized void reset() throws IOException {  
  50.         in.reset();  
  51.     }  
  52.     //判断是否支持重置和标记  
  53.     public boolean markSupported() {  
  54.         return in.markSupported();  
  55.     }  
  56. }  

基于JDK8的FilterOutputStream源码:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class FilterOutputStream extends OutputStream {  
  2.     /** 
  3.      * The underlying output stream to be filtered. 
  4.      */  
  5.     protected OutputStream out;  
  6.   
  7.     public FilterOutputStream(OutputStream out) {  
  8.         this.out = out;  
  9.     }  
  10.     //写到输入流中  
  11.     public void write(int b) throws IOException {  
  12.         out.write(b);  
  13.     }  
  14.     //写b到输入流中  
  15.     public void write(byte b[]) throws IOException {  
  16.         write(b, 0, b.length);  
  17.     }  
  18.     //写b中的起始位置为off,长度为len  
  19.     public void write(byte b[], int off, int len) throws IOException {  
  20.         if ((off | len | (b.length - (len + off)) | (off + len)) < 0)  
  21.             throw new IndexOutOfBoundsException();  
  22.   
  23.         for (int i = 0 ; i < len ; i++) {  
  24.             write(b[off + i]);  
  25.         }  
  26.     }  
  27.   
  28.     /** 
  29.      * Flushes this output stream and forces any buffered output bytes 
  30.      * to be written out to the stream. 
  31.      * <p> 
  32.      * The <code>flush</code> method of <code>FilterOutputStream</code> 
  33.      * calls the <code>flush</code> method of its underlying output stream. 
  34.      * 
  35.      * @exception  IOException  if an I/O error occurs. 
  36.      * @see        java.io.FilterOutputStream#out 
  37.      */  
  38.     //刷新,强制所有的缓冲数据都被写出  
  39.     public void flush() throws IOException {  
  40.         out.flush();  
  41.     }  
  42.     //关闭资源  
  43.     @SuppressWarnings("try")  
  44.     public void close() throws IOException {  
  45.         try (OutputStream ostream = out) {  
  46.             flush();  
  47.         }  
  48.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值