为什么没有更多的Java代码使用PipedInputStream / PipedOutputStream? [英] Why doesn't more Java code use PipedInputStream / PipedOutputStream?

查看:204
本文介绍了为什么没有更多的Java代码使用PipedInputStream / PipedOutputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了这个成语,我想知道是否有我遗漏的东西。我从来没有见过它使用。几乎所有我在野外工作的Java代码都喜欢把数据写入字符串或缓冲区,而不是像这个例子(使用HttpClient和XML API):

I've "discovered" this idiom recently, and I am wondering if there is something I am missing. I've never seen it used. Nearly all Java code I've worked with "in the wild" favors slurping data into a string or buffer, rather than something like this example (using HttpClient and XML APIs for example):

    final LSOutput output; // XML stuff initialized elsewhere
    final LSSerializer serializer;
    final Document doc;
    // ...
    PostMethod post; // HttpClient post request
    final PipedOutputStream source = new PipedOutputStream();
    PipedInputStream sink = new PipedInputStream(source);
    // ...
    executor.execute(new Runnable() {
            public void run() {
                output.setByteStream(source);
                serializer.write(doc, output);
                try {
                    source.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }});

    post.setRequestEntity(new InputStreamRequestEntity(sink));
    int status = httpClient.executeMethod(post);

该代码使用Unix管道样式技术来防止XML数据的多个副本保存在内存中。它使用HTTP Post OutputStream和DOM加载/保存API将XML文档序列化为HTTP请求的内容。就我可以告诉它最小化的内存使用非常少的额外的代码(只有几行为Runnable,PipedInputStream和PipedOutputStream)。

That code uses a Unix-piping style technique to prevent multiple copies of the XML data being kept in memory. It uses the HTTP Post OutputStream and the DOM Load/Save API to serialize an XML Document as the content of the HTTP request. As far as I can tell it minimizes the use of memory with very little extra code (just the few lines for Runnable, PipedInputStream, and PipedOutputStream).

这个成语?如果这个成语没有错,为什么我没看到它?

So, what's wrong with this idiom? If there's nothing wrong with this idiom, why haven't I seen it?

编辑:澄清,PipedInputStream和PipedOutputStream替换样板缓冲区缓冲区副本并且它们还允许您同时处理传入数据并写出处理的数据。他们不使用操作系统管道。

to clarify, PipedInputStream and PipedOutputStream replace the boilerplate buffer-by-buffer copy that shows up everywhere, and they also allow you to process incoming data concurrently with writing out the processed data. They don't use OS pipes.

推荐答案

Javadocs


通常,一个线程从PipedInputStream对象读取数据,并且由其他线程将数据写入对应的PipedOutputStream。尝试从单个线程使用这两个对象不被推荐,因为它可能会死锁线程。

Typically, data is read from a PipedInputStream object by one thread and data is written to the corresponding PipedOutputStream by some other thread. Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.

这可能部分解释为什么不太常用。

This may partially explain why it is not more commonly used.

我假设另一个原因是许多开发人员不明白其目的/好处。

I'd assume another reason is that many developers do not understand its purpose / benefit.

这篇关于为什么没有更多的Java代码使用PipedInputStream / PipedOutputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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