什么是stream?

来源:互联网 发布:企业网软件下载 编辑:程序博客网 时间:2024/05/17 09:01

平常最经常看到的就是IOstream了,还有其他各种stream,FileStream,NetworkStream。

stream表示的是一连串的objects,(在java的世界里,所有的东西都是objects),譬如连续好几个int,好几个float,或者其他类对象等等。

想象stream就是一条河流,我们不用关心它的源头在哪里,只需要知道我们可以在目的地(海?)可以收集到来自五湖四海的一样。一般情况下,我们也不用管这河流源头在哪里,只需要拿到我们需要的东西。

同样,通过stream,我们想获取data,不用管其源头( a file, a socket,serial ports等等)。

stream就是提供了我们一个抽象层把我们和data的来源隔开。

Typical operations on a stream:

  • read one byte. Next time you read, you'll get the next byte, and so on.
  • read several bytes from the stream into an array
  • seek (move your current position in the stream, so that next time you read you get bytes from the new position)
  • write one byte
  • write several bytes from an array into the stream
  • skip bytes from the stream (this is like read, but you ignore the data. Or if you prefer it's like seek but can only go forwards.)
  • push back bytes into an input stream (this is like "undo" for read - you shove a few bytes back up the stream, so that next time you read that's what you'll see. It's occasionally useful for parsers, as is:
  • peek (look at bytes without reading them, so that they're still there in the stream to be read later)

summary:

a stream is just a source of input, hiding away (abstracting) another source. As long as you don't break the abstraction, your code will be very flexible.

注:本文从stackoverflow总结而来,详细请看:

http://stackoverflow.com/questions/1216380/what-is-a-stream/1216400#1216400

http://stackoverflow.com/questions/507747/can-you-explain-the-concept-of-streams

0 0
原创粉丝点击