001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.quercus.lib.file;
031:
032: import com.caucho.quercus.env.Env;
033: import com.caucho.quercus.env.EnvCleanup;
034: import com.caucho.vfs.VfsStream;
035: import com.caucho.vfs.WriteStream;
036:
037: import java.io.IOException;
038: import java.io.OutputStream;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: * Represents an output stream for a popen'ed process.
044: */
045: public class PopenOutput extends AbstractBinaryOutput implements
046: EnvCleanup {
047: private static final Logger log = Logger
048: .getLogger(PopenOutput.class.getName());
049:
050: private Env _env;
051: private Process _process;
052: private WriteStream _os;
053:
054: public PopenOutput(Env env, Process process) throws IOException {
055: _env = env;
056:
057: _env.addCleanup(this );
058:
059: _process = process;
060:
061: _os = new WriteStream(new VfsStream(null, _process
062: .getOutputStream()));
063:
064: _process.getInputStream().close();
065: }
066:
067: /**
068: * Returns the write stream.
069: */
070: public OutputStream getOutputStream() {
071: return _os;
072: }
073:
074: /**
075: * Prints a string to a file.
076: */
077: public void print(char v) throws IOException {
078: if (_os != null)
079: _os.print(v);
080: }
081:
082: /**
083: * Prints a string to a file.
084: */
085: public void print(String v) throws IOException {
086: if (_os != null)
087: _os.print(v);
088: }
089:
090: /**
091: * Writes a character
092: */
093: public void write(int ch) throws IOException {
094: if (_os != null)
095: _os.write(ch);
096: }
097:
098: /**
099: * Writes a buffer to a file.
100: */
101: public void write(byte[] buffer, int offset, int length)
102: throws IOException {
103: if (_os != null)
104: _os.write(buffer, offset, length);
105: }
106:
107: /**
108: * Flushes the output.
109: */
110: public void flush() {
111: try {
112: if (_os != null)
113: _os.flush();
114: } catch (IOException e) {
115: log.log(Level.FINE, e.toString(), e);
116: }
117: }
118:
119: /**
120: * Closes the file.
121: */
122: public void closeWrite() {
123: close();
124: }
125:
126: public int pclose() {
127: try {
128: WriteStream os = _os;
129: _os = null;
130:
131: if (os != null)
132: os.close();
133:
134: return _process.waitFor();
135: } catch (Exception e) {
136: log.log(Level.FINE, e.toString(), e);
137:
138: return -1;
139: } finally {
140: _env.removeCleanup(this );
141: }
142: }
143:
144: /**
145: * Closes the file.
146: */
147: public void close() {
148: pclose();
149: }
150:
151: /**
152: * Implements the EnvCleanup interface.
153: */
154:
155: public void cleanup() {
156: pclose();
157: }
158:
159: /**
160: * Converts to a string.
161: * @param env
162: */
163: public String toString() {
164: return "PopenOutput[" + _process + "]";
165: }
166: }
|