- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 java PipedInputStream/PipeOutputStream 。
我想读取标准输入(下面的“Source”类)并将其重定向到一个进程(此处为“grep A”),Grep 的输出将被重定向到 System.out。
为了在 grep 之后使用 stdout 和 stderr,我还创建了一个类 CopyTo 将输入流重定向到输出流。
import java.io.*;
class Test
{
private static class Source
implements Runnable
{
private PipedOutputStream pipedOutputStream=new PipedOutputStream();
private InputStream in;
Source(InputStream in) throws IOException
{
this.in=in;
}
@Override
public void run()
{
try
{
int c;
while((c=this.in.read())!=-1)
{
pipedOutputStream.write(c);
}
pipedOutputStream.flush();
pipedOutputStream.close();
}
catch(Exception err)
{
err.printStackTrace();
}
}
}
private static class Grep
implements Runnable
{
private PipedInputStream pipeInPipedInputStream;
public Grep(Source src) throws IOException
{
this.pipeInPipedInputStream=new PipedInputStream(src.pipedOutputStream);
}
@Override
public void run()
{
try {
Process proc=Runtime.getRuntime().exec(new String[]{
"/bin/grep",
"A"});
OutputStream os=proc.getOutputStream();
Thread t1=new Thread(new CopyTo(proc.getErrorStream(),System.err));
Thread t2=new Thread(new CopyTo(proc.getInputStream(),System.out));
t1.start();
t2.start();
int c;
while((c=this.pipeInPipedInputStream.read())!=-1)
{
os.write((char)c);
}
t1.join();
t2.join();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private static class CopyTo implements Runnable
{
private InputStream in;
private OutputStream out;
CopyTo(InputStream in,OutputStream out)
{
this.in=in;
this.out=out;
}
@Override
public void run() {
try {
int c;
while((c=in.read())!=-1)
{
out.write(c);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
try
{
Source src=new Source(System.in);
Thread t1=new Thread(src);
Thread t2=new Thread(new Grep(src));
t1.start();
t2.start();
}
catch(Exception err)
{
err.printStackTrace();
}
}
}
但是,编译并运行该程序不会产生任何输出(并且该程序被卡住)。
$ javac Test.java && echo -e "A\nT\nG\nC" | java Test
我哪里错了?谢谢。
最佳答案
您只需在代码块之后的 Grep 类的方法 run() 中刷新并关闭操作系统流:
while((c=this.pipeInPipedInputStream.read())!=-1)
{
os.write((char)c);
}
添加此行:
os.flush();
os.close();
Grep 类的 run() 方法必须如下所示:
@Override
public void run()
{
try {
Process proc=Runtime.getRuntime().exec(new String[]{
"/bin/grep",
"A"});
OutputStream os=proc.getOutputStream();
Thread t1=new Thread(new CopyTo(proc.getErrorStream(),System.err));
Thread t2=new Thread(new CopyTo(proc.getInputStream(),System.out));
t1.start();
t2.start();
int c;
while((c=this.pipeInPipedInputStream.read())!=-1)
{
os.write((char)c);
}
//missing lines of code
os.flush();
os.close();
t1.join();
t2.join();
}
catch (Exception e) {
e.printStackTrace();
}
}
命令的输出:
$ javac Test.java && echo -e "A\nT\nG\nC" | java Test
将是:
A
并且程序将终止。
关于java - PipedInputStream、PipedOutputStream 和进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22614263/
我有一个与 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
我是一名优秀的程序员,十分优秀!