Java-IO之BufferedInputStream(缓冲输入流)

BufferedInputStream是缓冲输入流,继承于FilterInputStream,作用是为另一个输入流添加一些功能,本质上是通过一个内部缓冲数组实现的。例如,在新建某输入流对应的BufferedInputStream后,当通过read()读取数据时,BufferedInputStream会将输入流的数据分批的填入到缓冲区中,每当缓冲区的数据读完之后,输入流会再次填充数据缓冲区,直到读完数据。

BufferedInputStream主要的函数列表:

BufferedInputStream(InputStream in)  
BufferedInputStream(InputStream in, int size)  
synchronized int available()  
void close()  
synchronized void mark(int readlimit)  
boolean markSupported()  
synchronized int read()  
synchronized int read(byte[] buffer, int offset, int byteCount)  
synchronized void reset()  
synchronized long skip(long byteCount)  

示例代码:

public class BufferedInputStreamTest {  
  
    private static final int LEN = 5;  
  
    public static void main(String[] args) {  
        testBufferedInputStream() ;  
    }  
    private static void testBufferedInputStream() {  
  
        // 创建BufferedInputStream字节流,内容是ArrayLetters数组  
        try {  
            File file = new File("file.txt");  
            InputStream in =new BufferedInputStream(new FileInputStream(file), 512);  
  
            // 从字节流中读取5个字节。“abcde”,a对应0x61,b对应0x62,依次类推...  
            for (int i=0; i<LEN; i++) {  
            // 若能继续读取下一个字节,则读取下一个字节  
                if (in.available() >= 0) {  
                // 读取“字节流的下一个字节”  
                int tmp = in.read();  
                System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));  
                }  
            }  
  
            // 若“该字节流”不支持标记功能,则直接退出  
            if (!in.markSupported()) {  
                System.out.println("make not supported!");  
                return ;  
            }  
  
            // 标记“当前索引位置”,即标记第6个位置的元素--“f”  
            // 1024对应marklimit  
            in.mark(1024);  
  
            // 跳过22个字节。  
            in.skip(22);  
  
            // 读取5个字节  
            byte[] buf = new byte[LEN];  
            in.read(buf, 0, LEN);  
            // 将buf转换为String字符串。  
            String str1 = new String(buf);  
            System.out.printf("str1=%s\n", str1);  
  
            // 重置“输入流的索引”为mark()所标记的位置,即重置到“f”处。  
            in.reset();  
            // 从“重置后的字节流”中读取5个字节到buf中。即读取“fghij”  
            in.read(buf, 0, LEN);  
            // 将buf转换为String字符串。  
            String str2 = new String(buf);  
            System.out.printf("str2=%s\n", str2);  
  
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (SecurityException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  


运行结果:
0 : 0x61
1 : 0x62
2 : 0x63
3 : 0x64
4 : 0x65
str1=12345
str2=fghij

基于JDK8的BufferInputStream代码:
public class BufferedInputStream extends FilterInputStream {  
  
    private static int DEFAULT_BUFFER_SIZE = 8192;//默认缓冲区大小为8X1024  
    private static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;  
    protected volatile byte buf[]; //缓冲数组  
  
    /** 
     * Atomic updater to provide compareAndSet for buf. This is 
     * necessary because closes can be asynchronous. We use nullness 
     * of buf[] as primary indicator that this stream is closed. (The 
     * "in" field is also nulled out on close.) 
     */  
    private static final AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater =  
  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值