KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > cmd > Spool


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/commandline/src/java/org/apache/webdav/cmd/Spool.java,v 1.3 2004/07/28 09:30:33 ib Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/07/28 09:30:33 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.cmd;
25
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31
32 /**
33  * This class provides wrappers around an InputStream and
34  * an OutputStream allowing you to spool both streams to a
35  * shared OutputStream.
36  *
37  */

38 public class Spool
39 {
40     private InputStream JavaDoc in = System.in;
41     private OutputStream JavaDoc out = System.out;
42     private OutputStream JavaDoc spool= null;
43
44     private InputStream JavaDoc wrappedInputStream;
45     private OutputStream JavaDoc wrappedOutputStream;
46
47     private boolean echo=false;
48
49     /**
50      * Create a spool for these two streams
51      */

52     public Spool(InputStream JavaDoc in, OutputStream JavaDoc out)
53     {
54         this();
55         this.in=in;
56         this.out=out;
57     }
58
59     /**
60      * Create a spool for System.in and System.out
61      */

62     public Spool()
63     {
64         wrappedInputStream = new InputStream JavaDoc()
65         {
66             public int read(byte b[]) throws IOException JavaDoc {
67                 int bytesRead = in.read(b);
68                 if (echo)
69                     out.write(b,0,bytesRead);
70                 if (spool!=null)
71                     spool.write(b,0,bytesRead);
72                 return bytesRead;
73             }
74             public int read(byte b[], int off, int len) throws IOException JavaDoc {
75                 int bytesRead = in.read(b,off,len);
76                 if (echo)
77                     out.write(b,off,bytesRead);
78                 if (spool!=null)
79                     spool.write(b,off,bytesRead);
80                 return bytesRead;
81             }
82             public int read() throws IOException JavaDoc {
83                 int nextByte = in.read();
84                 if ((nextByte!=-1) && (echo))
85                     out.write(nextByte);
86                 if ((nextByte!=-1) && (spool!=null))
87                     spool.write(nextByte);
88                 return nextByte;
89             }
90             public long skip(long n) throws IOException JavaDoc {
91                 return in.skip(n);
92             }
93             public int available() throws IOException JavaDoc {
94                 return in.available();
95             }
96             public void close() throws IOException JavaDoc {
97                 in.close();
98             }
99             public synchronized void mark(int readlimit) {
100                 in.mark(readlimit);
101             }
102             public synchronized void reset() throws IOException JavaDoc {
103                 in.reset();
104             }
105             public boolean markSupported() {
106                 return in.markSupported();
107             }
108         };
109         wrappedOutputStream = new OutputStream JavaDoc()
110         {
111             public void write(int b) throws IOException JavaDoc {
112                 out.write(b);
113                 if (spool!=null)
114                     spool.write(b);
115             }
116             public void write(byte b[]) throws IOException JavaDoc {
117                 out.write(b);
118                 if (spool!=null)
119                     spool.write(b);
120             }
121             public void write(byte b[], int off, int len) throws IOException JavaDoc {
122                 out.write(b,off,len);
123                 if (spool!=null)
124                     spool.write(b,off,len);
125             }
126             public void flush() throws IOException JavaDoc {
127                 out.flush();
128                 if (spool!=null)
129                     spool.flush();
130             }
131             public void close() throws IOException JavaDoc {
132                 out.close();
133             }
134         };
135     }
136
137     /**
138      * Echo the input stream to the output stream.
139      */

140     public void setEcho(boolean isEnabled)
141     {
142         this.echo=isEnabled;
143     }
144
145     /**
146      * Enable spooling and spool to the given filename.
147      */

148     public void enable(String JavaDoc filename) throws FileNotFoundException JavaDoc
149     {
150         enable(new FileOutputStream JavaDoc(filename));
151     }
152
153     /**
154      * Enable Spooling and spool to the given OutputStream.
155      */

156     public void enable(OutputStream JavaDoc spool)
157     {
158         if (isEnabled())
159             disable();
160         this.spool = spool;
161     }
162
163     /**
164      * Disable spooling
165      */

166     public void disable()
167     {
168         try
169         {
170             if (spool!=null)
171                 spool.close();
172         }
173         catch (IOException JavaDoc ex)
174         {
175         }
176         spool=null;
177     }
178
179     /**
180      * Returns if spooling is enabled.
181      */

182     public boolean isEnabled()
183     {
184         return (spool!=null);
185     }
186
187     /**
188      * Returns an InputStream wrapped around the real InputStream.
189      * Use this stream to read from.
190      */

191     public InputStream JavaDoc getInputStream()
192     {
193         return wrappedInputStream;
194     }
195
196     /**
197      * Returns an OutputStream wrapped around the real OutputStream.
198      * Use this stream to write to.
199      */

200     public OutputStream JavaDoc getOutputStream()
201     {
202         return wrappedOutputStream;
203     }
204 }
205
Popular Tags