1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.io.URLFile |
4 | |
5 | Copyright 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.io; |
22 | |
23 | import org.apache.derby.io.StorageFile; |
24 | import org.apache.derby.io.StorageRandomAccessFile; |
25 | |
26 | import org.apache.derby.iapi.services.sanity.SanityManager; |
27 | |
28 | import java.io.InputStream; |
29 | import java.io.OutputStream; |
30 | import java.io.IOException; |
31 | import java.io.FileNotFoundException; |
32 | |
33 | import java.net.URL; |
34 | |
35 | /** |
36 | * This class provides a class path based implementation of the StorageFile interface. It is used by the |
37 | * database engine to access persistent data and transaction logs under the classpath subsubprotocol. |
38 | */ |
39 | class URLFile extends InputStreamFile |
40 | { |
41 | |
42 | private final URLStorageFactory storageFactory; |
43 | |
44 | URLFile( URLStorageFactory storageFactory, String path) |
45 | { |
46 | super( storageFactory, path); |
47 | this.storageFactory = storageFactory; |
48 | } |
49 | |
50 | URLFile( URLStorageFactory storageFactory, String parent, String name) |
51 | { |
52 | super( storageFactory, parent, name); |
53 | this.storageFactory = storageFactory; |
54 | } |
55 | |
56 | URLFile( URLFile dir, String name) |
57 | { |
58 | super( dir,name); |
59 | this.storageFactory = dir.storageFactory; |
60 | } |
61 | |
62 | private URLFile( URLStorageFactory storageFactory, String child, int pathLen) |
63 | { |
64 | super( storageFactory, child, pathLen); |
65 | this.storageFactory = storageFactory; |
66 | } |
67 | |
68 | /** |
69 | * Tests whether the named file exists. |
70 | * |
71 | * @return <b>true</b> if the named file exists, <b>false</b> if not. |
72 | */ |
73 | public boolean exists() |
74 | { |
75 | try |
76 | { |
77 | InputStream is = getInputStream(); |
78 | if( is == null) |
79 | return false; |
80 | is.close(); |
81 | return true; |
82 | } |
83 | catch( IOException ioe){ return false;} |
84 | } // end of exists |
85 | |
86 | /** |
87 | * Get the parent of this file. |
88 | * |
89 | * @param pathLen the length of the parent's path name. |
90 | */ |
91 | StorageFile getParentDir( int pathLen) |
92 | { |
93 | return new URLFile( storageFactory, path, pathLen); |
94 | } |
95 | |
96 | /** |
97 | * Creates an input stream from a file name. |
98 | * |
99 | * @return an input stream suitable for reading from the file. |
100 | * |
101 | * @exception FileNotFoundException if the file is not found. |
102 | */ |
103 | public InputStream getInputStream( ) throws FileNotFoundException |
104 | { |
105 | try |
106 | { |
107 | URL url = new URL( path); |
108 | return url.openStream(); |
109 | } |
110 | catch( IOException ioe){ throw new java.io.FileNotFoundException(path);} |
111 | } // end of getInputStream |
112 | } |