1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.io.JarDBFile |
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.File; |
29 | import java.io.InputStream; |
30 | import java.io.OutputStream; |
31 | import java.io.IOException; |
32 | import java.io.FileNotFoundException; |
33 | import java.net.MalformedURLException; |
34 | import java.net.URL; |
35 | import java.util.zip.ZipEntry; |
36 | import java.util.zip.ZipFile; |
37 | |
38 | |
39 | /** |
40 | * This class provides a jar file based implementation of the StorageFile interface. It is used by the |
41 | * database engine to access persistent data and transaction logs under the jar subsubprotocol. |
42 | */ |
43 | class JarDBFile extends InputStreamFile |
44 | { |
45 | |
46 | private final JarStorageFactory storageFactory; |
47 | |
48 | JarDBFile( JarStorageFactory storageFactory, String path) |
49 | { |
50 | super( storageFactory, path); |
51 | this.storageFactory = storageFactory; |
52 | } |
53 | |
54 | JarDBFile( JarStorageFactory storageFactory, String parent, String name) |
55 | { |
56 | super( storageFactory, parent, name); |
57 | this.storageFactory = storageFactory; |
58 | } |
59 | |
60 | JarDBFile( JarDBFile dir, String name) |
61 | { |
62 | super( dir,name); |
63 | this.storageFactory = dir.storageFactory; |
64 | } |
65 | |
66 | private JarDBFile( JarStorageFactory storageFactory, String child, int pathLen) |
67 | { |
68 | super( storageFactory, child, pathLen); |
69 | this.storageFactory = storageFactory; |
70 | } |
71 | |
72 | /** |
73 | * Tests whether the named file exists. |
74 | * |
75 | * @return <b>true</b> if the named file exists, <b>false</b> if not. |
76 | */ |
77 | public boolean exists() |
78 | { |
79 | return getEntry() != null; |
80 | } // end of exists |
81 | |
82 | private ZipEntry getEntry() |
83 | { |
84 | return storageFactory.zipData.getEntry( path); |
85 | } |
86 | |
87 | /** |
88 | * Returns the length of the named file if it is not a directory. The return value is not specified |
89 | * if the file is a directory. |
90 | * |
91 | * @return The length, in bytes, of the named file if it exists and is not a directory, |
92 | * 0 if the file does not exist, or any value if the named file is a directory. |
93 | */ |
94 | public long length() |
95 | { |
96 | ZipEntry entry = getEntry(); |
97 | if( entry == null) |
98 | return 0; |
99 | return entry.getSize(); |
100 | } // end of length |
101 | |
102 | /** |
103 | * Get the name of the parent directory if this name includes a parent. |
104 | * |
105 | * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if |
106 | * it does not have a parent. |
107 | */ |
108 | StorageFile getParentDir( int pathLen) |
109 | { |
110 | return new JarDBFile( storageFactory, path, pathLen); |
111 | } |
112 | |
113 | /** |
114 | * Creates an input stream from a file name. |
115 | * |
116 | * @return an input stream suitable for reading from the file. |
117 | * |
118 | * @exception FileNotFoundException if the file is not found. |
119 | */ |
120 | public InputStream getInputStream( ) throws FileNotFoundException |
121 | { |
122 | ZipEntry zipEntry = getEntry( ); |
123 | if (zipEntry == null) |
124 | throw new java.io.FileNotFoundException(path); |
125 | |
126 | try |
127 | { |
128 | return storageFactory.zipData.getInputStream(zipEntry); |
129 | } |
130 | catch( IOException ioe){ throw new java.io.FileNotFoundException(path);} |
131 | } // end of getInputStream |
132 | |
133 | /** |
134 | * Get the file name for diagnostic purposes. Usually the same as getPath(). |
135 | * |
136 | * @return the file name |
137 | */ |
138 | public String toString() |
139 | { |
140 | return path; |
141 | } |
142 | /** |
143 | * Return a URL for this file (resource). Returns a URL according to the |
144 | * spec for java.net.JarURLConnection |
145 | * |
146 | * @see org.apache.derby.io.StorageFile#getURL() |
147 | */ |
148 | public URL getURL() throws MalformedURLException { |
149 | File pathFile = new File(storageFactory.zipData.getName()); |
150 | |
151 | String pathFileURL = pathFile.toURL().toString(); |
152 | |
153 | return new URL("jar:" + pathFileURL + "!/" + path); |
154 | } |
155 | } |