BufferedInputStream:

public synchronized int read() throws IOException

int res=bis.read();

System.out.println((char)res)

  调用一次,取一次值,并游标向前走一位;返回值为unicode

 public synchronized int read(byte b[], int off, int len) throws IOException

 byte[] buff=new byte[LEN];

 int res=bis.read(buff,0,LEN);

 System.out.println(new String(buff,0,res));

    调用一次 取LEN个值,并游标向前走LEN位,数据保存在字节数组中,返回值是数据个数

public synchronized int available() throws IOException

  返回目标文件大小, 底层实现​

 public synchronized void mark(int readlimit)
public synchronized void reset() throws IOException

  mark用来标记断点(下标)并传入一个readlimit (缓冲流最大限制)

reset 将返回到断点重新读取数据

根据源码解读:


  public synchronized void mark(int readlimit) {
marklimit = readlimit;
markpos = pos;
} public synchronized void reset() throws IOException {
getBufIfOpen(); // Cause exception if closed
if (markpos < 0)
throw new IOException("Resetting to invalid mark");
pos = markpos;
}

  

 private void fill() throws IOException {
byte[] buffer = getBufIfOpen();
if (markpos < 0)
pos = 0; /* no mark: throw away the buffer */
else if (pos >= buffer.length) /* no room left in buffer */
if (markpos > 0) { /* can throw away early part of the buffer */
int sz = pos - markpos;
System.arraycopy(buffer, markpos, buffer, 0, sz);
pos = sz;
markpos = 0;
} else if (buffer.length >= marklimit) {
markpos = -1; /* buffer got too big, invalidate mark */
pos = 0; /* drop buffer contents */
} else if (buffer.length >= MAX_BUFFER_SIZE) {
throw new OutOfMemoryError("Required array size too large");
} else { /* grow buffer */
int nsz = (pos <= MAX_BUFFER_SIZE - pos) ?
pos * 2 : MAX_BUFFER_SIZE;
if (nsz > marklimit)
nsz = marklimit;
byte nbuf[] = new byte[nsz];
System.arraycopy(buffer, 0, nbuf, 0, pos);
if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
// Can't replace buf if there was an async close.
// Note: This would need to be changed if fill()
// is ever made accessible to multiple threads.
// But for now, the only way CAS can fail is via close.
// assert buf == null;
throw new IOException("Stream closed");
}
buffer = nbuf;
}
count = pos;
int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
if (n > 0)
count = n + pos;
}

  

1、调用mark 并传入int类型参数赋值给marklimit,同时将pos赋值给markpos;

2、在读取数据过程中,在没调用reset前,pos会持续自增;

3、 在缓冲区中数据读取完后,会再次调用fill方法,使缓冲区扩大一倍,然后pos继续自增

接下来有三种运行路线

1、缓冲区大小超过marklimit时,调用reset时,会抛异常,因为markpos=-1;

2、缓冲区大小超过最大缓冲区大小,直接抛异常

3、缓冲区大小没有超过marklimit,调用reset,缓冲区大小回到初始值大小,将markpos复制给pos,返回到标记点,重新读取数据;

还有查看源码发现,如果缓冲区大小没有超过marklimit,,那么这个标记一直存在,知道缓冲close;没有找到去掉标记的方法;

BufferedOutputStream:

 public synchronized void write(int b) throws IOException 

 public synchronized void write(byte b[], int off, int len) throws IOException  

 public synchronized void flush() throws IOException

 private void flushBuffer() throws IOException

  

BufferedInputStream/BufferedOutputStream的更多相关文章

  1. 字节缓冲流 ( BufferedInputStream / BufferedOutputStream)

    package com.sxt.reader; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; imp ...

  2. Java字节流:BufferedInputStream BufferedOutputStream

    -----------------------------------------------------------------------------------BufferedInputStre ...

  3. BufferedInputStream/BufferedOutputStream复制文件

    public class Test{ public static void main(String[] args) throws IOException{ FileInputStream in = n ...

  4. Java API —— IO流( FileInputStream & FileOutputStream & BufferedInputStream & BufferedOutputStream )

    1.IO流概述 · IO流用来处理设备之间的数据传输        · 上传文件和下载文件        · Java对数据的操作是通过流的方式 · Java用于操作流的对象都在IO包中   2.IO ...

  5. [十二]JavaIO之BufferedInputStream BufferedOutputStream

    功能简介 BufferedInputStream 和 BufferedOutputStream一样,他们都是过滤流 装饰器模式下具体的装饰类 用来装饰InputStream以及OutputStream ...

  6. BufferedInputStream&BufferedOutputStream

    使用字符缓冲区相关实现copy文件: public static void main(String[] args) { //创建文件对象指定要拷贝的文件路径(源文件),文件须存在,测试用例不做判断 F ...

  7. 系统学习 Java IO (九)----缓冲流 BufferedInputStream/BufferedOutputStream

    目录:系统学习 Java IO---- 目录,概览 BufferedInputStream BufferedInputStream 类为输入流提供缓冲. 缓冲可以加快IO的速度. BufferedIn ...

  8. 使用文件流与使用缓冲流完成文件的复制操作性能对比,文件流 FileInputStream FileOutputStream 缓冲流: BufferedInputStream BufferedOutputStream

    package seday06; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOExc ...

  9. FileInputStream 与 BufferedInputStream 效率对比

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3550158.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...

随机推荐

  1. 轻量ORM-SqlRepoEx (五) 存储过程操作

    .Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵活的Lambda表达式组合,实现 ...

  2. redis sentinel搭建以及在jedis中使用

    一.redis主从搭建 1.搭建redis master 1>redis安装 mkdir -p /usr/local/webserver/redis //安装目录 cd /usr/local/w ...

  3. idea开启自动编译

    springboot+thymeleaf+idea   idea默认是不自动编译的. 首先热部署的jar包肯定是要加进去的 2,快捷键 ctrl+shift+alt+/       点击registr ...

  4. SQL里的concat() 以及group_concat() 函数的使用

    实例参考:https://blog.csdn.net/mary19920410/article/details/76545053 一 concat()函数 1.功能:将多个字符串连接成一个字符串. 2 ...

  5. ES6 语法学习总结

    第一节:什么是ES6?   ES6是什么?跟JavaScript有什么关系? JavaScrip由三部分组成:分别是ECMAScript,BOM和DOM. 1)由此看出,ECMAScript是Java ...

  6. python实现归并排序,归并排序的详细分析

    python实现归并排序,归并排序的详细分析.   学习归并排序的过程是十分痛苦的.它并不常用,看起来时间复杂度好像是几种排序中最低的,比快排的时间复杂度还要低,但是它的执行速度不是最快的.很多朋友不 ...

  7. Apache Maven(七):settings.xml

    settings.xml 文件中包含settings标签,这个标签可以配置如何去执行Maven.其中包括本地存储库位置,备用远程存储库服务器和身份验证信息等值. 有如下两个位置可能存放这setting ...

  8. SQL优化之语句优化

    昨天与大家分享了SQL优化中的索引优化,今天给大家聊一下,在开发过程中高质量的代码也是会带来优化的 网上关于SQL优化的教程很多,但是比较杂乱.整理了一下,写出来跟大家分享一下,其中有错误和不足的地方 ...

  9. hdcms v5.7.0学习笔记

    hdcms v5.7.0学习笔记 https://note.youdao.com/ynoteshare1/index.html?id=c404d63ac910eb15a440452f73d6a6db& ...

  10. python2.7入门---函数

        不是说现在的高级程序员都是秉承着用最少的代码实现功能么,那么,怎么才能使代码少呢?好吧,不装哔~~~了,这一波操作我说不来,咱们直接来看内容.首先,函数是组织好的,可重复使用的,用来实现单一, ...