KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.vfs.WriteStream;
37
38 import java.io.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.OutputStream JavaDoc;
41 import java.io.RandomAccessFile JavaDoc;
42 import java.nio.channels.FileChannel JavaDoc;
43 import java.nio.channels.FileLock JavaDoc;
44 import java.nio.channels.OverlappingFileLockException JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Represents a PHP open file
50  */

51 public class FileOutput extends AbstractBinaryOutput implements LockableStream {
52   private static final Logger JavaDoc log
53     = Logger.getLogger(FileOutput.class.getName());
54
55   private Env _env;
56   private Path _path;
57   private WriteStream _os;
58   private long _offset;
59   private FileLock JavaDoc _fileLock;
60   private FileChannel JavaDoc _fileChannel;
61
62   public FileOutput(Env env, Path path)
63     throws IOException JavaDoc
64   {
65     this(env, path, false);
66   }
67
68   public FileOutput(Env env, Path path, boolean isAppend)
69     throws IOException JavaDoc
70   {
71     _env = env;
72     
73     env.addClose(this);
74     
75     _path = path;
76
77     if (isAppend)
78       _os = path.openAppend();
79     else
80       _os = path.openWrite();
81   }
82
83   /**
84    * Returns the write stream.
85    */

86   public OutputStream JavaDoc getOutputStream()
87   {
88     return _os;
89   }
90
91   /**
92    * Returns the file's path.
93    */

94   public Path getPath()
95   {
96     return _path;
97   }
98
99   /**
100    * Prints a string to a file.
101    */

102   public void print(char v)
103     throws IOException JavaDoc
104   {
105     if (_os != null)
106       _os.print(v);
107   }
108
109   /**
110    * Prints a string to a file.
111    */

112   public void print(String JavaDoc v)
113     throws IOException JavaDoc
114   {
115     if (_os != null)
116       _os.print(v);
117   }
118
119   /**
120    * Writes a character
121    */

122   public void write(int ch)
123     throws IOException JavaDoc
124   {
125     if (_os != null)
126       _os.write(ch);
127   }
128
129   /**
130    * Writes a buffer to a file.
131    */

132   public void write(byte []buffer, int offset, int length)
133     throws IOException JavaDoc
134   {
135     if (_os != null)
136       _os.write(buffer, offset, length);
137   }
138
139   /**
140    * Flushes the output.
141    */

142   public void flush()
143     throws IOException JavaDoc
144   {
145     if (_os != null)
146       _os.flush();
147   }
148
149
150   /**
151    * Closes the file.
152    */

153   public void closeWrite()
154   {
155     close();
156   }
157   
158   /**
159    * Closes the file.
160    */

161   public void close()
162   {
163     try {
164       _env.removeClose(this);
165       
166       WriteStream os = _os;
167       _os = null;
168
169       if (os != null)
170         os.close();
171
172       if (_fileLock != null)
173         _fileLock.release();
174
175       if (_fileChannel != null)
176         _fileChannel.close();
177     } catch (IOException JavaDoc e) {
178       log.log(Level.FINE, e.toString(), e);
179     }
180   }
181
182   /**
183    * Lock the shared advisory lock.
184    */

185   public boolean lock(boolean shared, boolean block)
186   {
187     if (! (getPath() instanceof FilePath))
188       return false;
189
190     try {
191       File JavaDoc file = ((FilePath) getPath()).getFile();
192
193       if (_fileChannel == null) {
194         RandomAccessFile JavaDoc randomAccessFile = new RandomAccessFile JavaDoc(file, "rw");
195
196         _fileChannel = randomAccessFile.getChannel();
197       }
198
199       if (block)
200         _fileLock = _fileChannel.lock(0, Long.MAX_VALUE, shared);
201       else
202         _fileLock = _fileChannel.tryLock(0, Long.MAX_VALUE, shared);
203
204       return _fileLock != null;
205     } catch (OverlappingFileLockException JavaDoc e) {
206       return false;
207     } catch (IOException JavaDoc e) {
208       log.log(Level.FINE, e.toString(), e);
209
210       return false;
211     }
212   }
213
214   /**
215    * Unlock the advisory lock.
216    */

217   public boolean unlock()
218   {
219     try {
220       if (_fileLock != null) {
221         _fileLock.release();
222
223         return true;
224       }
225
226       return false;
227     } catch (IOException JavaDoc e) {
228       return false;
229     }
230   }
231
232   public Value stat()
233   {
234     return FileModule.statImpl(_env, getPath());
235   }
236
237   /**
238    * Converts to a string.
239    * @param env
240    */

241   public String JavaDoc toString()
242   {
243     return "FileOutput[" + getPath() + "]";
244   }
245 }
246
247
Popular Tags