Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package no.hild1;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.util.concurrent.LinkedBlockingQueue;
- public class mainClass {
- /**
- * @param args
- * @throws IOException
- * @throws InterruptedException
- */
- public static void main(String[] args) throws IOException, InterruptedException {
- testCat();
- }
- public static void testCat() throws IOException, InterruptedException {
- //Runtime rt = Runtime.getRuntime();
- String cmd ="tee ouput.log";
- cmd = "cat";
- cmd ="cs2cs -f '%.13f' +proj=tmerc +lat_0=58 +lon_0=10.722917 +k=1 +x_0=0 +y_0=0 +a=6377492.018 +b=6356173.508712696 +units=m +towgs84=278.3,93,474.5,7.889,0.05,-6.61,6.21 +no_defs +to +proj=latlong +datum=WGS84";
- //Process tee = rt.exec(cmd);
- Process tee = new ProcessBuilder(cmd.split(" ")).start();
- InputWrapper teeStderr = new InputWrapper(tee.getErrorStream(), "STDERR");
- InputWrapper teeStdout = new InputWrapper(tee.getInputStream(), "STDOUT");
- OutputWrapper teeStdinn = new OutputWrapper(tee.getOutputStream(), "STDINN");
- Thread stdinnThread = new Thread(teeStdinn);
- Thread stderrThread = new Thread(teeStderr);
- Thread stdoutThread = new Thread(teeStdout);
- stdinnThread.start();
- stderrThread.start();
- stdoutThread.start();
- synchronized (stdinnThread) {
- teeStdinn.add("100 111");
- teeStdinn.add("200 222");
- teeStdinn.add("300 333");
- teeStdinn.add("400 444");
- }
- while(true) {
- Thread.yield();
- Thread.sleep(100);
- }
- }
- }
- class InputWrapper implements Runnable {
- InputStream inputStream;
- String tag;
- BufferedReader bufferedReader;
- /**
- * @param inputStream
- */
- public InputWrapper(InputStream inputStream, String tag) {
- this.inputStream = inputStream;
- this.tag = tag;
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
- bufferedReader = new BufferedReader(inputStreamReader);
- }
- @Override
- public void run() {
- try {
- String line = null;
- while((line = bufferedReader.readLine()) != null) {
- System.out.println(tag + " read : " + line);
- System.out.flush();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- class OutputWrapper implements Runnable {
- OutputStream outputStream;
- String tag;
- PrintWriter printWriter;
- BufferedWriter bufferedWriter;
- LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<String>();
- /**
- * @param outputStream
- */
- public OutputWrapper(OutputStream outputStream, String tag) {
- this.outputStream = outputStream;
- this.tag = tag;
- bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
- printWriter = new PrintWriter(bufferedWriter);
- }
- @Override
- public void run() {
- try {
- while(true) {
- String line = lbq.take();
- printWriter.println(line);
- printWriter.flush();
- System.out.println(tag + " wrote : " + line);
- System.out.flush();
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void add(String line) {
- synchronized (lbq) {
- System.out.println(tag + " added : " + line + " to queue");
- System.out.flush();
- lbq.add(line);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment