Java中的输入和输出流管道 [英] Input and Output Stream Pipe in Java

查看:151
本文介绍了Java中的输入和输出流管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何关于在Java中创建Pipe对象的好建议, 既是InputStream又是OutputStream,因为Java没有多重继承,而且这两个流都是抽象类而不是接口?

Does anyone have any good suggestions for creating a Pipe object in Java which is both an InputStream and and OutputStream since Java does not have multiple inheritance and both of the streams are abstract classes instead of interfaces?

潜在的需求是让一个对象可以传递给需要一个InputStream或一个OutputStream的东西来管道从一个线程输出到另一个线程的输入。 / p>

The underlying need is to have a single object that can be passed to things which need either an InputStream or an OutputStream to pipe output from one thread to input for another.

推荐答案

似乎错过了这个问题的重点。如果我理解正确的话,你想要一个在一个线程中起到类似InputStream的对象,在另一个线程中想要一个OutputStream来创建一个在两个线程之间进行通信的方法。

It seems the point of this question is being missed. If I understand you correctly, you want an object that functions like an InputStream in one thread, and an OutputStream in another to create a means of communicating between the two threads.

也许一个答案是使用组合而不是继承(无论如何都是推荐的练习)。使用getInputStream()和getOutputStream()方法创建一个包含彼此连接的PipedInputStream和PipedOutputStream的管道。

Perhaps one answer is to use composition instead of inheritance (which is recommended practice anyway). Create a Pipe which contains a PipedInputStream and a PipedOutputStream connected to each other, with getInputStream() and getOutputStream() methods.

您无法直接将Pipe对象传递给需要流的东西,但你可以传递它的get方法的返回值。

You can't directly pass the Pipe object to something needing a stream, but you can pass the return value of it's get methods to do it.

这对你有用吗?

这篇关于Java中的输入和输出流管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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