, / , .
, jtahlborn, , .
, grep .
ls -1 | grep some test somefile.txt someotherfile.txt this_other_file.csv
: "pipe", p1 p2. , , . , .
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
ProcessBuilder pb1 = new ProcessBuilder("ls", "-1");
pb1.directory(new File("test"));
final Process process1 = pb1.start();
final InputStream p1InStream = process1.getInputStream();
final InputStream p1ErrStream = process1.getErrorStream();
Thread t1Err = new InputReaderThread(p1ErrStream, "Process 1 Err");
t1Err.start();
Process process2 = new ProcessBuilder("grep", "some").start();
final InputStream p2InStream = process2.getInputStream();
final InputStream p2ErrStream = process2.getErrorStream();
final OutputStream p2OutStream = process2.getOutputStream();
Thread t2In = new InputReaderThread(p2InStream, "Process 2 Out");
t2In.start();
Thread t2Err = new InputReaderThread(p2ErrStream, "Process 2 Err");
t2Err.start();
new Thread(new PipeClass(p1InStream, p2OutStream, "Process 1 Out")).start();
process2.waitFor();
} catch (IOException e) {
System.out.println("Command execution error: " + e.getMessage());
} catch (Exception e) {
System.out.println("General error: " + e.getMessage());
}
}
}
, . , , .
public class PipeClass implements Runnable {
InputStream is;
OutputStream os;
String isName;
public PipeClass(InputStream is, OutputStream os, String isName) {
this.is = is;
this.os = os;
this.isName = isName;
}
@Override
public void run() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
InputStream clonedIs1 = new ByteArrayInputStream(baos.toByteArray());
Scanner sc = new Scanner(clonedIs1);
while (sc.hasNextLine()) {
System.out.println(this.isName + " >> " + sc.nextLine());
}
InputStream clonedIs2 = new ByteArrayInputStream(baos.toByteArray());
buffer = new byte[512];
while ((bytesRead = clonedIs2.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
is.close();
os.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
, ,
public class InputReaderThread extends Thread {
InputStream is;
String name;
boolean hasData = false;
StringBuilder data = new StringBuilder();
public InputReaderThread(InputStream is, String name) {
this.is = is;
this.name = name;
}
public synchronized void setHasData(boolean hasData) {
this.hasData = hasData;
}
public boolean hasData() { return this.hasData; }
public StringBuilder getData() {
setHasData(false);
StringBuilder returnData = this.data;
this.data = new StringBuilder();
return returnData;
}
@Override
public void run() {
InputStreamReader isr = new InputStreamReader(this.is);
Scanner sc = new Scanner(isr);
while ( sc.hasNextLine() ) {
String line = sc.nextLine();
System.out.println(this.name + " >> " + line);
this.data.append(line + "\n");
}
setHasData(true);
}
}
:
Process 1 Out >> somefile.txt
Process 1 Out >> someotherfile.txt
Process 1 Out >> this_other_file.csv
Process 2 Out >> somefile.txt
Process 2 Out >> someotherfile.txt
, , ps -a | grep usr :
Process 1 Out >> PID PPID PGID WINPID TTY UID STIME COMMAND
Process 1 Out >> I 15016 1 15016 15016 con 400 13:45:59 /usr/bin/grep
Process 1 Out >> 15156 1 15156 15156 con 400 14:21:54 /usr/bin/ps
Process 1 Out >> I 9784 1 9784 9784 con 400 14:21:54 /usr/bin/grep
Process 2 Out >> I 15016 1 15016 15016 con 400 13:45:59 /usr/bin/grep
Process 2 Out >> 15156 1 15156 15156 con 400 14:21:54 /usr/bin/ps
Process 2 Out >> I 9784 1 9784 9784 con 400 14:21:54 /usr/bin/grep
grep 2, , , , , .
, , .
, , , .