00001 package gov.nasa.arc.ase.jpf; 00002 00003 import java.io.*; 00004 import gov.nasa.arc.ase.util.Debug; 00005 00006 class StreamForwarder implements Runnable { 00007 InputStream input; 00008 OutputStream output; 00009 OutputStream log; 00010 int id; 00011 00012 public StreamForwarder(int n, InputStream i) { 00013 init(n, i, null); 00014 } 00015 public StreamForwarder(int n, InputStream i, OutputStream o) { 00016 init(n, i, o); 00017 } 00018 private void init(int n, InputStream i, OutputStream o) { 00019 id = n; 00020 input = i; 00021 output = o; 00022 try { 00023 log = new FileOutputStream("jpf" + id + ".log"); 00024 } catch(FileNotFoundException e) { 00025 log = null; 00026 Debug.println(Debug.WARNING, "warning: can't write log file -- jpf " + id + ".log"); 00027 } 00028 } 00029 public void run() { 00030 if(output == null) 00031 if(log == null) 00032 return; 00033 else 00034 runFile(); 00035 else 00036 if(log == null) 00037 runScreen(); 00038 else 00039 runFileAndScreen(); 00040 } 00041 public void runFile() { 00042 byte[] bytes = new byte[1024]; 00043 int l; 00044 00045 try { 00046 while((l = input.read(bytes)) > 0) 00047 log.write(bytes, 0, l); 00048 } catch(IOException e) {}; 00049 } 00050 public void runFileAndScreen() { 00051 String ids = "[" + id + "] "; 00052 byte[] bytes = new byte[1024]; 00053 StringBuffer sb = new StringBuffer(ids); 00054 int l; 00055 00056 try { 00057 while((l = input.read(bytes)) > 0) { 00058 String s = new String(bytes, 0, l); 00059 int from = 0; 00060 int to = s.indexOf('\012'); 00061 00062 while(from < l && to != -1) { 00063 sb.append(s.substring(from, to+1)); 00064 output.write(sb.toString().getBytes()); 00065 sb = new StringBuffer(ids); 00066 from = to+1; 00067 to = s.indexOf('\012', from); 00068 } 00069 if(from < l) sb.append(s.substring(from)); 00070 log.write(bytes, 0, l); 00071 } 00072 } catch(IOException e) {}; 00073 } 00074 public void runScreen() { 00075 String ids = "[" + id + "] "; 00076 byte[] bytes = new byte[1024]; 00077 StringBuffer sb = new StringBuffer(ids); 00078 int l; 00079 00080 try { 00081 while((l = input.read(bytes)) > 0) { 00082 String s = new String(bytes, 0, l); 00083 int from = 0; 00084 int to = s.indexOf('\012'); 00085 00086 while(from < l && to != -1) { 00087 sb.append(s.substring(from, to)); 00088 output.write(sb.toString().getBytes()); 00089 sb = new StringBuffer(ids); 00090 from = to+1; 00091 to = s.indexOf('\012', from); 00092 } 00093 if(from < l) sb.append(s.substring(from)); 00094 } 00095 } catch(IOException e) {}; 00096 } 00097 }