当前位置:网站首页>ByteArrayInputStream class source code analysis
ByteArrayInputStream class source code analysis
2022-07-30 18:01:00 【51CTO】
这是 《水煮 JDK 源码》系列 的第3篇文章,计划撰写100篇关于JDK源码相关的文章
ByteArrayInputStream 类位于 java.io 包下,继承于 InputStream 类,Represents a byte array input stream,It creates a byte array buffer in memory,Then save all the data read from the input stream in the buffer,其 UML 类图如下:

1、成员变量
在 ByteArrayInputStream 中定义了4个成员变量,如下:
2、构造方法
创建 ByteArrayInputStream There are two main ways to input stream of byte array:
3、读取字节方法
ByteArrayInputStream Two methods for reading bytes are provided,如下:
上面的两个 read() 方法都使用了 synchronized 关键字,That is, it is represented as a synchronous method,It can be seen from the specific implementation,ByteArrayInputStream The data in can not be read repeatedly,Each read a byte of data,The index of the next readable byte position is automatic+1,If the last element of the byte array has been read,can no longer be read.
4、其他方法
ByteArrayInputStream The class provides some other methods,如下:
public synchronized long skip(long n):Skip from the input stream n 个字节;public synchronized int available():Gets the length in bytes that can also be read from the input stream;public boolean markSupported():Determines whether the input stream supports the tagging function,默认返回 true,即ByteArrayInputStream是支持的;public void mark(int readAheadLimit):Sets the marker position in the input stream to the current readable position;public synchronized void reset():Resets the next readable position in the input stream to the marked position;public void close():关闭输入流;
下面分别看看这些方法的具体实现.
和 ByteArrayOutputStream 一样,ByteArrayInputStream 类中的 close() methods are also implemented without any code,即使被调用,也没有任何的作用.
5、示例代码
5.1 Simple data read
上面分析了 ByteArrayInputStream 类的相关源码,Let's take a look at it with a simple example ByteArrayInputStream 该如何使用.
运行后,输出的结果如下:
5.2 验证不可重复读
Let's verify that the input stream cannot be read repeatedly.
运行后,输出结果如下:
5.3 Repeatable read implementation
从上面的结果可以看出,第2次读取时,未读取到任何数据,If you want to be able to read again,那么又该如何处理呢?可以在第1after reading,使用 reset() 方法进行重置.
运行后输出结果如下:
边栏推荐
- 18.支持向量机(SVM)的介绍
- un7.30:Linux——如何在docker容器中显示MySQL的中文字符?
- linux 安装mysql8.0 超详细教程(实战多次)
- This year..I sincerely recommend the professional engineer to upgrade to the book!
- What is industrial radiography equipment?
- PLSQL Developer安装和配置
- 自动化早已不是那个自动化了,谈一谈自动化测试现状和自我感受……
- 编曲软件FL Studio中文版安装教程及切换语言教程
- Mysql brush dirty several scenarios and related parameters
- 【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
猜你喜欢
随机推荐
Dodging ice cream assassins?Crawling ice cream prices through crawlers
C陷阱与缺陷 第6章 预处理器
分布式消息队列平滑迁移技术实战
【HMS Core】【FAQ】运动健康、音频编辑、华为帐号服务 典型问题合集7
mysql刷脏的几种场景以及相关参数
LayaBox---TypeScript---变量声明
多年以后「PageHelper」又深深的给我上了一课
EMC VPLEX VS2 SPS电池更换详细探讨
【解决】关于 Unity Hub 获取许可证失败 或 无响应导致无法开发的问题
SQL存储过程详解
Ecplise执行C语言报错:cannot open output file xxx.exe: Permission denied
什么是无损检测设备?
Microsoft Office 2019 software download and installation detailed tutorial!
Logback的使用
[Solved] The problem that Unity Hub fails to obtain a license or does not respond and cannot develop
Mo Team - Elegant Violence
高级语言垃圾回收思路和如何减少性能影响原理分析
莫队--优雅的暴力
Graph Attention Mechanism
Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB









