当前位置:网站首页>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()
方法进行重置.
运行后输出结果如下:
边栏推荐
- MySQL中的存储过程(详细篇)
- Informatics Olympiad 1915: [01NOIP Popularization Group] Greatest Common Divisor and Least Common Multiple | Luogu P1029 [NOIP2001 Popularization Group] The problem of the greatest common divisor and
- JMeter Notes 4 | JMeter Interface Introduction
- Moralis去中心化Web3应用开发教程
- Basic knowledge points in js - BOM
- 开源盛宴ApacheCon Asia 2022即将开幕,精彩不容错过!
- 习题:变量、常量和基本数据类型
- 论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
- linux 下MySQL本地安装mysql - u root - p 无法登入
- 首发!阿里技术大牛最新耗时半个月整理出最全MySQL性能优化和高可用架构技术宝典,直接封神!
猜你喜欢
公司部门来了个00后测试卷王之王,老油条表示真干不过,已经...
顺通海关查验预约综合管理系统
This year..I sincerely recommend the professional engineer to upgrade to the book!
强啊,点赞业务缓存设计优化探索之路。
weiit新零售小程序如何探索数字化门店的破局之路
软件测试13年从业经验的前辈,总结的5条测试就业建议....
Vulkan与OpenGL对比——Vulkan的全新渲染架构
宽带射频放大器OA4SMM4(1)
Ecplise执行C语言报错:cannot open output file xxx.exe: Permission denied
Metaverse Web 3.0 和 DeFi大师班
随机推荐
JVM诊断命令jcmd介绍
编曲软件FL Studio中文版安装教程及切换语言教程
「Redis应用与深度实践笔记」,深得行业人的心,这还不来看看?
Prometheus 基本概念
顺通海关查验预约综合管理系统
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
BI报表与数据开发
What are the applications of X-rays?
莫队--优雅的暴力
荐书 | 推荐好评如潮的3本数据库书籍
Vulkan与OpenGL对比——Vulkan的全新渲染架构
Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB
高性能短链设计
今年这情况。。真心推荐专科的工程师升个本!
知识蒸馏4:准备数据集并修改网络配置
C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
How Google earth engine realizes the arrangement and selection of our time list
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
Leetcode数据库系列题解合集(持续更新)
一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧