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.vfs.Path;
033: import com.caucho.vfs.WriteStream;
034:
035: import java.io.IOException;
036: import java.util.logging.Level;
037: import java.util.logging.Logger;
038:
039: /**
040: * Represents a PHP open file
041: */
042: public class FileWriteValue extends FileValue {
043: private static final Logger log = Logger
044: .getLogger(FileReadValue.class.getName());
045:
046: private WriteStream _os;
047: private long _offset;
048:
049: public FileWriteValue(Path path) throws IOException {
050: super (path);
051:
052: _os = path.openWrite();
053: }
054:
055: public FileWriteValue(Path path, boolean isAppend)
056: throws IOException {
057: super (path);
058:
059: if (isAppend)
060: _os = path.openAppend();
061: else
062: _os = path.openWrite();
063: }
064:
065: /**
066: * Prints a string to a file.
067: */
068: public void print(char v) throws IOException {
069: if (_os != null)
070: _os.print(v);
071: }
072:
073: /**
074: * Prints a string to a file.
075: */
076: public void print(String v) throws IOException {
077: if (_os != null)
078: _os.print(v);
079: }
080:
081: /**
082: * Writes a buffer to a file.
083: */
084: public int write(byte[] buffer, int offset, int length)
085: throws IOException {
086: if (_os != null) {
087: _os.write(buffer, offset, length);
088:
089: return length;
090: }
091:
092: return 0;
093: }
094:
095: /**
096: * Flushes the output.
097: */
098: public void flush() {
099: try {
100: if (_os != null)
101: _os.flush();
102: } catch (IOException e) {
103: log.log(Level.FINE, e.toString(), e);
104: }
105: }
106:
107: /**
108: * Closes the file.
109: */
110: public void close() {
111: try {
112: WriteStream os = _os;
113: _os = null;
114:
115: if (os != null)
116: os.close();
117: } catch (IOException e) {
118: log.log(Level.FINE, e.toString(), e);
119: }
120: }
121:
122: /**
123: * Converts to a string.
124: * @param env
125: */
126: public String toString() {
127: return "File[" + getPath() + "]";
128: }
129: }
|