inputstream.read在Java中读取多少数据 [英] How much data does inputstream.read reads in java

查看:72
本文介绍了inputstream.read在Java中读取多少数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看inputstream对象的read方法的定义,因为每次读取多少数据,我都感到困惑,因为它说读取一些字节"

I was looking at the definition of the read method of inputstream object, I am confused about how much data is read everytime since it says "reads some bytes"

public int read(byte[] b)
         throws IOException
Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

可以说我有一个大小为200的缓冲区数组,输入流中的数据为100字节.是否可以保证inputStream.read获得所有100个字节?

Lets say i have a buffer array of size 200 and the data in the inputstream is 100 bytes. Is it guaranteed that inputStream.read gets all 100 bytes ?

推荐答案

此接口的全部要点(或者确切地说是抽象类):您可以绝对不依靠假设读取了多少字节.您总是总是必须检查该方法的返回值才能知道.

The whole point of this interface (or to be precise: abstract class): you can absolutely not rely on assuming how many bytes were read. You always always always have to check the return value of that method to know.

背景:此接口有许多不同的实现.我的缓冲区有些,有些可能没有.一些读取固定"输入(可能从内存中的现有数据).可能有人会决定给您提供流向Internet的流,下载10 GB的文件,然后开始向您发送一个字节.

Background: there are many different implementations for this interface. Some my buffer, some may not. Some read "fixed" input (maybe from existing data in memory). Somebody might decide to give you a stream that turns to the internet, download a 10 GB file and then start sending you one byte after the other.

您唯一知道的是:方法返回

The only thing you know is: the method returns

读入缓冲区的总字节数

the total number of bytes read into the buffer

故事的结尾.

这篇关于inputstream.read在Java中读取多少数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆