Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f1a72623b7e38d018f6699764d0f55e3d49b7cf6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
 * Copyright (c) 2005, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.storage.bundlefile;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * A BundleEntry represented by a File object.  The FileBundleEntry class is
 * used for bundles that are installed as extracted zips on a file system.
 */
public class FileBundleEntry extends BundleEntry {
	/**
	 * File for this entry.
	 */
	private final File file;
	/**
	 * The name for this entry
	 */
	private final String name;

	/**
	 * Constructs the BundleEntry using a File.
	 * @param file BundleFile object this entry is a member of
	 * @param name the name of this BundleEntry
	 */
	public FileBundleEntry(File file, String name) {
		this.file = file;
		boolean endsInSlash = name.length() > 0 && name.charAt(name.length() - 1) == '/';
		if (BundleFile.secureAction.isDirectory(file)) {
			if (!endsInSlash)
				name += '/';
		} else if (endsInSlash)
			name = name.substring(0, name.length() - 1);
		this.name = name;
	}

	/**
	 * Return an InputStream for the entry.
	 *
	 * @return InputStream for the entry
	 * @exception java.io.IOException
	 */
	public InputStream getInputStream() throws IOException {
		return BundleFile.secureAction.getFileInputStream(file);
	}

	/**
	 * Return size of the uncompressed entry.
	 *
	 * @return size of entry
	 */
	public long getSize() {
		return BundleFile.secureAction.length(file);
	}

	/**
	 * Return name of the entry.
	 *
	 * @return name of entry
	 */
	public String getName() {
		return (name);
	}

	/**
	 * Get the modification time for this BundleEntry.
	 * <p>If the modification time has not been set,
	 * this method will return <tt>-1</tt>.
	 *
	 * @return last modification time.
	 */
	public long getTime() {
		return BundleFile.secureAction.lastModified(file);
	}

	public URL getLocalURL() {
		return getFileURL();
	}

	@SuppressWarnings("deprecation")
	public URL getFileURL() {
		try {
			return file.toURL();
		} catch (MalformedURLException e) {
			return null;
		}
	}
}

Back to the top