KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > StreamPipe


1 /*
2  * Copyright (c) 2004, Brian Hawkins
3  * brianhks@activeclickweb.com
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20  
21
22 package cpmake;
23
24 import java.io.*;
25
26 class StreamPipe extends Thread JavaDoc
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 JavaDoc line;
58         int ch;
59         
60         //System.out.println("Starting pipe");
61
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             //Do not close the input and output streams as this can have
99
//Bad effects if one of them is System.in or System.out
100
if (m_file != null)
101                 m_file.close();
102             }
103         catch(IOException ioe){}
104         //System.out.println("Pipe dead");
105
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 JavaDoc ie) {}
118             }
119         }
120         
121     }
Popular Tags