什么是stream?

平常最经常看到的就是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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值