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.QuercusModuleException;
033: import com.caucho.quercus.env.BooleanValue;
034: import com.caucho.quercus.env.Value;
035: import com.caucho.vfs.TempBuffer;
036:
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.io.OutputStream;
040:
041: /**
042: * Represents a PHP open file
043: */
044: abstract public class AbstractBinaryOutput extends OutputStream
045: implements BinaryOutput {
046: private int lockedShared = 0;
047: private boolean lockedExclusive = false;
048:
049: /**
050: * Returns self as the output stream.
051: */
052: public OutputStream getOutputStream() {
053: return this ;
054: }
055:
056: /**
057: * Writes to a stream.
058: */
059: public int write(InputStream is, int length) {
060: int writeLength = 0;
061:
062: TempBuffer tb = TempBuffer.allocate();
063: byte[] buffer = tb.getBuffer();
064:
065: try {
066: while (length > 0) {
067: int sublen;
068:
069: if (length < buffer.length)
070: sublen = length;
071: else
072: sublen = buffer.length;
073:
074: sublen = is.read(buffer, 0, sublen);
075:
076: if (sublen < 0)
077: break;
078:
079: write(buffer, 0, sublen);
080:
081: writeLength += sublen;
082: length -= sublen;
083: }
084:
085: return writeLength;
086: } catch (IOException e) {
087: throw new QuercusModuleException(e);
088: } finally {
089: TempBuffer.free(tb);
090: }
091: }
092:
093: /**
094: * Prints a string to a file.
095: */
096: public void print(char v) throws IOException {
097: write((byte) v);
098: }
099:
100: /**
101: * Prints a string to a file.
102: */
103: public void print(String v) throws IOException {
104: for (int i = 0; i < v.length(); i++)
105: write(v.charAt(i));
106: }
107:
108: /**
109: * Flushes the output.
110: */
111: public void flush() throws IOException {
112: }
113:
114: /**
115: * Closes the file.
116: */
117: public void closeWrite() {
118: close();
119: }
120:
121: /**
122: * Closes the stream.
123: */
124: public void close() {
125: }
126:
127: /**
128: * Returns false always for output streams
129: */
130: public boolean isEOF() {
131: return false;
132: }
133:
134: /**
135: * Tells the position in the stream
136: */
137: public long getPosition() {
138: return 0;
139: }
140:
141: /**
142: * Sets the position.
143: */
144: public boolean setPosition(long offset) {
145: return false;
146: }
147:
148: public String getResourceType() {
149: return "stream";
150: }
151:
152: public Value stat() {
153: return BooleanValue.FALSE;
154: }
155: }
|