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. MySQL数据库主从(主主)配置

    一.系统环境: centos7.4 (centos 1708) mysql 5.7 master主机的IP地址为192.168.159.50 slave主机的IP地址为192.168.159.51 M ...

  2. 使用js获取表单元素的值

    function getParams(formName) { var frmMain = document.getElementById(formName)?document.getElementBy ...

  3. flask中的request

    1.request是什么? 简单来说,它就是flask的封装的一个对象,这个对象包含着前端请求所带的所有信息.既然说它是一个对象,那么它肯定是有一些熟悉,和方法的,下面就来介绍下request里的熟悉 ...

  4. C++定义一个简单的Computer类

    /*定义一个简单的Computer类 有数据成员芯片(cpu).内存(ram).光驱(cdrom)等等, 有两个公有成员函数run.stop.cpu为CPU类的一个对象, ram为RAM类的一个对象, ...

  5. poj_1091_跳蚤

    Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最后一个是M ...

  6. 一、spring 4概述

    0 前言 0.0 Spring 来历 Spring 是于2003年兴起的一个轻量级的Java 开发框架, 为了解决企业应用开发的复杂性而创建, 核心是控制反转(IoC)和面向切面编程(AOP). 简单 ...

  7. 用 jQuery 实现表单验证(转载)

    jQuery 官方 API 地址: http://api.jquery.com/ 在线引用 jQuery:http://code.jquery.com/ ——选自<锋利的jQuery>(第 ...

  8. frame3.5安装出错

    一般是因为禁用了microsoft update,可以在服务里禁用改为手动,之后启动,然后就可以安装

  9. 来自一枚初生牛犊不怕虎的小菜鸟的Mock.js使用,不足之处欢迎读者的指出 谢谢

    本文章写的是基于require的mock.js的几种常用生成随机数据和ajax模拟前后端的交互信息 <script src="./app/libs/require.js"&g ...

  10. jQuery 使用问题

    attr('checked', 'checked')调用多次仅第一次生效 使用attr()获取这些属性的返回值为String类型,如果被选中(或禁用)就返回checked.selected或disab ...