KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > file > FileInput


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.file;
31
32 import com.caucho.quercus.env.Env;
33 import com.caucho.quercus.env.Value;
34 import com.caucho.vfs.FilePath;
35 import com.caucho.vfs.Path;
36
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.RandomAccessFile JavaDoc;
40 import java.nio.channels.FileChannel JavaDoc;
41 import java.nio.channels.FileLock JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Represents a Quercus file open for reading
46  */

47 public class FileInput extends ReadStreamInput implements LockableStream {
48   private static final Logger JavaDoc log
49     = Logger.getLogger(FileInput.class.getName());
50
51   private Env _env;
52   private Path _path;
53   private FileLock JavaDoc _fileLock;
54   private FileChannel JavaDoc _fileChannel;
55
56   public FileInput(Env env, Path path)
57     throws IOException JavaDoc
58   {
59     _env = env;
60
61     _path = path;
62
63     init(path.openRead());
64   }
65
66   /**
67    * Returns the path.
68    */

69   public Path getPath()
70   {
71     return _path;
72   }
73
74   /**
75    * Opens a copy.
76    */

77   public BinaryInput openCopy()
78     throws IOException JavaDoc
79   {
80     return new FileInput(_env, _path);
81   }
82
83   /**
84    * Returns the number of bytes available to be read, 0 if no known.
85    */

86   public long getLength()
87   {
88     return getPath().getLength();
89   }
90
91   public long seek(long offset, int whence)
92   {
93     switch (whence) {
94       case BinaryInput.SEEK_CUR:
95         offset = getPosition() + offset;
96         break;
97       case BinaryInput.SEEK_END:
98         offset = getLength() + offset;
99         break;
100       case SEEK_SET:
101       default:
102         break;
103     }
104
105     setPosition(offset);
106
107     return offset;
108   }
109
110   /**
111    * Lock the shared advisory lock.
112    */

113   public boolean lock(boolean shared, boolean block)
114   {
115     if (! (getPath() instanceof FilePath))
116       return false;
117
118     try {
119       File JavaDoc file = ((FilePath) getPath()).getFile();
120
121       if (_fileChannel == null) {
122         RandomAccessFile JavaDoc randomAccessFile = new RandomAccessFile JavaDoc(file, "rw");
123
124         _fileChannel = randomAccessFile.getChannel();
125       }
126
127       if (block)
128         _fileLock = _fileChannel.lock(0, Long.MAX_VALUE, shared);
129       else
130         _fileLock = _fileChannel.tryLock(0, Long.MAX_VALUE, shared);
131
132       return _fileLock != null;
133     } catch (IOException JavaDoc e) {
134       return false;
135     }
136   }
137
138   /**
139    * Unlock the advisory lock.
140    */

141   public boolean unlock()
142   {
143     try {
144       if (_fileLock != null) {
145         _fileLock.release();
146
147         return true;
148       }
149
150       return false;
151     } catch (IOException JavaDoc e) {
152       return false;
153     }
154   }
155
156   public Value stat()
157   {
158     return FileModule.statImpl(_env, getPath());
159   }
160
161   /**
162    * Converts to a string.
163    */

164   public String JavaDoc toString()
165   {
166     return "FileInput[" + getPath() + "]";
167   }
168 }
169
170
Popular Tags