KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > io > InputStreamFile


1 /*
2
3    Derby - Class org.apache.derby.impl.io.InputStreamFile
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.derby.impl.io;
23
24 import org.apache.derby.io.StorageFactory;
25 import org.apache.derby.io.StorageFile;
26 import org.apache.derby.io.StorageRandomAccessFile;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import java.io.File JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URL JavaDoc;
37
38 /**
39  * This class provides the base for read-only stream implementations of the StorageFile interface. It is used with the
40  * classpath, jar, http, and https subsubprotocols
41  */

42 abstract class InputStreamFile implements StorageFile
43 {
44
45     final String JavaDoc path;
46     final int nameStart; // getName() = path.substring( nameStart)
47
final BaseStorageFactory storageFactory;
48
49     InputStreamFile( BaseStorageFactory storageFactory, String JavaDoc path)
50     {
51         this.storageFactory = storageFactory;
52         if( path == null || path.length() == 0)
53         {
54             this.path = storageFactory.dataDirectory;
55             nameStart = -1;
56         }
57         else
58         {
59             StringBuffer JavaDoc sb = new StringBuffer JavaDoc( storageFactory.separatedDataDirectory);
60             if( File.separatorChar != '/')
61                 sb.append( path.replace( File.separatorChar, '/'));
62             else
63                 sb.append( path);
64             this.path = sb.toString();
65             nameStart = this.path.lastIndexOf( '/') + 1;
66         }
67     }
68
69     InputStreamFile( BaseStorageFactory storageFactory, String JavaDoc parent, String JavaDoc name)
70     {
71         this.storageFactory = storageFactory;
72         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( storageFactory.separatedDataDirectory);
73         if( File.separatorChar != '/')
74         {
75             sb.append( parent.replace( File.separatorChar, '/'));
76             sb.append( '/');
77             sb.append( name.replace( File.separatorChar, '/'));
78         }
79         else
80         {
81             sb.append( parent);
82             sb.append( '/');
83             sb.append( name);
84         }
85         path = sb.toString();
86         nameStart = this.path.lastIndexOf( '/') + 1;
87     }
88
89     InputStreamFile( InputStreamFile dir, String JavaDoc name)
90     {
91         this.storageFactory = dir.storageFactory;
92         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( dir.path);
93         sb.append( '/');
94         if( File.separatorChar != '/')
95             sb.append( name.replace( File.separatorChar, '/'));
96         else
97             sb.append( name);
98         path = sb.toString();
99         nameStart = this.path.lastIndexOf( '/') + 1;
100     }
101
102     InputStreamFile( BaseStorageFactory storageFactory, String JavaDoc child, int pathLen)
103     {
104         this.storageFactory = storageFactory;
105         path = child.substring( 0, pathLen);
106         nameStart = this.path.lastIndexOf( '/') + 1;
107     }
108
109     public boolean equals( Object JavaDoc other)
110     {
111         if( other == null || ! getClass().equals( other.getClass()))
112             return false;
113         InputStreamFile otherFile = (InputStreamFile) other;
114         return path.equals( otherFile.path);
115     }
116
117     public int hashCode()
118     {
119         return path.hashCode();
120     }
121
122     /**
123      * Get the names of all files and sub-directories in the directory named by this path name.
124      *
125      * @return An array of the names of the files and directories in this
126      * directory denoted by this abstract pathname. The returned array will have length 0
127      * if this directory is empty. Returns null if this StorageFile is not a directory, or
128      * if an I/O error occurs.
129      */

130     public String JavaDoc[] list()
131     {
132         return null;
133     }
134
135     /**
136      * Determine whether the named file is writable.
137      *
138      * @return <b>true</b> if the file exists and is writable, <b>false</b> if not.
139      */

140     public boolean canWrite()
141     {
142         return false;
143     }
144
145     /**
146      * Tests whether the named file exists.
147      *
148      * @return <b>true</b> if the named file exists, <b>false</b> if not.
149      */

150     public abstract boolean exists();
151
152     /**
153      * Tests whether the named file is a directory, or not. This is only called in writable storage factories.
154      *
155      * @return <b>true</b> if named file exists and is a directory, <b>false</b> if not.
156      * The return value is undefined if the storage is read-only.
157      */

158     public boolean isDirectory()
159     {
160         return false;
161     }
162
163     /**
164      * Deletes the named file or empty directory. This method does not delete non-empty directories.
165      *
166      * @return <b>true</b> if the named file or directory is successfully deleted, <b>false</b> if not
167      */

168     public boolean delete()
169     {
170         return false;
171     }
172
173     /**
174      * Deletes the named file and, if it is a directory, all the files and directories it contains.
175      *
176      * @return <b>true</b> if the named file or directory is successfully deleted, <b>false</b> if not
177      */

178     public boolean deleteAll()
179     {
180         return false;
181     }
182
183     /**
184      * Converts this StorageFile into a pathname string. The character returned by StorageFactory.getSeparator()
185      * is used to separate the directory and file names in the sequence.
186      *
187      *<p>
188      *<b>The returned path may include the database directory. Therefore it cannot be directly used to make an StorageFile
189      * equivalent to this one.</b>
190      *
191      * @return The pathname as a string.
192      *
193      * @see StorageFactory#getSeparator
194      */

195     public String JavaDoc getPath()
196     {
197         if( File.separatorChar != '/')
198             return path.replace( '/', File.separatorChar);
199         return path;
200     } // end of getPath
201

202     public String JavaDoc getCanonicalPath() throws IOException JavaDoc
203     {
204         return storageFactory.getCanonicalName() + "/" + path;
205     }
206     
207     /**
208      * @return The last segment in the path name, "" if the path name sequence is empty.
209      */

210     public String JavaDoc getName()
211     {
212         return (nameStart < 0) ? "" : path.substring( nameStart);
213     }
214
215     /**
216      * If the named file does not already exist then create it as an empty normal file.
217      *
218      * The implementation
219      * must synchronize with other threads accessing the same file (in the same or a different process).
220      * If two threads both attempt to create a file with the same name
221      * at the same time then at most one should succeed.
222      *
223      * @return <b>true</b> if this thread's invocation of createNewFile successfully created the named file;
224      * <b>false</b> if not, i.e. <b>false</b> if the named file already exists or if another concurrent thread created it.
225      *
226      * @exception IOException - If the directory does not exist or some other I/O error occurred
227      */

228     public boolean createNewFile() throws IOException JavaDoc
229     {
230         throw new IOException JavaDoc( "createNewFile called in a read-only file system.");
231     }
232
233     /**
234      * Rename the file denoted by this name. Note that StorageFile objects are immutable. This method
235      * renames the underlying file, it does not change this StorageFile object. The StorageFile object denotes the
236      * same name as before, however the exists() method will return false after the renameTo method
237      * executes successfully.
238      *
239      *<p>It is not specified whether this method will succeed if a file already exists under the new name.
240      *
241      * @param newName the new name.
242      *
243      * @return <b>true</b> if the rename succeeded, <b>false</b> if not.
244      */

245     public boolean renameTo( StorageFile newName)
246     {
247         return false;
248     }
249     
250     /**
251      * Creates the named directory.
252      *
253      * @return <b>true</b> if the directory was created; <b>false</b> if not.
254      */

255     public boolean mkdir()
256     {
257         return false;
258     }
259
260     /**
261      * Creates the named directory, and all nonexistent parent directories.
262      *
263      * @return <b>true</b> if the directory was created, <b>false</b> if not
264      */

265     public boolean mkdirs()
266     {
267         return false;
268     }
269
270     /**
271      * Returns the length of the named file if it is not a directory. The return value is not specified
272      * if the file is a directory.
273      *
274      * @return The length, in bytes, of the named file if it exists and is not a directory,
275      * 0 if the file does not exist, or any value if the named file is a directory.
276      */

277     public long length()
278     {
279         try
280         {
281             InputStream JavaDoc is = getInputStream();
282             if( is == null)
283                 return 0;
284             long len = is.available();
285             is.close();
286             return len;
287         }
288         catch( IOException JavaDoc e){ return 0;}
289     } // end of length
290

291     /**
292      * Get the name of the parent directory if this name includes a parent.
293      *
294      * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if
295      * it does not have a parent.
296      */

297     public StorageFile getParentDir()
298     {
299         if( path.length() <= storageFactory.separatedDataDirectory.length())
300             return null;
301         return getParentDir( path.lastIndexOf( '/'));
302     }
303
304     /**
305      * Get the parent of this file.
306      *
307      * @param pathLen the length of the parent's path name.
308      */

309     abstract StorageFile getParentDir( int pathLen);
310
311     /**
312      * Make the named file or directory read-only. This interface does not specify whether this
313      * also makes the file undeletable.
314      *
315      * @return <b>true</b> if the named file or directory was made read-only, or it already was read-only;
316      * <b>false</b> if not.
317      */

318     public boolean setReadOnly()
319     {
320         return true;
321     }
322
323     /**
324      * Creates an output stream from a file name. If a normal file already exists with this name it
325      * will first be truncated to zero length.
326      *
327      * @return an output stream suitable for writing to the file.
328      *
329      * @exception FileNotFoundException if the file exists but is a directory
330      * rather than a regular file, does not exist but cannot be created, or
331      * cannot be opened for any other reason.
332      */

333     public OutputStream JavaDoc getOutputStream( ) throws FileNotFoundException JavaDoc
334     {
335         throw new FileNotFoundException JavaDoc( "Attempt to write into a read only file system.");
336     }
337
338     
339     /**
340      * Creates an output stream from a file name. If a normal file already exists with this name it
341      * will first be truncated to zero length.
342      *
343      * @return an output stream suitable for writing to the file.
344      *
345      * @exception FileNotFoundException if the file exists but is a directory
346      * rather than a regular file, does not exist but cannot be created, or
347      * cannot be opened for any other reason.
348      */

349     public OutputStream JavaDoc getOutputStream( boolean append) throws FileNotFoundException JavaDoc
350     {
351         throw new FileNotFoundException JavaDoc( "Attempt to write into a read only file system.");
352     }
353
354     
355     /**
356      * Creates an input stream from a file name.
357      *
358      * @return an input stream suitable for reading from the file.
359      *
360      * @exception FileNotFoundException if the file is not found.
361      */

362     abstract public InputStream JavaDoc getInputStream( ) throws FileNotFoundException JavaDoc;
363
364     /**
365      * Get an exclusive lock with this name. This is used to ensure that two or more JVMs do not open the same database
366      * at the same time.
367      *
368      * @return EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE if the lock cannot be acquired because it is already held.<br>
369      * EXCLUSIVE_FILE_LOCK if the lock was successfully acquired.<br>
370      * NO_FILE_LOCK_SUPPORT if the system does not support exclusive locks.<br>
371      */

372     public int getExclusiveFileLock()
373     {
374         return NO_FILE_LOCK_SUPPORT;
375     }
376
377     /**
378      * Release the resource associated with an earlier acquired exclusive lock
379      *
380      * @see #getExclusiveFileLock
381      */

382     public void releaseExclusiveFileLock()
383     {}
384
385     /**
386      * Get a random access file.
387      *
388      * @param mode "r", "rw", "rws", or "rwd". The "rws" and "rwd" modes specify
389      * that the data is to be written to persistent store, consistent with the
390      * java.io.RandomAccessFile class ("synchronized" with the persistent
391      * storage, in the file system meaning of the word "synchronized"). However
392      * the implementation is not required to implement the "rws" or "rwd"
393      * modes. The implementation may treat "rws" and "rwd" as "rw". It is up to
394      * the user of this interface to call the StorageRandomAccessFile.sync
395      * method. However, if the "rws" or "rwd" modes are supported and the
396      * RandomAccessFile was opened in "rws" or "rwd" mode then the
397      * implementation of StorageRandomAccessFile.sync need not do anything.
398      *
399      * @return an object that can be used for random access to the file.
400      *
401      * @exception IllegalArgumentException if the mode argument is not equal to one of "r", "rw", "rws", or "rwd".
402      * @exception FileNotFoundException if the file exists but is a directory rather than a regular
403      * file, or cannot be opened or created for any other reason .
404      *
405      * @see <a HREF="http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html">java.io.RandomAccessFile</a>
406      */

407     public StorageRandomAccessFile getRandomAccessFile( String JavaDoc mode) throws FileNotFoundException JavaDoc
408     {
409         if( SanityManager.DEBUG)
410             SanityManager.NOTREACHED();
411         return null;
412     }
413
414     /**
415      * Get the file name for diagnostic purposes. Usually the same as getPath().
416      *
417      * @return the file name
418      */

419     public String JavaDoc toString()
420     {
421         return path;
422     }
423     
424     /**
425      * @see org.apache.derby.io.StorageFile#getURL()
426      */

427     public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
428         throw new MalformedURLException JavaDoc(toString());
429     }
430 }
431
Popular Tags