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. 整理下react中常见的坑

    其实有些也不能算是坑,有些是react的规定,或者是react的模式和平常的js处理的方式不同罢了 1.setState()是异步的this.setState()会调用render方法,但并不会立即改 ...

  2. 史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f5-zuul/ 本文出自方志朋的博客 在微服务架构中,需要几 ...

  3. C#中Math.Round()的中国式用法

    C#中的Math.Round()并不是使用的"四舍五入"法.而是(银行家算法),即:四舍六入五取偶.事实上这也是IEEE的规范,因此所有符合IEEE标准的语言都应该采用这样的算法. ...

  4. 微信小程序流量主如何开通

    2018年7月09日,微信小程序流量主全面开通,开通条件如下: 累计独立访客(UV)不低于1000          # 一共一千个人访问你的小程序就可以申请(不限时间) 有严重违规记录的小程序不予申 ...

  5. Vue+node.js+express+mysql实例---对图书信息进行管理

    一个简单的 CURD 实例 ---对图书信息进行管理 目录 1 开发环境 1.1 前端开发环境 1.2 后端开发环境 2 数据库设计和创建 2.1 数据库和表设计 2.2 book 表设计 2.3 s ...

  6. window.location.href url含中文乱码问题

    (1).页面中先对中文进行编码. 如:window.location.href = url+"&groupName=" + encodeURI(encodeURI(grou ...

  7. #leetcode刷题之路26-删除排序数组中的重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: ...

  8. 使用Ansible实现nginx+keepalived高可用负载均衡自动化部署

    本篇文章记录通过Ansible自动化部署nginx的负载均衡高可用,前端代理使用nginx+keepalived,端web server使用3台nginx用于负载效果的体现,结构图如下: 部署前准备工 ...

  9. css3新样式

    超出两行变省略号 overflow:hidden; text-overflow:ellipsis;display:-webkit-box; -webkit-box-orient:vertical;-w ...

  10. latex03-LaTeX中的中文处理办法

    编译含中文的tex文件的前提有三个: 默认编译器为XeLaTeX: 编辑器的默认字体编码格式为UTF-8: 引入ctex的宏包. 效果: 源码: %导言区 \documentclass{article ...