1 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 ; 37 import java.io.OutputStream ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 44 public class PopenOutput extends AbstractBinaryOutput { 45 private static final Logger log 46 = Logger.getLogger(PopenOutput.class.getName()); 47 48 private Env _env; 49 private Process _process; 50 private WriteStream _os; 51 52 public PopenOutput(Env env, Process process) 53 throws IOException 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 69 public OutputStream getOutputStream() 70 { 71 return _os; 72 } 73 74 77 public void print(char v) 78 throws IOException 79 { 80 if (_os != null) 81 _os.print(v); 82 } 83 84 87 public void print(String v) 88 throws IOException 89 { 90 if (_os != null) 91 _os.print(v); 92 } 93 94 97 public void write(int ch) 98 throws IOException 99 { 100 if (_os != null) 101 _os.write(ch); 102 } 103 104 107 public void write(byte []buffer, int offset, int length) 108 throws IOException 109 { 110 if (_os != null) 111 _os.write(buffer, offset, length); 112 } 113 114 117 public void flush() 118 { 119 try { 120 if (_os != null) 121 _os.flush(); 122 } catch (IOException e) { 123 log.log(Level.FINE, e.toString(), e); 124 } 125 } 126 127 130 public void closeWrite() 131 { 132 close(); 133 } 134 135 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 e) { 154 log.log(Level.FINE, e.toString(), e); 155 156 return -1; 157 } finally { 158 _env.removeClose(this); 159 } 160 } 161 162 166 public String toString() 167 { 168 return "PopenOutput[" + _process + "]"; 169 } 170 } 171 172 | Popular Tags |