当前位置:网站首页>Bufferedinputstream buffer fill problem
Bufferedinputstream buffer fill problem
2022-07-23 08:28:00 【Crawling Oriole】
List of articles
BufferedInputStream The function of is to provide buffer function for other input streams . establish BufferedInputStream when , We will specify an input stream as a parameter through its constructor .BufferedInputStream The input stream data will be read in batches , Read a portion at a time into the buffer ; After this part of the buffer has been manipulated , And then read the next part of the data from the input stream .
problem
Recently, I encountered a problem in the process of communicating with the server , The size of the data stream returned by the server is 2602 Bytes , When I receive it on the client , These bytes cannot be read at one time . The sample code is as follows :
byte[] buffer = new byte[2602];
// Write the data in the buffer to the byte array buffer in .0 It's an array of bytes buffer Starting position ,2602 Is the write length
int pos = read(buffer, 0, 2602);
The above example code is normal pos The value should be 2602, But sometimes it's 1434, Read 1434 Bytes .
analysis
First, let's be clear BufferedInputStream The default filling area is 8192 Bytes , This buffer is not filled at one time , It is filled in step by step , That is, after the user reads the current data , Refilled , So the cycle ends .
Solution
As described above 2602 byte , Is the data returned by the server , In the actual project, there must be variables , It depends on the length of the data returned by the server .
while Cyclic segmented reading
int num = 0;
while (num < 2602) {
int pos = read(buffer, num, 2602 - num);
num += pos;
}
for Cycle a byte by byte reading
int num = 0;
for (int i=0;i<2602;i++) {
buffer[i] = (byte)read();
num++;
}
边栏推荐
- 构造函数的初始化、清理及const修饰成员函数
- Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
- buu web
- QgrapicsView实现画板
- Typora set the title to automatically add sequence number
- 【小程序的 插值表达式,渲染判断,绑定事件以及分享】
- 【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection
- RPC-BDY(5)-服务自动注销、负载均衡
- Easily take you to the gate of turtle drawing
- RequestContextHolder
猜你喜欢

动作捕捉在自动化控制领域的应用

Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life

promise(二)
[email protected]、$?、env看所有的全局变量值、set看所有变量"/>Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量

Send benefits again! Calendar applet source code

Go 并发编程基础:什么是上下文

阿里云国际版忘记会员名或登录密码,怎么办?

你知道怎么做好接口测试?

What if Alibaba cloud international forgets its member name or login password?

程序员可能还是程序员,码农可能只能是码农了
随机推荐
Redis transaction and locking mechanism
30行自己写并发工具类(Semaphore, CyclicBarrier, CountDownLatch)是什么体验?
Use the same interface to realize different login methods
轻松带你走进turtle绘图的大门
动作捕捉在自动化控制领域的应用
Vue组件切换时缓存组件状态即动态组件keep-alive 动态组件与异步组件
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
微信小程序中使用全局数据实现数据共享
Redistemplate pipeline use
The cubic root of a number
容器监控三剑客CAdvisor收集监控数据 + InfluxDB储存数据 + Granfana展示图表数据的简介
云计算或成时代新拐点?从哪些点可以看出?
深入浅出地理解STM32中的中断系统——从原理到简单工程示例——保姆级教程
Several ways of QT thread exit
Lc: sword finger offer 39. numbers that appear more than half of the time in the array
【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection
Genesis公链:夯实Web 3.0发展底座
appendToFile追加失败
C语言函数(1)
Algorithm --- use the minimum cost to climb stairs (kotlin)