ByteArrayInputStream真的是一个流吗?

huangapple go评论59阅读模式
英文:

Is ByteArrayInputStream really a stream?

问题

Point A >> 在Java中可以通过单个调用完整读取文件并将数据存储在内存中进行处理,适用于较小大小的文件。很好!

Point B >> 当涉及非常大的文件时,我们需要进行一定的优化,以避免内存溢出。因此IOStream存在,其名称本身就表明它是一个流。

Point C >> 读取部分数据并进行一些处理。再读取一些数据,忽略先前读取的数据,在当前读取上执行某些操作,依此类推……这定义了数据的流式传输。

对于问题1:使用ByteArrayInputStream,我必须使用已经在内存中的整个数据(使用byte[])初始化流。那么,它首先为什么是InputStream的子类?因为它实际上并没有流式传输任何内容。

对于问题2:为什么JavaDoc中会提到ByteArrayInputStream的缓冲区。它是在哪里应用的?

抱歉,如果我理解有误。我在网络上没有找到与这些具体问题相关的答案。

是否有人可以在这里提供帮助?

英文:

Point A >> It is possible to read the complete file in a single call in Java and store the data in-memory and do-processing. It applicable for smaller-sized files. Fine!

Point B >> When it comes to very large files, we need to do some sort of optimization to not hit up the memory overflow. Hence the IOStream exist whose name itself convey it is a Stream.

Point C >> Read some portion of data and do some processing. Read some more and forget about the data from previous read and do something on current read and so - on... which defines the data is streaming.

I see a confusion between the standard definition of the Streaming concept from point C (and) the existence of ByteArrayInputStream.

Question 1 : With ByteArrayInputStream, I have to initialise the stream with the entire data (using byte[]) I ALREADY have in-memory. Why then, it is a subclass of InputStream in first place? because it stream's nothing.

Question 2 : Why does the JavaDoc say something about buffering for ByteArrayInputStream. Where does it get applied?

Sorry, If my understanding is bad. I didn't get related answers for such narrowed questions online.

Can someone help here?

答案1

得分: 3

因为对于该类的用户而言,ByteArrayInputStream行为就像是流,即它具有相同的接口。

更一般地说,你应该将流视为具有特定接口的东西,而不是将其视为用于特定目的的东西。

例如,对于ArrayList也是如此,它的行为类似于List,但在内部却是一个数组,这意味着与(例如)LinkedList不同,它具有恒定的随机访问时间。

英文:

Because, to the user of the class, a ByteArrayInputStream behaves like a stream, i.e. it has the same interface.

More generally, you should think of a stream as something that has a certain interface, not as something that is used for a certain purpose.

The same is true, for example, for ArrayList which behaves like a List but, internally, is an array, which means that, unlike (for example) a LinkedList it has constant random access times.

huangapple
  • 本文由 发表于 2020年8月15日 17:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63424551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定