001: /*
002:
003: Derby - Class org.apache.derby.impl.io.URLFile
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.InputStream;
030: import java.io.OutputStream;
031: import java.io.IOException;
032: import java.io.FileNotFoundException;
033:
034: import java.net.URL;
035:
036: /**
037: * This class provides a class path based implementation of the StorageFile interface. It is used by the
038: * database engine to access persistent data and transaction logs under the classpath subsubprotocol.
039: */
040: class URLFile extends InputStreamFile {
041:
042: private final URLStorageFactory storageFactory;
043:
044: URLFile(URLStorageFactory storageFactory, String path) {
045: super (storageFactory, path);
046: this .storageFactory = storageFactory;
047: }
048:
049: URLFile(URLStorageFactory storageFactory, String parent, String name) {
050: super (storageFactory, parent, name);
051: this .storageFactory = storageFactory;
052: }
053:
054: URLFile(URLFile dir, String name) {
055: super (dir, name);
056: this .storageFactory = dir.storageFactory;
057: }
058:
059: private URLFile(URLStorageFactory storageFactory, String child,
060: int pathLen) {
061: super (storageFactory, child, pathLen);
062: this .storageFactory = storageFactory;
063: }
064:
065: /**
066: * Tests whether the named file exists.
067: *
068: * @return <b>true</b> if the named file exists, <b>false</b> if not.
069: */
070: public boolean exists() {
071: try {
072: InputStream is = getInputStream();
073: if (is == null)
074: return false;
075: is.close();
076: return true;
077: } catch (IOException ioe) {
078: return false;
079: }
080: } // end of exists
081:
082: /**
083: * Get the parent of this file.
084: *
085: * @param pathLen the length of the parent's path name.
086: */
087: StorageFile getParentDir(int pathLen) {
088: return new URLFile(storageFactory, path, pathLen);
089: }
090:
091: /**
092: * Creates an input stream from a file name.
093: *
094: * @return an input stream suitable for reading from the file.
095: *
096: * @exception FileNotFoundException if the file is not found.
097: */
098: public InputStream getInputStream() throws FileNotFoundException {
099: try {
100: URL url = new URL(path);
101: return url.openStream();
102: } catch (IOException ioe) {
103: throw new java.io.FileNotFoundException(path);
104: }
105: } // end of getInputStream
106: }
|