Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / MyDerby-10.3 / java / engine / org / apache / derby / impl / io / JarDBFile.java
diff --git a/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/impl/io/JarDBFile.java b/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/impl/io/JarDBFile.java
new file mode 100644 (file)
index 0000000..94f74e6
--- /dev/null
@@ -0,0 +1,156 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.io.JarDBFile\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.io;\r
+\r
+import org.apache.derby.io.StorageFile;\r
+import org.apache.derby.io.StorageRandomAccessFile;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import java.io.File;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.io.IOException;\r
+import java.io.FileNotFoundException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipFile;\r
+\r
+\r
+/**\r
+ * This class provides a jar file based implementation of the StorageFile interface. It is used by the\r
+ * database engine to access persistent data and transaction logs under the jar subsubprotocol.\r
+ */\r
+class JarDBFile extends InputStreamFile\r
+{\r
+\r
+    private final JarStorageFactory storageFactory;\r
+\r
+    JarDBFile( JarStorageFactory storageFactory, String path)\r
+    {\r
+        super( storageFactory, path);\r
+        this.storageFactory = storageFactory;\r
+    }\r
+\r
+    JarDBFile( JarStorageFactory storageFactory, String parent, String name)\r
+    {\r
+        super( storageFactory, parent, name);\r
+        this.storageFactory = storageFactory;\r
+    }\r
+\r
+    JarDBFile( JarDBFile dir, String name)\r
+    {\r
+        super( dir,name);\r
+        this.storageFactory = dir.storageFactory;\r
+    }\r
+\r
+    private JarDBFile( JarStorageFactory storageFactory, String child, int pathLen)\r
+    {\r
+        super( storageFactory, child, pathLen);\r
+        this.storageFactory = storageFactory;\r
+    }\r
+\r
+    /**\r
+     * Tests whether the named file exists.\r
+     *\r
+     * @return <b>true</b> if the named file exists, <b>false</b> if not.\r
+     */\r
+    public boolean exists()\r
+    {\r
+        return getEntry() != null;\r
+    } // end of exists\r
+\r
+    private ZipEntry getEntry()\r
+    {\r
+        return storageFactory.zipData.getEntry( path);\r
+    }\r
+\r
+    /**\r
+     * Returns the length of the named file if it is not a directory. The return value is not specified\r
+     * if the file is a directory.\r
+     *\r
+     * @return The length, in bytes, of the named file if it exists and is not a directory,\r
+     *         0 if the file does not exist, or any value if the named file is a directory.\r
+     */\r
+    public long length()\r
+    {\r
+        ZipEntry entry = getEntry();\r
+        if( entry == null)\r
+            return 0;\r
+        return entry.getSize();\r
+    } // end of length\r
+\r
+    /**\r
+     * Get the name of the parent directory if this name includes a parent.\r
+     *\r
+     * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if\r
+     *         it does not have a parent.\r
+     */\r
+    StorageFile getParentDir( int pathLen)\r
+    {\r
+        return new JarDBFile( storageFactory, path, pathLen);\r
+    }\r
+    \r
+    /**\r
+     * Creates an input stream from a file name.\r
+     *\r
+     * @return an input stream suitable for reading from the file.\r
+     *\r
+     * @exception FileNotFoundException if the file is not found.\r
+     */\r
+    public InputStream getInputStream( ) throws FileNotFoundException\r
+    {\r
+        ZipEntry zipEntry = getEntry( );\r
+               if (zipEntry == null)\r
+                       throw new java.io.FileNotFoundException(path);\r
+\r
+        try\r
+        {\r
+            return storageFactory.zipData.getInputStream(zipEntry);\r
+        }\r
+        catch( IOException ioe){ throw new java.io.FileNotFoundException(path);}\r
+    } // end of getInputStream\r
+\r
+    /**\r
+     * Get the file name for diagnostic purposes. Usually the same as getPath().\r
+     *\r
+     * @return the file name\r
+     */\r
+    public String toString()\r
+    {\r
+        return path;\r
+    }\r
+       /**\r
+     * Return a URL for this file (resource). Returns a URL according to the\r
+     * spec for java.net.JarURLConnection\r
+     * \r
+     * @see org.apache.derby.io.StorageFile#getURL()\r
+     */\r
+    public URL getURL() throws MalformedURLException {\r
+        File pathFile = new File(storageFactory.zipData.getName());\r
+\r
+        String pathFileURL = pathFile.toURL().toString();\r
+\r
+        return new URL("jar:" + pathFileURL + "!/" + path);\r
+    }\r
+}\r