01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.util;
05:
06: import java.io.IOException;
07: import java.io.InputStream;
08: import java.net.URL;
09: import java.util.jar.JarEntry;
10: import java.util.jar.JarInputStream;
11:
12: /**
13: * Helper methods to load resources from a jar
14: */
15: public final class JarResourceLoader {
16:
17: private JarResourceLoader() {
18: // uninstantiateable
19: }
20:
21: /**
22: * Load a resource file from a jar file at location
23: * @param location The URL to the JAR file
24: * @param resource The resource string
25: * @return Input stream to the resource - close it when you're done
26: * @throws IOException If some bad IO happens
27: */
28: public static InputStream getJarResource(final URL location,
29: final String resource) throws IOException {
30: final JarInputStream jis = new JarInputStream(location
31: .openStream());
32: for (JarEntry entry = jis.getNextJarEntry(); entry != null; entry = jis
33: .getNextJarEntry()) {
34: if (entry.getName().equals(resource)) {
35: return jis;
36: }
37: }
38: return null;
39: }
40:
41: }
|