001: /*
002:
003: Derby - Class org.apache.derby.impl.io.JarDBFile
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.io;
023:
024: import org.apache.derby.io.StorageFile;
025: import org.apache.derby.io.StorageRandomAccessFile;
026:
027: import org.apache.derby.iapi.services.sanity.SanityManager;
028:
029: import java.io.File;
030: import java.io.InputStream;
031: import java.io.OutputStream;
032: import java.io.IOException;
033: import java.io.FileNotFoundException;
034: import java.net.MalformedURLException;
035: import java.net.URL;
036: import java.util.zip.ZipEntry;
037: import java.util.zip.ZipFile;
038:
039: /**
040: * This class provides a jar file based implementation of the StorageFile interface. It is used by the
041: * database engine to access persistent data and transaction logs under the jar subsubprotocol.
042: */
043: class JarDBFile extends InputStreamFile {
044:
045: private final JarStorageFactory storageFactory;
046:
047: JarDBFile(JarStorageFactory storageFactory, String path) {
048: super (storageFactory, path);
049: this .storageFactory = storageFactory;
050: }
051:
052: JarDBFile(JarStorageFactory storageFactory, String parent,
053: String name) {
054: super (storageFactory, parent, name);
055: this .storageFactory = storageFactory;
056: }
057:
058: JarDBFile(JarDBFile dir, String name) {
059: super (dir, name);
060: this .storageFactory = dir.storageFactory;
061: }
062:
063: private JarDBFile(JarStorageFactory storageFactory, String child,
064: int pathLen) {
065: super (storageFactory, child, pathLen);
066: this .storageFactory = storageFactory;
067: }
068:
069: /**
070: * Tests whether the named file exists.
071: *
072: * @return <b>true</b> if the named file exists, <b>false</b> if not.
073: */
074: public boolean exists() {
075: return getEntry() != null;
076: } // end of exists
077:
078: private ZipEntry getEntry() {
079: return storageFactory.zipData.getEntry(path);
080: }
081:
082: /**
083: * Returns the length of the named file if it is not a directory. The return value is not specified
084: * if the file is a directory.
085: *
086: * @return The length, in bytes, of the named file if it exists and is not a directory,
087: * 0 if the file does not exist, or any value if the named file is a directory.
088: */
089: public long length() {
090: ZipEntry entry = getEntry();
091: if (entry == null)
092: return 0;
093: return entry.getSize();
094: } // end of length
095:
096: /**
097: * Get the name of the parent directory if this name includes a parent.
098: *
099: * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if
100: * it does not have a parent.
101: */
102: StorageFile getParentDir(int pathLen) {
103: return new JarDBFile(storageFactory, path, pathLen);
104: }
105:
106: /**
107: * Creates an input stream from a file name.
108: *
109: * @return an input stream suitable for reading from the file.
110: *
111: * @exception FileNotFoundException if the file is not found.
112: */
113: public InputStream getInputStream() throws FileNotFoundException {
114: ZipEntry zipEntry = getEntry();
115: if (zipEntry == null)
116: throw new java.io.FileNotFoundException(path);
117:
118: try {
119: return storageFactory.zipData.getInputStream(zipEntry);
120: } catch (IOException ioe) {
121: throw new java.io.FileNotFoundException(path);
122: }
123: } // end of getInputStream
124:
125: /**
126: * Get the file name for diagnostic purposes. Usually the same as getPath().
127: *
128: * @return the file name
129: */
130: public String toString() {
131: return path;
132: }
133:
134: /**
135: * Return a URL for this file (resource). Returns a URL according to the
136: * spec for java.net.JarURLConnection
137: *
138: * @see org.apache.derby.io.StorageFile#getURL()
139: */
140: public URL getURL() throws MalformedURLException {
141: File pathFile = new File(storageFactory.zipData.getName());
142:
143: String pathFileURL = pathFile.toURL().toString();
144:
145: return new URL("jar:" + pathFileURL + "!/" + path);
146: }
147: }
|