KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > corruptio > CorruptFile


1 /*
2
3    Derby - Class org.apache.derby.impl.io.DirFile
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util.corruptio;
23 import org.apache.derby.io.StorageFile;
24 import org.apache.derby.io.StorageRandomAccessFile;
25 import java.io.File JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.RandomAccessFile JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35
36 /**
37  * This class provides proxy implementation of the StorageFile interface. It is
38  * used by CorruptDiskStorageFactory to instrument the database engine
39  * i/o for testing.
40  *
41  * @see StorageFile
42  */

43 class CorruptFile implements StorageFile {
44
45     private StorageFile realFile = null;
46
47     CorruptFile(StorageFile realFile)
48     {
49         this.realFile = realFile;
50     }
51
52
53     /**
54      * Get the names of all files and sub-directories in the directory named
55      * by this path name.
56      *
57      * This method is only used in a writable database.
58      *
59      * @return An array of the names of the files and directories in this
60      * directory denoted by this abstract pathname. The returned array
61      * will have length 0 if this directory is empty. Returns null if
62      * this StorageFile is not a directory, or if an I/O error occurs.
63      * The return value is undefined if the database is read-only.
64      */

65     public String JavaDoc[] list()
66     {
67         return realFile.list();
68     }
69
70     /**
71      * Determine whether the named file is writable.
72      *
73      * @return <b>true</b> if the file exists and is writable, <b>false</b> if not.
74      */

75     public boolean canWrite()
76     {
77         return realFile.canWrite();
78     }
79
80     /**
81      * Tests whether the named file exists.
82      *
83      * @return <b>true</b> if the named file exists, <b>false</b> if not.
84      */

85     public boolean exists()
86     {
87         return realFile.exists();
88     }
89
90     /**
91      * Tests whether the named file is a directory, or not.
92      * This is only called in writable storage factories.
93      *
94      * @return <b>true</b> if named file exists and is a directory,
95      * <b>false</b> if not.
96      * The return value is undefined if the storage is read-only.
97      */

98     public boolean isDirectory()
99     {
100         return realFile.isDirectory();
101     }
102
103     /**
104      * Deletes the named file or empty directory.
105      * This method does not delete non-empty directories.
106      *
107      * @return <b>true</b> if the named file or directory is successfully
108      * deleted, <b>false</b> if not
109      */

110     public boolean delete()
111     {
112         return realFile.delete();
113     }
114
115     /**
116      * Deletes the named file and, if it is a directory, all the files and
117      * directories it contains.
118      *
119      * @return <b>true</b> if the named file or directory is successfully
120      * deleted, <b>false</b> if not
121      */

122     public boolean deleteAll()
123     {
124         return realFile.deleteAll();
125     }
126     /**
127      * Converts this StorageFile into a pathname string.
128      * The character returned by StorageFactory.getSeparator()
129      * is used to separate the directory and file names in the sequence.
130      *
131      *<p>
132      *<b>The returned path may include the database directory.
133      * Therefore it cannot be directly used to make an StorageFile
134      * equivalent to this one.</b>
135      *
136      * @return The pathname as a string.
137      *
138      * @see StorageFactory#getSeparator
139      */

140     public String JavaDoc getPath()
141     {
142         return realFile.getPath();
143     }
144     /**
145      * Converts this StorageFile into a canonical pathname string.
146      * The form of the canonical path is system dependent.
147      *
148      * @return The pathname as a string.
149      *
150      * @exception IOException if an I/O error occurred while finding the
151      * canonical name
152      */

153     public String JavaDoc getCanonicalPath() throws IOException JavaDoc
154     {
155         return realFile.getCanonicalPath();
156     }
157
158     /**
159      * @return The last segment in the path name, "" if the path name sequence
160      * is empty.
161      */

162     public String JavaDoc getName()
163     {
164         return realFile.getName();
165     }
166
167     /**
168      * If the named file does not already exist then create it as an empty
169      * normal file.
170      *
171      * The implementation must synchronize with other threads accessing the
172      * same file (in the same or a different process).
173      * If two threads both attempt to create a file with the same name
174      * at the same time then at most one should succeed.
175      *
176      * @return <b>true</b> if this thread's invocation of createNewFile
177      * successfully created the named file;
178      * <b>false</b> if not, i.e. <b>false</b> if the named file
179      * already exists or if another concurrent thread created it.
180      *
181      * @exception IOException - If the directory does not exist or some
182      * other I/O error occurred
183      */

184     public boolean createNewFile() throws IOException JavaDoc
185     {
186         return realFile.createNewFile();
187     }
188
189     /**
190      * Rename the file denoted by this name.
191      *
192      * Note that StorageFile objects are immutable. This method renames the
193      * underlying file, it does not change this StorageFile object. The
194      * StorageFile object denotes the same name as before, however the exists()
195      * method will return false after the renameTo method executes successfully.
196      *
197      * <p>
198      * It is not specified whether this method will succeed if a file
199      * already exists under the new name.
200      *
201      * @param newName the new name.
202      *
203      * @return <b>true</b> if the rename succeeded, <b>false</b> if not.
204      */

205     public boolean renameTo( StorageFile newName)
206     {
207         return realFile.renameTo(newName);
208     }
209
210     /**
211      * Creates the named directory.
212      *
213      * @return <b>true</b> if the directory was created; <b>false</b> if not.
214      */

215     public boolean mkdir()
216     {
217         return realFile.mkdir();
218     }
219     /**
220      * Creates the named directory, and all nonexistent parent directories.
221      *
222      * @return <b>true</b> if the directory was created, <b>false</b> if not
223      */

224     public boolean mkdirs()
225     {
226         return realFile.mkdirs();
227     }
228
229
230     /**
231      * Returns the length of the named file if it is not a directory.
232      *
233      * The return value is not specified if the file is a directory.
234      *
235      * @return The length, in bytes, of the named file if it exists and is not
236      * a directory, 0 if the file does not exist, or any value if the
237      * named file is a directory.
238      */

239     public long length()
240     {
241         return realFile.length();
242     }
243
244     /**
245      * Make the named file or directory read-only.
246      *
247      * This interface does not specify whether this also makes the file
248      * undeletable.
249      *
250      * @return <b>true</b> if the named file or directory was made read-only,
251      * or it already was read-only; <b>false</b> if not.
252      */

253     public boolean setReadOnly()
254     {
255         return realFile.setReadOnly();
256     }
257
258     /**
259      * Get the name of the parent directory if this name includes a parent.
260      *
261      * @return An StorageFile denoting the parent directory of this StorageFile,
262      * if it has a parent, null if it does not have a parent.
263      */

264     public StorageFile getParentDir()
265     {
266         return realFile.getParentDir();
267     }
268     
269     /**
270      * Creates an output stream from a file name.
271      *
272      * @param name The file name.
273      *
274      * @return an output stream suitable for writing to the file.
275      *
276      * @exception FileNotFoundException if the file exists but is a directory
277      * rather than a regular file, does not exist but cannot be
278      * created, or cannot be opened for any other reason.
279      */

280     public OutputStream JavaDoc getOutputStream( ) throws FileNotFoundException JavaDoc
281     {
282         return realFile.getOutputStream();
283     }
284     
285     /**
286      * Creates an output stream from a file name.
287      *
288      * @param append If true then data will be appended to the end of the file,
289      * if it already exists.
290      * If false and a normal file already exists with this name
291      * the file will first be truncated to zero length.
292      *
293      * @return an output stream suitable for writing to the file.
294      *
295      * @exception FileNotFoundException if the file exists but is a directory
296      * rather than a regular file, does not
297      * exist but cannot be created, or cannot
298      * be opened for any other reason.
299      */

300     public OutputStream JavaDoc getOutputStream( final boolean append) throws FileNotFoundException JavaDoc
301     {
302         return realFile.getOutputStream(append);
303     }
304
305     /**
306      * Creates an input stream from a file name.
307      *
308      * @param name The file name.
309      *
310      * @return an input stream suitable for reading from the file.
311      *
312      * @exception FileNotFoundException if the file is not found.
313      */

314     public InputStream JavaDoc getInputStream( ) throws FileNotFoundException JavaDoc
315     {
316         return realFile.getInputStream();
317     }
318
319     /**
320      * Get an exclusive lock.
321      *
322      * This is used to ensure that two or more JVMs do not open the same
323      * database at the same time.
324      *
325      * @param lockFile The name of the lock. In a file system implementation it
326      * will be the name of a lock file.
327      *
328      * @return EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE if the lock cannot be
329      * acquired because it is already held.
330      * <br> EXCLUSIVE_FILE_LOCK if the lock was successfully acquired.
331      * <br> NO_FILE_LOCK_SUPPORT if the system does not support
332      * exclusive locks.<br>
333      */

334     public synchronized int getExclusiveFileLock()
335     {
336         return realFile.getExclusiveFileLock();
337
338     } // end of getExclusiveFileLock
339

340     /**
341      * Release the resource associated with an earlier acquired exclusive lock
342      *
343      * @param lockFile the name of the lock file
344      *
345      * @see #getExclusiveFileLock
346      */

347     public synchronized void releaseExclusiveFileLock()
348     {
349         realFile.releaseExclusiveFileLock();
350
351     } // End of releaseExclusiveFileLock
352

353
354     /**
355      * Get a random access (read/write) file.
356      *
357      * @param name The name of the file.
358      * @param mode "r", "rw", "rws", or "rwd". The "rws" and "rwd" modes specify
359      * that the data is to be written to persistent store,
360      * consistent with the java.io.RandomAccessFile class
361      * ("synchronized" with the persistent storage, in the file
362      * system meaning of the word "synchronized"). However
363      * the implementation is not required to implement the "rws" or
364      * "rwd" modes. The implementation may treat "rws" and "rwd" as
365      * "rw". It is up to the user of this interface to call the
366      * StorageRandomAccessFile.sync method. If the "rws" or "rwd"
367      * modes are supported and the RandomAccessFile was opened in
368      * "rws" or "rwd" mode then the implementation of
369      * StorageRandomAccessFile.sync need not do anything.
370      *
371      * @return an object that can be used for random access to the file.
372      *
373      * @exception IllegalArgumentException if the mode argument is not equal to
374      * one of "r", "rw".
375      * @exception FileNotFoundException if the file exists but is a directory
376      * rather than a regular file, or cannot
377      * be opened or created for any other
378      * reason .
379      */

380     public StorageRandomAccessFile getRandomAccessFile( String JavaDoc mode) throws FileNotFoundException JavaDoc
381     {
382         return new CorruptRandomAccessFile(realFile.getRandomAccessFile(mode), (File) realFile);
383     }
384
385     /**
386      * retuns the real file handle that is used to delegate the calls
387      */

388     protected StorageFile getRealFileInstance()
389     {
390         return realFile;
391     }
392     /**
393      * @see org.apache.derby.io.StorageFile#getURL()
394      */

395     public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
396         throw new MalformedURLException JavaDoc(toString());
397     }
398 }
399
Popular Tags