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 Scott Ferguson
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.quercus.env.Value;
035: import com.caucho.vfs.FilePath;
036: import com.caucho.vfs.Path;
037: import com.caucho.vfs.WriteStream;
038: import com.caucho.vfs.LockableStream;
039:
040: import java.io.File;
041: import java.io.IOException;
042: import java.io.OutputStream;
043:
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046:
047: /**
048: * Represents a PHP open file
049: */
050: public class FileOutput extends AbstractBinaryOutput implements
051: LockableStream, EnvCleanup {
052: private static final Logger log = Logger.getLogger(FileOutput.class
053: .getName());
054:
055: private Env _env;
056: private Path _path;
057: private WriteStream _os;
058: private long _offset;
059:
060: public FileOutput(Env env, Path path) throws IOException {
061: this (env, path, false);
062: }
063:
064: public FileOutput(Env env, Path path, boolean isAppend)
065: throws IOException {
066: _env = env;
067:
068: env.addCleanup(this );
069:
070: _path = path;
071:
072: if (isAppend)
073: _os = path.openAppend();
074: else
075: _os = path.openWrite();
076: }
077:
078: /**
079: * Returns the write stream.
080: */
081: public OutputStream getOutputStream() {
082: return _os;
083: }
084:
085: /**
086: * Returns the file's path.
087: */
088: public Path getPath() {
089: return _path;
090: }
091:
092: /**
093: * Prints a string to a file.
094: */
095: public void print(char v) throws IOException {
096: if (_os != null)
097: _os.print(v);
098: }
099:
100: /**
101: * Prints a string to a file.
102: */
103: public void print(String v) throws IOException {
104: if (_os != null)
105: _os.print(v);
106: }
107:
108: /**
109: * Writes a character
110: */
111: public void write(int ch) throws IOException {
112: if (_os != null)
113: _os.write(ch);
114: }
115:
116: /**
117: * Writes a buffer to a file.
118: */
119: public void write(byte[] buffer, int offset, int length)
120: throws IOException {
121: if (_os != null)
122: _os.write(buffer, offset, length);
123: }
124:
125: /**
126: * Flushes the output.
127: */
128: public void flush() throws IOException {
129: if (_os != null)
130: _os.flush();
131: }
132:
133: /**
134: * Closes the file.
135: */
136: public void closeWrite() {
137: close();
138: }
139:
140: /**
141: * Closes the file.
142: */
143: public void close() {
144: _env.removeCleanup(this );
145:
146: cleanup();
147: }
148:
149: /**
150: * Implements the EnvCleanup interface.
151: */
152: public void cleanup() {
153: try {
154: WriteStream os = _os;
155: _os = null;
156:
157: if (os != null)
158: os.close();
159: } catch (IOException e) {
160: log.log(Level.FINE, e.toString(), e);
161: }
162: }
163:
164: /**
165: * Lock the shared advisory lock.
166: */
167: public boolean lock(boolean shared, boolean block) {
168: return _os.lock(shared, block);
169: }
170:
171: /**
172: * Unlock the advisory lock.
173: */
174: public boolean unlock() {
175: return _os.unlock();
176: }
177:
178: public Value stat() {
179: return FileModule.statImpl(_env, getPath());
180: }
181:
182: /**
183: * Converts to a string.
184: * @param env
185: */
186: public String toString() {
187: return "FileOutput[" + getPath() + "]";
188: }
189: }
|