KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > file > PopenOutput


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.quercus.lib.file;
31
32 import com.caucho.quercus.env.Env;
33 import com.caucho.vfs.VfsStream;
34 import com.caucho.vfs.WriteStream;
35
36 import java.io.IOException JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * Represents an output stream for a popen'ed process.
43  */

44 public class PopenOutput extends AbstractBinaryOutput {
45   private static final Logger JavaDoc log
46     = Logger.getLogger(PopenOutput.class.getName());
47
48   private Env _env;
49   private Process JavaDoc _process;
50   private WriteStream _os;
51
52   public PopenOutput(Env env, Process JavaDoc process)
53     throws IOException JavaDoc
54   {
55     _env = env;
56
57     _env.addClose(this);
58     
59     _process = process;
60
61     _os = new WriteStream(new VfsStream(null, _process.getOutputStream()));
62
63     _process.getInputStream().close();
64   }
65
66   /**
67    * Returns the write stream.
68    */

69   public OutputStream JavaDoc getOutputStream()
70   {
71     return _os;
72   }
73
74   /**
75    * Prints a string to a file.
76    */

77   public void print(char v)
78     throws IOException JavaDoc
79   {
80     if (_os != null)
81       _os.print(v);
82   }
83
84   /**
85    * Prints a string to a file.
86    */

87   public void print(String JavaDoc v)
88     throws IOException JavaDoc
89   {
90     if (_os != null)
91       _os.print(v);
92   }
93
94   /**
95    * Writes a character
96    */

97   public void write(int ch)
98     throws IOException JavaDoc
99   {
100     if (_os != null)
101       _os.write(ch);
102   }
103
104   /**
105    * Writes a buffer to a file.
106    */

107   public void write(byte []buffer, int offset, int length)
108     throws IOException JavaDoc
109   {
110     if (_os != null)
111       _os.write(buffer, offset, length);
112   }
113
114   /**
115    * Flushes the output.
116    */

117   public void flush()
118   {
119     try {
120       if (_os != null)
121         _os.flush();
122     } catch (IOException JavaDoc e) {
123       log.log(Level.FINE, e.toString(), e);
124     }
125   }
126
127   /**
128    * Closes the file.
129    */

130   public void closeWrite()
131   {
132     close();
133   }
134   
135   /**
136    * Closes the file.
137    */

138   public void close()
139   {
140     pclose();
141   }
142
143   public int pclose()
144   {
145     try {
146       WriteStream os = _os;
147       _os = null;
148
149       if (os != null)
150         os.close();
151
152       return _process.waitFor();
153     } catch (Exception JavaDoc e) {
154       log.log(Level.FINE, e.toString(), e);
155
156       return -1;
157     } finally {
158       _env.removeClose(this);
159     }
160   }
161
162   /**
163    * Converts to a string.
164    * @param env
165    */

166   public String JavaDoc toString()
167   {
168     return "PopenOutput[" + _process + "]";
169   }
170 }
171
172
Popular Tags