KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dfs > StreamPipe


1 package com.knowgate.dfs;
2
3 //-< Pipe.java >-----------------------------------------------------*--------*
4
// JSYNC Version 1.04 (c) 1998 GARRET * ? *
5
// (Java synchronization classes) * /\| *
6
// * / \ *
7
// Created: 20-Jun-98 K.A. Knizhnik * / [] \ *
8
// Last update: 6-Jul-98 K.A. Knizhnik * GARRET *
9
// http://www.garret.ru/~knizhnik/java.html *
10
//-------------------------------------------------------------------*--------*
11
// Link two existed input and output threads.
12
//-------------------------------------------------------------------*--------*
13

14 import java.io.InputStream JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.BufferedInputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20
21 import com.knowgate.debug.DebugFile;
22
23 /** This class links input and output streams so that data taken from input
24  * stream is transfered to the output stream. This class can be used to
25  * connect standard input/ouput stream of Java application with
26  * output/input streams of spawned child process, so that all user's input is
27  * redirected to the child and all it's output is visible for the user.<P>
28  *
29  * This class starts a thread, which transfers data from input stream to
30  * output stream until End Of File is reached or IOException caused by IO
31  * error is catched.
32  * @author Konstantin Knizhnik
33  * @version 1.04
34  */

35 public class StreamPipe {
36     /** Default size of buffer used to transfer data from the input
37      * stream to the output stream.
38      */

39     private static final int defaultBufferSize = 8000;
40     boolean bSynchronous;
41     /**
42      * <b>Create synchronous stream conector.</b>
43      * between() methods do not return until end of input stream is reached and
44      * written into ouput stream.
45      */

46     public StreamPipe () {
47       bSynchronous = true;
48     }
49
50     /**
51      * <b>Create synchronous or asynchronous stream conector.</b>
52      * between() methods do not return until end of input stream is reached and
53      * written into ouput stream.
54      */

55     public StreamPipe (boolean bSync) {
56       bSynchronous = bSync;
57     }
58
59     // -------------------------------------------------------------------------
60

61     private static void pipe (InputStream JavaDoc in, OutputStream JavaDoc out, int bufferSize, boolean autoFlush)
62       throws IOException JavaDoc {
63
64       if (DebugFile.trace) {
65         DebugFile.writeln("Begin StreamPipe.pipe()");
66       }
67
68       int total = 0;
69       byte[] buffer = new byte[bufferSize];
70
71           int length;
72           while ((length = in.read(buffer)) > 0) {
73             if (DebugFile.trace) DebugFile.writeln("OutputStream.write(byte[], 0, " + String.valueOf(length)+")");
74               out.write(buffer, 0, length);
75               if (autoFlush) out.flush();
76               total += length;
77           }
78
79       if (DebugFile.trace) {
80         DebugFile.writeln("End StreamPipe.pipe() : " + String.valueOf(total));
81       }
82     } // pipe
83

84     // -------------------------------------------------------------------------
85

86     /** Establish connection between input and output streams with specified size of buffer used for data transfer.
87      * @param in input stream
88      * @param out output stream
89      * @param bufferSize size of buffer used to transfer data from the input stream to the output stream
90      * @param autoFlush if set to <b>true</b> OutputStream.flush() method will be called each time bufferSize bytes are written into output stream
91      */

92     public void between(InputStream JavaDoc in,OutputStream JavaDoc out, int bufferSize, boolean autoFlush)
93       throws IOException JavaDoc
94     {
95       if (bSynchronous)
96         pipe(in, out, bufferSize, autoFlush);
97       else
98         (new PipeThread(in, out, bufferSize, autoFlush)).start();
99     }
100
101     /** Establish connection between input and output streams with specified size of buffer used for data transfer and no auto-flush.
102      * @param in input stream
103      * @param out output stream
104      */

105     public void between(InputStream JavaDoc in, OutputStream JavaDoc out, int bufferSize)
106       throws IOException JavaDoc
107     {
108       if (bSynchronous)
109         pipe(in, out, bufferSize, false);
110       else
111         (new PipeThread(in, out, bufferSize, false)).start();
112     }
113
114     /** Establish connection between input and output streams with default buffer size and no auto-flush.
115      * @param in input stream
116      * @param out output stream
117      */

118     public void between(InputStream JavaDoc in, OutputStream JavaDoc out)
119       throws IOException JavaDoc {
120
121       if (bSynchronous)
122         pipe(in, out, defaultBufferSize, false);
123       else
124         (new PipeThread(in, out, defaultBufferSize, false)).start();
125     }
126
127     // -------------------------------------------------------------------------
128

129     /** Establish synchronous connection between a file and an output stream
130      * with specified size of buffer used for data transfer.
131      * autoFlush is set to <b>false</b> and buffer size is set to 8000 bytes.
132      * @param sFilePath input stream
133      * @param oOutStrm output stream
134      * @since 3.0
135      */

136     public static void between(String JavaDoc sFilePath, OutputStream JavaDoc oOutStrm)
137       throws IOException JavaDoc,FileNotFoundException JavaDoc {
138
139       FileInputStream JavaDoc oFileIoStrm = new FileInputStream JavaDoc(sFilePath);
140       BufferedInputStream JavaDoc oBfIoStrm = new BufferedInputStream JavaDoc(oFileIoStrm, defaultBufferSize);
141       pipe(oBfIoStrm, oOutStrm, 8000, false);
142       oBfIoStrm.close();
143       oFileIoStrm.close();
144     } // between
145
}
146
147   // ---------------------------------------------------------------------------
148

149 final class PipeThread extends Thread JavaDoc {
150     InputStream JavaDoc in;
151     OutputStream JavaDoc out;
152     byte[] buffer;
153     boolean flush;
154
155     PipeThread(InputStream JavaDoc in, OutputStream JavaDoc out, int bufferSize, boolean autoFlush) {
156         this.in = in;
157         this.out = out;
158         buffer = new byte[bufferSize];
159         flush = autoFlush;
160
161     }
162
163     public void run() {
164         if (DebugFile.trace) {
165           DebugFile.writeln("Begin StreamPipe.PipeThread.run()");
166         }
167
168         int total = 0;
169
170         try {
171             int length;
172             while ((length = in.read(buffer)) > 0) {
173               if (DebugFile.trace) DebugFile.writeln("OutputStream.write(byte[], 0, " + String.valueOf(length)+")");
174                 out.write(buffer, 0, length);
175                 if (flush) out.flush();
176                 total += length;
177             }
178
179         } catch(IOException JavaDoc ex) { }
180
181         if (DebugFile.trace) {
182           DebugFile.writeln("End StreamPipe.PipeThread.run() : " + String.valueOf(total));
183         }
184     }
185 }
186
Popular Tags