KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.io.JarDBFile
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.StorageFile;
25 import org.apache.derby.io.StorageRandomAccessFile;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import java.io.File JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.net.MalformedURLException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.zip.ZipEntry JavaDoc;
37 import java.util.zip.ZipFile JavaDoc;
38
39
40 /**
41  * This class provides a jar file based implementation of the StorageFile interface. It is used by the
42  * database engine to access persistent data and transaction logs under the jar subsubprotocol.
43  */

44 class JarDBFile extends InputStreamFile
45 {
46
47     private final JarStorageFactory storageFactory;
48
49     JarDBFile( JarStorageFactory storageFactory, String JavaDoc path)
50     {
51         super( storageFactory, path);
52         this.storageFactory = storageFactory;
53     }
54
55     JarDBFile( JarStorageFactory storageFactory, String JavaDoc parent, String JavaDoc name)
56     {
57         super( storageFactory, parent, name);
58         this.storageFactory = storageFactory;
59     }
60
61     JarDBFile( JarDBFile dir, String JavaDoc name)
62     {
63         super( dir,name);
64         this.storageFactory = dir.storageFactory;
65     }
66
67     private JarDBFile( JarStorageFactory storageFactory, String JavaDoc child, int pathLen)
68     {
69         super( storageFactory, child, pathLen);
70         this.storageFactory = storageFactory;
71     }
72
73     /**
74      * Tests whether the named file exists.
75      *
76      * @return <b>true</b> if the named file exists, <b>false</b> if not.
77      */

78     public boolean exists()
79     {
80         return getEntry() != null;
81     } // end of exists
82

83     private ZipEntry JavaDoc getEntry()
84     {
85         return storageFactory.zipData.getEntry( path);
86     }
87
88     /**
89      * Returns the length of the named file if it is not a directory. The return value is not specified
90      * if the file is a directory.
91      *
92      * @return The length, in bytes, of the named file if it exists and is not a directory,
93      * 0 if the file does not exist, or any value if the named file is a directory.
94      */

95     public long length()
96     {
97         ZipEntry JavaDoc entry = getEntry();
98         if( entry == null)
99             return 0;
100         return entry.getSize();
101     } // end of length
102

103     /**
104      * Get the name of the parent directory if this name includes a parent.
105      *
106      * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if
107      * it does not have a parent.
108      */

109     StorageFile getParentDir( int pathLen)
110     {
111         return new JarDBFile( storageFactory, path, pathLen);
112     }
113     
114     /**
115      * Creates an input stream from a file name.
116      *
117      * @return an input stream suitable for reading from the file.
118      *
119      * @exception FileNotFoundException if the file is not found.
120      */

121     public InputStream JavaDoc getInputStream( ) throws FileNotFoundException JavaDoc
122     {
123         ZipEntry JavaDoc zipEntry = getEntry( );
124         if (zipEntry == null)
125             throw new java.io.FileNotFoundException JavaDoc(path);
126
127         try
128         {
129             return storageFactory.zipData.getInputStream(zipEntry);
130         }
131         catch( IOException JavaDoc ioe){ throw new java.io.FileNotFoundException JavaDoc(path);}
132     } // end of getInputStream
133

134     /**
135      * Get the file name for diagnostic purposes. Usually the same as getPath().
136      *
137      * @return the file name
138      */

139     public String JavaDoc toString()
140     {
141         return path;
142     }
143     /**
144      * Return a URL for this file (resource). Returns a URL according to the
145      * spec for java.net.JarURLConnection
146      *
147      * @see org.apache.derby.io.StorageFile#getURL()
148      */

149     public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
150         File pathFile = new File(storageFactory.zipData.getName());
151
152         String JavaDoc pathFileURL = pathFile.toURL().toString();
153
154         return new URL JavaDoc("jar:" + pathFileURL + "!/" + path);
155     }
156 }
157
Popular Tags