关于Java中的流的问题

发布于 2024-11-03 20:43:11 字数 454 浏览 6 评论 0原文

过去几天我一直在阅读有关 Java 中的流的内容。在阅读了相当多的内容后,我开始理解选择“流”这个名字是因为与我们在“现实生活”中使用的词(例如水)相似。而且没有必要知道数据来自哪里。如果我理解错误请纠正。

但我不明白这一点。例如,当我在套接字上说 getOutputStreamgetInputStream 时,我会得到一个 InputStream,我可以将其链接到我喜欢的任何内容。但是InputStream/OutputStream不是抽象类吗?我不知道如何正确解释它,但我不明白仅通过调用该方法的套接字连接就自动具有字节/字符可以流动的流/通道?实际上什么是输入流/输出流?流是抽象真实源的一种方式吗?

我想我理解链接它们的各种方式,但是我觉得我错过了这个概念的核心。

由于缺乏正确的解释方式,如果问题不好,我会删除它。

感谢您抽出时间。

I have been reading about streams in Java the past days. After reading quite a bit I start to understand that the name "stream" was chosen because of similarity to what we use the word about in "real life" such as water. And that it is not necessary to know where the data comes from. Please correct if I have interpreted it wrong.

But I do not understand this. When I say getOutputStream or getInputStream on a socket for example I get a InputStreamwhich I can chain to whatever I like. But isn't the InputStream/OutputStream abstract classes? I don't know exactly how to explain it properly but I do not understand that a socket connection just by invoking that method automatically has a stream/channel where bytes/characters can flow? What is in fact a InputStream / OutputStream? Are streams a way of abstracting the real sources?

I think I understand the various ways of chaining them though, however I feel I miss the core of the concept.

In lack of a proper way of explaining it I will delete question if it is bad.

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天荒地未老 2024-11-10 20:43:11

InputStream/OutputStream 是抽象。它们为您提供了一些用于读/写字节或字节组的基本 API,而无需公开实际的实现。让我们以OutputStream为例:

OutputStream通过公共API接收一堆字节。您实际上并不知道(也不关心)这些字节之后发生了什么:它们已发送。真正的实现可能:将它们附加到文件中,忽略它们(Apache Commons 中的 NullOutputStream ),将它们保存在内存中或...通过套接字发送。

这就是当你调用Socket.getOutputStream()时发生的情况:你得到OutputStream一些实现,只是不在乎,它是实现- 依赖和具体。当您向此流发送字节时,底层实现将使用 TCP/IP 或 UDP 推送它们。事实上,TCP/IP 本身是一个流协议,尽管它是在数据包/帧上运行的。

InputStream 的情况类似 - 您从套接字获得一些 实现。当您向流请求几个字节时,底层的 InputStream 实现将向操作系统套接字请求相同数量的字节,这可能会阻塞。但这才是继承的真正乐趣:你不在乎!只需按照您想要的方式使用这些流即可,例如链接、缓冲等。

InputStream/OutputStream are, well, abstractions. They give you some basic API for reading/writing bytes or groups of bytes without exposing the actual implementation. Let's take OutputStream as an example:

OutputStream receives a bunch of bytes through the public API. You don't actually know (and care) what is happening with these bytes afterwards: they are sent. Real implementation may: append them to file, ignore them (NullOutputStream in Apache Commons), save them in memory or... send through socket.

This is what happens when you call Socket.getOutputStream(): you get some implementation of OutputStream, just don't care, it is implementation-dependent and specific. When you send bytes to this stream, underlying implementation will push them using TCP/IP or UDP. In fact TCP/IP itself is a stream protocol, even though it operates on packets/frames.

The situation is similar for InputStream - you get some implementation from socket. When you ask the stream for few bytes, the underlying InputStream implementation will ask OS socket for the same amount of bytes, possibly blocking. But this is the real fun of inheritance: you don't care! Just use these stream any way you want, chaining, buffering, etc.

憧憬巴黎街头的黎明 2024-11-10 20:43:11

当您调用getInputStream时,套接字会返回InputStream的某个具体子类的实例。通常您不必担心返回对象的确切类;因为它是一个InputStream,所以您只需以这种方式使用它即可。 (子类甚至可能是套接字类的私有嵌套类。)

When you call getInputStream, the socket returns an instance of some concrete subclass of InputStream. Usually you don't worry about the exact class of the returned object; since it is an InputStream, you just work with it that way. (The subclass may even be a private nested class of the socket class.)

长伴 2024-11-10 20:43:11

InputStream 确实是一个抽象。在每种情况下都可以使用流概念的不同实现。但流的用户不需要知道确切的实现是什么。

对于Socket,实现是一个SocketInputStream,它扩展了FileInputStream

InputStream is indeed an abstraction. On each occasion a different implementation of the stream concept can be used. But the user of the stream does not need to know what was the exact implementation.

In the case of Socket, the implementation is a SocketInputStream which extends FileInputStream

时光匆匆的小流年 2024-11-10 20:43:11

我不认为这是一个坏问题。你说得很对,流从你身上抽象出了数据来源的复杂性并使其统一。因此,您可以编写从文件或套接字读取的代码,并且该代码看起来几乎相同。这意味着您通常需要编写更少的代码。

当您从 Socket 获取 InputStream 时,您就可以访问到达该套接字的任何数据。从此流中读取数据时,通常,您提供一个字节数组并要求流为您填充它。它将读取尽可能多的可用数据,否则将填充缓冲区。如何处理这个字节数组中的数据取决于您。

不过,对于任何类型的 Socket IO,尽管已经说了有关 Streams 的所有内容,但 Java 套接字 API 已经相当古老,并且有一些非常好的替代方案可以包装它并且更易于使用。我强烈推荐 Netty 使用它你可以忘记流并专注于 POJO 以及如何编码和解码它们。

I don't think this is a bad question. You're quite right that streams abstract away from you the complexity of where the data is coming from and make it uniform. Therefore you can write code that reads from a File or a Socket and that code could look almost identical. It means you generally have to write less code.

When you get the InputStream from a Socket you are able to access any data arriving at that socket. When reading from this stream, typically, you supply a byte array and ask the stream to fill it for you. It will read as much data as is available or it will fill the buffer. It's the up to you what you do with the data in this byte array.

For any kind of Socket IO though, having said all this about Streams, the Java socket API is quite old and there are some really good alternatives available which wrap it and are easier to use. I highly recommend Netty using which you can forget about streams and focus on POJOs and how to encode and decode them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文