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;
031:
032: import com.caucho.quercus.env.Env;
033: import com.caucho.quercus.env.EnvCleanup;
034:
035: import com.caucho.quercus.lib.file.AbstractBinaryOutput;
036: import com.caucho.quercus.lib.file.PopenOutput;
037: import com.caucho.vfs.VfsStream;
038: import com.caucho.vfs.WriteStream;
039:
040: import java.io.IOException;
041: import java.io.OutputStream;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * Represents an output stream for a proc_open process.
047: */
048: public class ProcOpenOutput extends AbstractBinaryOutput implements
049: EnvCleanup {
050: private static final Logger log = Logger
051: .getLogger(PopenOutput.class.getName());
052:
053: private Env _env;
054: private WriteStream _os;
055:
056: public ProcOpenOutput(Env env, OutputStream out) throws IOException {
057: _env = env;
058:
059: _env.addCleanup(this );
060:
061: _os = new WriteStream(new VfsStream(null, out));
062: }
063:
064: /**
065: * Returns the write stream.
066: */
067: public OutputStream getOutputStream() {
068: return _os;
069: }
070:
071: /**
072: * Prints a string to a file.
073: */
074: public void print(char v) throws IOException {
075: if (_os != null)
076: _os.print(v);
077: }
078:
079: /**
080: * Prints a string to a file.
081: */
082: public void print(String v) throws IOException {
083: if (_os != null)
084: _os.print(v);
085: }
086:
087: /**
088: * Writes a character
089: */
090: public void write(int ch) throws IOException {
091: if (_os != null)
092: _os.write(ch);
093: }
094:
095: /**
096: * Writes a buffer to a file.
097: */
098: public void write(byte[] buffer, int offset, int length)
099: throws IOException {
100: if (_os != null)
101: _os.write(buffer, offset, length);
102: }
103:
104: /**
105: * Flushes the output.
106: */
107: public void flush() {
108: try {
109: if (_os != null)
110: _os.flush();
111: } catch (IOException e) {
112: log.log(Level.FINE, e.toString(), e);
113: }
114: }
115:
116: /**
117: * Closes the file.
118: */
119: public void closeWrite() {
120: close();
121: }
122:
123: /**
124: * Closes the file.
125: */
126: public void close() {
127: _env.removeCleanup(this );
128:
129: cleanup();
130: }
131:
132: /**
133: * Implements the EnvCleanup interface.
134: */
135: public void cleanup() {
136: try {
137: WriteStream os = _os;
138: _os = null;
139:
140: if (os != null)
141: os.close();
142: } catch (IOException e) {
143: log.log(Level.FINE, e.toString(), e);
144: }
145: }
146:
147: /**
148: * Converts to a string.
149: * @param env
150: */
151: public String toString() {
152: return "ProcOpenOutput[pipe]";
153: }
154: }
|