Java基础知识之ByteArrayInputStream流的使用
作者:咕噜是个大胖子 时间:2023-10-27 14:37:53
Java ByteArrayInputStream流
一、ByteArrayInputStream流定义
API说明:ByteArrayInputStream包含一个内部缓冲区,其中包含可以从流中读取的字节,内部计数器跟踪read方法提供的下一个字节,关闭ByteArrayInputStream流无效,关闭流后调用类的方法不会有异常产生
二、ByteArrayInputStream流实例域
/**
* 字节数组缓冲区,buf[0]到buf[count-1]是可以从流中读取的字节,buf[pos]是读取的下一字节
*/
protected byte buf[];
/**
*读取字节的索引
*/
protected int pos;
/**
* 流中当前标记的位置,默认标记为0,可以通过mark方法设置新的标记点,而后通过reset方法将当前位置设置为标记点
* 从标记点开始读取数据
*
* @since JDK1.1
*/
protected int mark = 0;
/**
* 索引结束位置+1,不大于缓冲区的长度
*/
protected int count;
三、ByteArrayInputStream流构造函数
/**
* 使用指定字节数组创建ByteArrayInputStream流,字节数组为流的缓冲区,
* 当前位置索引pos初始值是0,索引结束位置count的是buf的长度
*/
public ByteArrayInputStream(byte buf[]) {
this.buf = buf;
this.pos = 0;
this.count = buf.length;
}
/**
* 使用指定的数组创建ByteArrayInputStream流
* 目标数组为流的缓冲区数组
* 缓冲区当前起始位置变量值为off
* 缓冲区的索引结束位置为:buf.length和off+length的最小值
*/
public ByteArrayInputStream(byte buf[], int offset, int length) {
this.buf = buf;
this.pos = offset;
this.count = Math.min(offset + length, buf.length);
this.mark = offset;
}
四、ByteArrayInputStream流方法
1)read()
:从此输入流中读取下一个字节并返回,当流到达末尾时,返回-1
/**
* 从此输入流中读取下一个字节并返回
* 当流到达末尾时,返回-1
* 注意& 0xff是字节的补码操作,暂时不用理会
*/
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
2)read(byte b[], int off, int len)
:从输入流中读取最多len个字节到目标数组中,返回实际读取的字节数
/**
* 从输入流中读取最多len个字节到目标数组中,返回实际读取的字节数
* 当缓冲区中剩余字符数小于len个字节时,读取缓冲区剩余字符数
* 当剩余字符数大于len个字节时,读取len个字节
*/
public synchronized int read(byte b[], int off, int len) {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
if (pos >= count) {
return -1;
}
int avail = count - pos;
if (len > avail) {
len = avail;
}
if (len <= 0) {
return 0;
}
System.arraycopy(buf, pos, b, off, len);
pos += len;
return len;
}
3)close()
:关闭流无效,关闭后调用其它方法不会有异常
/**
* 关闭流无效,关闭后调用其它方法不会有异常
*/
public void close() throws IOException {
}
五、ByteArrayInputStream流的作用
暂时不理解具体作用,不清楚什么时候会用到该流,因为实际项目暂未用到,故先了解其功能即可
六、ByteArrayInputStream的用法解析
看下面这个程序,看懂了就会了
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
public class Test4 {
//ByteArrayInputStream本身操作的是一个数组,并没有打开文件描述之类的,所有不需要关闭流
public static void main(String[] args) {
ByteArrayInputStream bais=null;
StringBuilder sb=new StringBuilder();
int temp=0;
int num=0;
long date1=System.currentTimeMillis();
try{
byte[] b="abcdefghijklmnopqstuvxyz".getBytes();
//从字符数组b中读取数据,从下标为2开始计数读8个
bais=new ByteArrayInputStream(b,2,8);
while((temp=bais.read())!=-1){
sb.append((char)temp);
num++;
}
System.out.println(sb);
System.out.println("读取的字节数:"+num);
}finally{
try{
bais.close();//不需要关闭流的,但是调用close没有任何影响,close不做任何事情
}catch(IOException e){
e.printStackTrace();
}
new File("d:"+File.separator+"a.txt");//File.separator是一个文件分隔符,在windows和linux平台下运行都没有问题
}
long date2=System.currentTimeMillis();
System.out.println("耗时:"+(date2-date1));
}
}
来源:https://blog.csdn.net/ai_bao_zi/article/details/81355044
标签:Java,ByteArrayInputStream,ByteArrayInputStream流
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis-Plus自动填充更新操作相关字段的实现
2022-01-14 20:43:01
Spring Boot Admin 进行项目监控管理的方法
2021-09-01 23:39:19
![](https://img.aspxhome.com/file/2023/1/67001_0s.png)
Java读写文件,在文件中搜索内容,并输出含有该内容的所有行方式
2022-12-14 18:23:58
![](https://img.aspxhome.com/file/2023/7/94207_0s.jpg)
java中 ${} 和 #{} 有什么区别
2023-11-29 01:34:32
![](https://img.aspxhome.com/file/2023/5/67015_0s.png)
c#获取本机在局域网ip地址的二种方法
2023-01-20 00:19:47
java静态工具类注入service出现NullPointerException异常处理
2021-11-28 09:59:11
![](https://img.aspxhome.com/file/2023/0/88520_0s.jpg)
简单分析针对ARM平台的C语言程序的编译问题
2021-06-19 21:59:13
IDEA编译乱码Build Output提示信息乱码
2023-08-07 12:14:35
![](https://img.aspxhome.com/file/2023/9/72369_0s.png)
Qt TCP实现简单通信功能
2021-07-07 02:16:30
![](https://img.aspxhome.com/file/2023/3/124023_0s.jpg)
详解SpringCloud的负载均衡
2022-03-14 03:42:28
![](https://img.aspxhome.com/file/2023/8/78348_0s.png)
android图片处理 让图片变成圆形
2023-10-03 00:47:30
![](https://img.aspxhome.com/file/2023/6/83536_0s.jpg)
android 自定义圆角button效果的实例代码(自定义view Demo)
2022-07-13 11:35:09
![](https://img.aspxhome.com/file/2023/1/139471_0s.png)
Android实现CoverFlow效果控件的实例代码
2023-06-23 13:12:43
![](https://img.aspxhome.com/file/2023/5/83545_0s.png)
java通过ip获取客户端Mac地址的小例子
2021-12-22 06:37:07
SpringBoot 如何编写配置文件
2023-07-14 10:38:24
![](https://img.aspxhome.com/file/2023/2/84302_0s.png)
java简单列出文件夹下所有文件的方法
2022-12-23 19:25:05
Java提取2个集合中的相同和不同元素代码示例
2023-11-28 05:48:41
教你在 Java 中实现 Dijkstra 最短路算法的方法
2023-04-03 00:19:42
![](https://img.aspxhome.com/file/2023/0/99900_0s.png)
c#的sortedlist使用方法
2023-09-22 04:58:13
浅谈Android Studio如何Debug对应so文件C/C++代码
2023-11-20 03:23:35
![](https://img.aspxhome.com/file/2023/2/89552_0s.jpg)