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.ReadStream;
038: import com.caucho.vfs.LockableStream;
039:
040: import java.io.File;
041: import java.io.IOException;
042: import java.util.logging.*;
043:
044: /**
045: * Represents a Quercus file open for reading
046: */
047: public class FileInput extends ReadStreamInput implements
048: LockableStream, EnvCleanup {
049: private static final Logger log = Logger.getLogger(FileInput.class
050: .getName());
051:
052: private Env _env;
053: private Path _path;
054:
055: private ReadStream _is;
056:
057: public FileInput(Env env, Path path) throws IOException {
058: super (env);
059:
060: _env = env;
061:
062: env.addCleanup(this );
063:
064: _path = path;
065:
066: _is = path.openRead();
067:
068: init(_is);
069: }
070:
071: /**
072: * Returns the path.
073: */
074: public Path getPath() {
075: return _path;
076: }
077:
078: /**
079: * Opens a copy.
080: */
081: public BinaryInput openCopy() throws IOException {
082: return new FileInput(_env, _path);
083: }
084:
085: /**
086: * Returns the number of bytes available to be read, 0 if no known.
087: */
088: public long getLength() {
089: return getPath().getLength();
090: }
091:
092: public long seek(long offset, int whence) {
093: long position;
094:
095: switch (whence) {
096: case BinaryInput.SEEK_CUR:
097: position = getPosition() + offset;
098: break;
099: case BinaryInput.SEEK_END:
100: position = getLength() + offset;
101: break;
102: case BinaryInput.SEEK_SET:
103: default:
104: position = offset;
105: break;
106: }
107:
108: if (!setPosition(position))
109: return -1L;
110: else
111: return position;
112: }
113:
114: /**
115: * Lock the shared advisory lock.
116: */
117: public boolean lock(boolean shared, boolean block) {
118: return _is.lock(shared, block);
119: }
120:
121: /**
122: * Unlock the advisory lock.
123: */
124: public boolean unlock() {
125: return _is.unlock();
126: }
127:
128: public Value stat() {
129: return FileModule.statImpl(_env, getPath());
130: }
131:
132: public void close() {
133: _env.removeCleanup(this );
134:
135: cleanup();
136: }
137:
138: /**
139: * Implements the EnvCleanup interface.
140: */
141: public void cleanup() {
142: super .close();
143: }
144:
145: /**
146: * Converts to a string.
147: */
148: public String toString() {
149: return "FileInput[" + getPath() + "]";
150: }
151: }
|