001: /*
002: * Copyright (c) 1998-2007 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 Nam Nguyen
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.BinaryInput;
036: import com.caucho.quercus.lib.file.FileInput;
037: import com.caucho.quercus.lib.file.FileOutput;
038: import com.caucho.quercus.lib.file.ReadStreamInput;
039: import com.caucho.vfs.ReadStream;
040: import com.caucho.vfs.VfsStream;
041:
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046:
047: /**
048: * Represents an input stream for a proc_open process.
049: */
050: public class ProcOpenInput extends ReadStreamInput implements
051: EnvCleanup {
052: private static final Logger log = Logger.getLogger(FileInput.class
053: .getName());
054:
055: private Env _env;
056: private InputStream _in;
057: private FileOutput _out;
058:
059: public ProcOpenInput(Env env, InputStream in) throws IOException {
060: super (env);
061:
062: _env = env;
063: _in = in;
064:
065: env.addCleanup(this );
066:
067: init(new ReadStream(new VfsStream(in, null)));
068: }
069:
070: public ProcOpenInput(Env env, InputStream in, FileOutput out) {
071: super (env);
072:
073: _env = env;
074: _in = in;
075:
076: // Invoke removeCleanup() to ensure that out is not closed
077: // before cleanup() is invoked for this object.
078:
079: env.removeCleanup(out);
080:
081: _out = out;
082:
083: env.addCleanup(this );
084:
085: init(new ReadStream(new VfsStream(in, null)));
086: }
087:
088: /**
089: * Opens a copy.
090: */
091: public BinaryInput openCopy() throws IOException {
092: return new ProcOpenInput(_env, _in, _out);
093: }
094:
095: /**
096: * Returns the number of bytes available to be read, 0 if no known.
097: */
098: public long getLength() {
099: return 0;
100: }
101:
102: /**
103: * Converts to a string.
104: */
105: public String toString() {
106: if (_out != null)
107: return "ProcOpenInput[" + _out + "]";
108: else
109: return "ProcOpenInput[pipe]";
110: }
111:
112: public void close() {
113: _env.removeCleanup(this );
114:
115: cleanup();
116: }
117:
118: /**
119: * Implements the EnvCleanup interface.
120: */
121:
122: public void cleanup() {
123: try {
124: if (_out != null) {
125: int ch;
126: while ((ch = _in.read()) >= 0) {
127: _out.write(ch);
128: }
129:
130: _out.close();
131: }
132:
133: _in.close();
134: } catch (IOException e) {
135: log.log(Level.FINE, e.toString(), e);
136: _env.warning(e);
137: }
138: }
139:
140: }
|