1 20 21 22 package cpmake; 23 24 import java.io.*; 25 26 class StreamPipe extends Thread 27 { 28 BufferedReader m_input; 29 BufferedWriter m_output; 30 BufferedWriter m_file; 31 boolean m_activeThread; 32 boolean m_multiThreaded; 33 34 public StreamPipe(InputStream input, OutputStream output, File file, boolean multiThreaded) 35 throws IOException 36 { 37 m_multiThreaded = multiThreaded; 38 m_input = new BufferedReader(new InputStreamReader(input)); 39 m_output = new BufferedWriter(new OutputStreamWriter(output)); 40 if (file != null) 41 m_file = new BufferedWriter(new FileWriter(file, true)); 42 else 43 m_file = null; 44 m_activeThread = true; 45 start(); 46 } 47 48 public StreamPipe(InputStream input, OutputStream output, boolean multiThreaded) 49 throws IOException 50 { 51 this(input, output, null, multiThreaded); 52 } 53 54 public synchronized void run() 55 { 56 int data; 57 String line; 58 int ch; 59 60 try 62 { 63 if (m_multiThreaded) 64 { 65 while ((line = m_input.readLine()) != null) 66 { 67 m_output.write(line, 0, line.length()); 68 m_output.newLine(); 69 m_output.flush(); 70 if (m_file != null) 71 { 72 m_file.write(line, 0, line.length()); 73 m_file.newLine(); 74 m_file.flush(); 75 } 76 } 77 } 78 else 79 { 80 while ((ch = m_input.read()) != -1) 81 { 82 m_output.write(ch); 83 m_output.flush(); 84 if (m_file != null) 85 { 86 m_file.write(ch); 87 } 88 } 89 } 90 } 91 catch(IOException ioe) 92 { 93 ioe.printStackTrace(); 94 } 95 96 try 97 { 98 if (m_file != null) 101 m_file.close(); 102 } 103 catch(IOException ioe){} 104 m_activeThread = false; 106 notifyAll(); 107 } 108 109 public synchronized void waitForClose() 110 { 111 while (m_activeThread) 112 { 113 try 114 { 115 wait(); 116 } 117 catch (InterruptedException ie) {} 118 } 119 } 120 121 } | Popular Tags |