- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个数据生成器,它在单独的线程中运行并将生成的数据推送到连接到 PipedInputStream
的 PipedOutputStream
中。此输入流的引用通过公共(public) API 公开,以便任何客户端都可以使用它。 PipedInputStream
包含一个有限的缓冲区,如果缓冲区已满,将阻止数据生产者。基本上,当客户端从输入流中读取数据时,数据生产者 会生成新数据。
问题是数据生产者可能会失败并抛出异常。但是由于消费者在单独的线程中运行,因此没有很好的方法将异常传递给客户端。
我所做的是捕获该异常并关闭输入流。这将导致 IOException
在客户端出现消息“Pipe closed”,但我真的很想向客户说明背后的真正原因。
这是我的 API 的粗略代码:
public InputStream getData() {
final PipedInputStream inputStream = new PipedInputStream(config.getPipeBufferSize());
final PipedOutputStream outputStream = new PipedOutputStream(inputStream);
Thread thread = new Thread(() -> {
try {
// Start producing the data and push it into output stream.
// The production my fail and throw an Exception with the reason
} catch (Exception e) {
try {
// What to do here?
outputStream.close();
inputStream.close();
} catch (IOException e1) {
}
}
});
thread.start();
return inputStream;
}
我有两个解决方法:
IOException
,客户端可以向 API 询问原因。close()
方法。然后流抛出的 IOException
可以包含该原因作为消息。有什么更好的主意吗?
最佳答案
巧合的是,我刚刚编写了类似的代码来允许对流进行 GZip 压缩。您不需要扩展 PipedInputStream,只需 FilterInputStream将执行并返回一个包装版本,例如
final PipedInputStream in = new PipedInputStream();
final InputStreamWithFinalExceptionCheck inWithException = new InputStreamWithFinalExceptionCheck(in);
final PipedOutputStream out = new PipedOutputStream(in);
Thread thread = new Thread(() -> {
try {
// Start producing the data and push it into output stream.
// The production my fail and throw an Exception with the reason
} catch (final IOException e) {
inWithException.fail(e);
} finally {
inWithException.countDown();
}
});
thread.start();
return inWithException;
然后 InputStreamWithFinalExceptionCheck 就是
private static final class InputStreamWithFinalExceptionCheck extends FilterInputStream {
private final AtomicReference<IOException> exception = new AtomicReference<>(null);
private final CountDownLatch complete = new CountDownLatch(1);
public InputStreamWithFinalExceptionCheck(final InputStream stream) {
super(stream);
}
@Override
public void close() throws IOException {
try {
complete.await();
final IOException e = exception.get();
if (e != null) {
throw e;
}
} catch (final InterruptedException e) {
throw new IOException("Interrupted while waiting for synchronised closure");
} finally {
stream.close();
}
}
public void fail(final IOException e) {
exception.set(Preconditions.checkNotNull(e));
}
public void countDown() {complete.countDown();}
}
关于java - PipedInputStream 和 PipedOutputStream 中的异常传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33686430/
我有一个与 PipedInputStream 相关的问题和 PipedOutputStream ,也不知道是我对这些类的设计有误解,还是PipedInputStream.java中的java代码有bu
我正在使用java管道将数据(outstream)从解压模块(JavaUncompress类)传递到解析模块(处理类),文件很大,我想先解压文件并直接解析而不是保存解压缩的文件,然后解析。但是,它仅适
我正在学习 java PipedInputStream/PipeOutputStream 。 我想读取标准输入(下面的“Source”类)并将其重定向到一个进程(此处为“grep A”),Grep 的
我在 SO 上看到了两个答案,它们声称 Java 提供的 PipedInputStream 和 PipedOutputStream 类存在缺陷。但他们没有详细说明他们出了什么问题。他们真的有缺陷吗?如
我正在尝试编写一段解锁代码来读取 PipedInputStream .它基本上会在调用阻塞读取 API 之前检查是否有任何内容要读取: int n = 0; if ((n = pipedInputSt
PipedInputStream 如何从 PipedOutputStream 读取数据?是使用toString()还是有一些隐藏的魔法来访问private成员变量? 最佳答案 PipedOutputS
我正在编写一个程序,该程序通过使用 Class 和 Method 类调用 main 方法来执行另一个 Java 程序。然后,另一个程序尝试从 System.in 读取。为了将参数传递给程序,我将 Sy
什么是管道流的用例?为什么不将数据读入缓冲区然后将它们写出呢? 最佳答案 BlockingQueue 或类似的集合可能会更好地为您服务,它们是线程安全的、健壮的并且扩展性更好。 关于java - Pi
Write end dead exception在以下情况发生:两个线程: A: PipedOutputStream put = new PipedOutputStream(); String
我正在编写一个简单的 Swing GUI,其中包含一个用于打印调试消息和异常的文本字段。我目前在写入 PipedOutputStream 的地方设置了它,并且我有一个守护线程,它从连接的 PipedI
我有一个需要读取的 OutputStream,因此我使用以下 (Groovy) 代码来获取对数据的 InputStream 引用: PipedInputStream inputStream = new
我有一个数据生成器,它在单独的线程中运行并将生成的数据推送到连接到 PipedInputStream 的 PipedOutputStream 中。此输入流的引用通过公共(public) API 公开,
PipedOutputStream的Android实现 write(byte[] buffer, int offset, int count) 是根据write(byte oneByte)实现的。更具
我有一个具有单读者线程模型的多写入线程。ThreadMultipleDateReceiver 类设计用于从多个线程读取。 public class ThreadMultipleDateReceiver
我在 Scala 中有以下代码: val pos = new PipedOutputStream() val pis = new PipedInputStream(pos)
如何正确完成管道输出端的工作?我需要写入线程终止或做一些其他工作,而读取线程读取所有写入数据直到结束。 我应该在写入端关闭管道还是什么? 更新 1 我想澄清一下......根据给定的答案,我认为设计管
我想使用 PipedOutputStream 和 PipedInputStream Java 类编写类似于Producer Consumer Problem 的示例。 注意:这是应用其原理的小示例。
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭10
我不确定,但我很确定我在 Oracle Java 实现(1.7.0_67 和 1.8.0_31 我可以确认受影响)中发现了一个错误(或未记录的功能)。 症状 当管道已满时,写入管道的时间可能比管道再次
我最近发现了这个成语,我想知道我是否遗漏了什么。我从未见过它使用过。我在野外使用过的几乎所有 Java 代码都倾向于将数据放入字符串或缓冲区中,而不是像这个示例(例如使用 HttpClient 和 X
我是一名优秀的程序员,十分优秀!