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.StringValue;
033: import com.caucho.quercus.env.UnicodeValueImpl;
034: import com.caucho.vfs.Path;
035: import com.caucho.vfs.ReadStream;
036:
037: import java.io.IOException;
038: import java.io.OutputStream;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: * Represents a Quercus open file
044: */
045: public class FileReadValue extends FileValue {
046: private static final Logger log = Logger
047: .getLogger(FileReadValue.class.getName());
048:
049: private ReadStream _is;
050: private long _offset;
051:
052: public FileReadValue(Path path) throws IOException {
053: super (path);
054:
055: _is = path.openRead();
056: }
057:
058: /**
059: * Returns the number of bytes available to be read, 0 if no known.
060: */
061: public long getLength() {
062: return getPath().getLength();
063: }
064:
065: /**
066: * Reads a character from a file, returning -1 on EOF.
067: */
068: public int read() throws IOException {
069: if (_is != null) {
070: int v = _is.read();
071:
072: if (v >= 0)
073: _offset++;
074: else
075: close();
076:
077: return v;
078: } else
079: return -1;
080: }
081:
082: /**
083: * Reads a buffer from a file, returning -1 on EOF.
084: */
085: public int read(byte[] buffer, int offset, int length)
086: throws IOException {
087: if (_is != null) {
088: int len = _is.read(buffer, offset, length);
089:
090: if (len >= 0)
091: _offset += len;
092: else
093: close();
094:
095: return len;
096: } else
097: return -1;
098: }
099:
100: /**
101: * Reads the optional linefeed character from a \r\n
102: */
103: public boolean readOptionalLinefeed() throws IOException {
104: if (_is != null) {
105: int ch = _is.read();
106:
107: if (ch == '\n') {
108: _offset++;
109: return true;
110: } else {
111: _is.unread();
112: return false;
113: }
114: } else
115: return false;
116: }
117:
118: @Override
119: public void writeToStream(OutputStream os, int length)
120: throws IOException {
121: if (_is != null) {
122: _is.writeToStream(os, length);
123: }
124: }
125:
126: /**
127: * Reads a line from a file, returning null on EOF.
128: */
129: public StringValue readLine() throws IOException {
130: // XXX: offset messed up
131:
132: if (_is != null)
133: return new UnicodeValueImpl(_is.readLineNoChop());
134: else
135: return null;
136: }
137:
138: /**
139: * Returns true on the EOF.
140: */
141: public boolean isEOF() {
142: if (_is == null)
143: return true;
144: else {
145: try {
146: // XXX: not quite right for sockets
147: return _is.available() <= 0;
148: } catch (IOException e) {
149: log.log(Level.FINE, e.toString(), e);
150:
151: return true;
152: }
153: }
154: }
155:
156: /**
157: * Returns the current location in the file.
158: */
159: public long getPosition() {
160: if (_is == null)
161: return -1;
162: else
163: return _is.getPosition();
164: }
165:
166: /**
167: * Closes the file.
168: */
169: public void close() {
170: ReadStream is = _is;
171: _is = null;
172:
173: if (is != null)
174: is.close();
175: }
176:
177: /**
178: * Converts to a string.
179: * @param env
180: */
181: public String toString() {
182: return "File[" + getPath() + "]";
183: }
184:
185: }
|