BufferedInputStream/BufferedOutputStream
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的更多相关文章
- 字节缓冲流 ( BufferedInputStream / BufferedOutputStream)
package com.sxt.reader; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; imp ...
- Java字节流:BufferedInputStream BufferedOutputStream
-----------------------------------------------------------------------------------BufferedInputStre ...
- BufferedInputStream/BufferedOutputStream复制文件
public class Test{ public static void main(String[] args) throws IOException{ FileInputStream in = n ...
- Java API —— IO流( FileInputStream & FileOutputStream & BufferedInputStream & BufferedOutputStream )
1.IO流概述 · IO流用来处理设备之间的数据传输 · 上传文件和下载文件 · Java对数据的操作是通过流的方式 · Java用于操作流的对象都在IO包中 2.IO ...
- [十二]JavaIO之BufferedInputStream BufferedOutputStream
功能简介 BufferedInputStream 和 BufferedOutputStream一样,他们都是过滤流 装饰器模式下具体的装饰类 用来装饰InputStream以及OutputStream ...
- BufferedInputStream&BufferedOutputStream
使用字符缓冲区相关实现copy文件: public static void main(String[] args) { //创建文件对象指定要拷贝的文件路径(源文件),文件须存在,测试用例不做判断 F ...
- 系统学习 Java IO (九)----缓冲流 BufferedInputStream/BufferedOutputStream
目录:系统学习 Java IO---- 目录,概览 BufferedInputStream BufferedInputStream 类为输入流提供缓冲. 缓冲可以加快IO的速度. BufferedIn ...
- 使用文件流与使用缓冲流完成文件的复制操作性能对比,文件流 FileInputStream FileOutputStream 缓冲流: BufferedInputStream BufferedOutputStream
package seday06; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOExc ...
- FileInputStream 与 BufferedInputStream 效率对比
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3550158.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...
随机推荐
- IntelliJ IDEA使用hibernate
环境:数据库:mariadb 10.2.16 https://downloads.mariadb.org/配置好maven:见收藏的博文链接 https://www.cnblogs.com/ICE_ ...
- iOS之查看代码运行的时间
有时候我们想要准确的知道某段代码.某个循环执行的时间,然后分析效率等问题,这个时候就需要执行时间是多少.正好看到网上已经有人做了这个工作,我就直接摘下来了.正好也用了宏的方式计算时间,我们只要在需要计 ...
- OI 刷题记录——每周更新
每周日更新 2016.05.29 UVa中国麻将(Chinese Mahjong,Uva 11210) UVa新汉诺塔问题(A Different Task,Uva 10795) NOIP2012同余 ...
- 在C++中如何实现文件的读写
一.ASCII 输出为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取代< fstream.h>,所 ...
- 编程 - 前端 - JavaScript - 库 - ECharts (开源可视化)
ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等) ...
- ajax在同一页面中同控制器不同方法中调用数据并异步刷新的实例
我在实习以来都有做一些笔记,之前做的笔记都在简书里,现在我提前把公司给我的任务做好了,坐在电脑前又不好玩别的,那么我就整理下我之前的笔记吧!(此项目是thinkphp5开发的) 先上效果图 这是整体页 ...
- mongodb的windows系统下安装
先下载安装包,地址有下面两个,按需选择吧. https://www.mongodb.com/download-center/v2/community https://www.mongodb.org/d ...
- Python基本图形绘制
turtle的一个画布空间最小单位是像素 turtle的绘制窗体:turtle.stup(width,heigth,startx,starty) 四个参数中后两个可选 turtle空间坐标体系:tur ...
- C++ vector二维数组
C++ 构建二维动态数组 int **p; p = ]; //注意,int*[10]表示一个有10个元素的指针数组 ; i < ; ++i) { p[i] = ]; } 这样就构成10*5的数组 ...
- xpath简单入门
语法: 选取节点: 实例: (贴图转载自w3school) 补充: /a/@href #获取a标签的href属性 当<div class="demo">& ...