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.vfs;
031:
032: import java.io.FileInputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035:
036: import java.nio.channels.FileChannel;
037: import java.nio.channels.FileLock;
038:
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: * Stream encapsulating FileInputStream
044: */
045: public class FileReadStream extends StreamImpl implements
046: LockableStream {
047: private static final Logger log = Logger
048: .getLogger(FileRandomAccessStream.class.getName());
049:
050: private FileInputStream _is;
051:
052: private FileLock _fileLock;
053: private FileChannel _fileChannel;
054:
055: /**
056: * Create a new FileReadStream.
057: */
058: public FileReadStream() {
059: }
060:
061: /**
062: * Create a new FileReadStream based on the java.io.* stream.
063: *
064: * @param is the underlying input stream.
065: */
066: public FileReadStream(FileInputStream is) {
067: init(is);
068: }
069:
070: /**
071: * Create a new FileReadStream based on the java.io.* stream.
072: *
073: * @param is the underlying input stream.
074: * @param path the associated Path.
075: */
076: public FileReadStream(FileInputStream is, Path path) {
077: init(is);
078: setPath(path);
079: }
080:
081: /**
082: * Initializes a VfsStream with an input/output stream pair. Before a
083: * read, the output will be flushed to avoid deadlocks.
084: *
085: * @param is the underlying InputStream.
086: * @param os the underlying OutputStream.
087: */
088: public void init(FileInputStream is) {
089: _is = is;
090: setPath(null);
091: }
092:
093: /**
094: * Returns true if there's an associated file.
095: */
096: public boolean hasSkip() {
097: return _is != null;
098: }
099:
100: /**
101: * Skips bytes in the file.
102: *
103: * @param n the number of bytes to skip
104: *
105: * @return the actual bytes skipped.
106: */
107: public long skip(long n) throws IOException {
108: if (_is != null)
109: return _is.skip(n);
110: else
111: return -1;
112: }
113:
114: /**
115: * Seeks based on the start.
116: */
117: public void seekStart(long offset) throws IOException {
118: if (_is != null)
119: _is.getChannel().position(offset);
120: }
121:
122: /**
123: * Returns true if there's an associated file.
124: */
125: public boolean canRead() {
126: return _is != null;
127: }
128:
129: /**
130: * Reads bytes from the file.
131: *
132: * @param buf a byte array receiving the data.
133: * @param offset starting index to receive data.
134: * @param length number of bytes to read.
135: *
136: * @return the number of bytes read or -1 on end of file.
137: */
138: public int read(byte[] buf, int offset, int length)
139: throws IOException {
140: if (_is == null)
141: return -1;
142:
143: int len = _is.read(buf, offset, length);
144:
145: return len;
146: }
147:
148: /**
149: * Returns the number of bytes available for reading.
150: */
151: public int getAvailable() throws IOException {
152: if (_is == null)
153: return -1;
154: else {
155: return _is.available();
156: }
157: }
158:
159: /**
160: * Closes the underlying stream.
161: */
162: public void close() throws IOException {
163: unlock();
164:
165: _fileChannel = null;
166:
167: InputStream is = _is;
168: _is = null;
169:
170: if (is != null)
171: is.close();
172: }
173:
174: public boolean lock(boolean shared, boolean block) {
175: unlock();
176:
177: if (!shared) {
178: // Invalid request for an exclusive "write" lock on a read only stream.
179:
180: return false;
181: }
182:
183: try {
184: if (_fileChannel == null) {
185: _fileChannel = _is.getChannel();
186: }
187:
188: if (block)
189: _fileLock = _fileChannel.lock(0, Long.MAX_VALUE, true);
190: else
191: _fileLock = _fileChannel.tryLock(0, Long.MAX_VALUE,
192: true);
193:
194: return _fileLock != null;
195: } catch (IOException e) {
196: log.log(Level.FINE, e.toString(), e);
197: return false;
198: }
199: }
200:
201: public boolean unlock() {
202: try {
203: FileLock lock = _fileLock;
204: _fileLock = null;
205:
206: if (lock != null) {
207: lock.release();
208:
209: return true;
210: }
211:
212: return false;
213: } catch (IOException e) {
214: log.log(Level.FINE, e.toString(), e);
215: return false;
216: }
217: }
218:
219: }
|