Java基础之IO流,自定义字节流缓冲区装饰类(模仿)

import java.io.*;

/*
    自定义缓冲区
*/
class MyBufferedInputStreamDemo
{
     public  static  void main(String[] args)   throws IOException
    {
        MyBufferedInputStream mis =  new MyBufferedInputStream( new FileInputStream( new File("001.avi")));
        BufferedOutputStream bos =  new BufferedOutputStream( new FileOutputStream( new File("001-1.avi")));
        
         int ch = 0;
         while((ch=mis.read())!=-1)
        {
            bos.write(ch);
        }
        
        mis.close();
        bos.close();
    }
}

class MyBufferedInputStream
{
     private InputStream inputStream =  null;
     private  byte[] buffer =  new  byte[1024 * 4];
     private  int pos = 0,count=0;
    
     public MyBufferedInputStream(InputStream inputStream)  throws IOException
    {
         this.inputStream = inputStream;
    }
    
     public  int read()  throws IOException
    {
         // 如果结果长度为零,则从流中读取数据
         if(count==0)
        {
             // 读取数据被放入字节数组中
            count =  this.inputStream.read(buffer);    
            
             // 如果读取的结果长度为-1,说明文件读取完成
             if(count<0)
                 return -1;
            
             // 每次重新从流中取数据时,将指针重置为0
            pos = 0;
            
             byte b = buffer[pos];            
            count--;
            pos++;
             return b&0xff;
        }
         else
        {
             byte b = buffer[pos];            
            count--;
            pos++;
             return b&0xff;
        }
    }
    
     public  void close()  throws IOException
    {
         this.inputStream.close();
    }
}

转载于:https://www.cnblogs.com/cxmsky/archive/2013/01/31/2886940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值