本文整理匯總了Java中java.net.URLStreamHandler類的典型用法代碼示例。如果您正苦於以下問題:Java URLStreamHandler類的具體用法?Java URLStreamHandler怎麽用?Java URLStreamHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
URLStreamHandler類屬於java.net包,在下文中一共展示了URLStreamHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: literalURL
import java.net.URLStreamHandler; //導入依賴的package包/類
static URL literalURL(final String content) {
try {
return new URL("literal", null, 0, content, new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
return new URLConnection(u) {
public @Override
InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(content.getBytes());
}
public @Override
void connect() throws IOException {
}
};
}
});
} catch (MalformedURLException x) {
throw new AssertionError(x);
}
}
示例2: check
import java.net.URLStreamHandler; //導入依賴的package包/類
private void check(final String n) {
assertNull(Lookups.metaInfServices(new ClassLoader() {
protected @Override Enumeration<URL> findResources(String name) throws IOException {
if (name.equals("META-INF/services/java.lang.Object")) {
return singleton(new URL(null, "dummy:stuff", new URLStreamHandler() {
protected URLConnection openConnection(URL u) throws IOException {
return new URLConnection(u) {
public void connect() throws IOException {}
public @Override InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(n.getBytes("UTF-8"));
}
};
}
}));
} else {
return Collections.enumeration(Collections.<URL>emptyList());
}
}
}).lookup(Object.class));
}
示例3: createURLStreamHandler
import java.net.URLStreamHandler; //導入依賴的package包/類
public @Override synchronized URLStreamHandler createURLStreamHandler(final String protocol) {
if (STANDARD_PROTOCOLS.contains(protocol)) {
// Well-known handlers in JRE. Do not try to initialize lookup.
return null;
}
if (!results.containsKey(protocol)) {
final Lookup.Result<URLStreamHandler> result = Lookups.forPath("URLStreamHandler/" + protocol).lookupResult(URLStreamHandler.class);
LookupListener listener = new LookupListener() {
public @Override void resultChanged(LookupEvent ev) {
synchronized (ProxyURLStreamHandlerFactory.this) {
Collection<? extends URLStreamHandler> instances = result.allInstances();
handlers.put(protocol, instances.isEmpty() ? null : instances.iterator().next());
}
}
};
result.addLookupListener(listener);
listener.resultChanged(null);
results.put(protocol, result);
}
return handlers.get(protocol);
}
示例4: createURLStreamHandler
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Creates a new URLStreamHandler instance with the specified protocol.
* Will return null if the protocol is not <code>jndi</code>.
*
* @param protocol the protocol (must be "jndi" here)
* @return a URLStreamHandler for the jndi protocol, or null if the
* protocol is not JNDI
*/
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equals("jndi")) {
return new DirContextURLStreamHandler();
} else if (protocol.equals("classpath")) {
return new ClasspathURLStreamHandler();
} else {
for (URLStreamHandlerFactory factory : userFactories) {
URLStreamHandler handler =
factory.createURLStreamHandler(protocol);
if (handler != null) {
return handler;
}
}
return null;
}
}
示例5: load
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Creates a new URL object that retrieves the content specified by the
* given URL with credentials. Internally the content is pre-fetched with
* the given credentials. The returned URL has a custom protocol handler
* that simply returns the pre-fetched content.
*
* @param url
* the URL to read
* @param username
* @param password
* @return an URL with a custom protocol handler
* @throws IOException
* if an I/O exception occurs.
*/
public static URL load(final URL url, final String username,
final String password) throws IOException {
final byte[] content = getUrlContent(url, username, password);
final URLStreamHandler handler = new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
return new URLConnection(url) {
@Override
public void connect() throws IOException {
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(content);
}
};
}
};
return new URL(url.getProtocol(), url.getHost(), url.getPort(), url
.getFile(), handler);
}
示例6: buildUrl
import java.net.URLStreamHandler; //導入依賴的package包/類
private static URL buildUrl(String urlSpec,
URLStreamHandler handler) {
try {
URLStreamHandler keyHandler;
if (handler == null && urlSpec.startsWith(LEP_PROTOCOL + ":")) {
keyHandler = buildDefaultStreamHandler();
} else {
keyHandler = handler;
}
return new URL(null, urlSpec, keyHandler);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Can't build URL by LEP resource spec: "
+ urlSpec
+ " because of error: "
+ e.getMessage(), e);
}
}
示例7: createURL
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Creates a URL from the specified protocol name, host name, port number,
* and file name.
* <p/>
* No validation of the inputs is performed by this method.
*
* @param protocol the name of the protocol to use
* @param host the name of the host
* @param port the port number
* @param file the file on the host
* @return URL created using specified protocol, host, and file
* @throws MalformedURLException if an unknown protocol is specified
*/
public static URL createURL(String protocol,
String host,
int port,
String file) throws MalformedURLException {
URLStreamHandlerFactory factory = _factories.get(protocol);
// If there is no URLStreamHandlerFactory registered for the
// scheme/protocol, then we just use the regular URL constructor.
if (factory == null) {
return new URL(protocol, host, port, file);
}
// If there is a URLStreamHandlerFactory associated for the
// scheme/protocol, then we create a URLStreamHandler. And, then use
// then use the URLStreamHandler to create a URL.
URLStreamHandler handler = factory.createURLStreamHandler(protocol);
return new URL(protocol, host, port, file, handler);
}
示例8: registerHandler
import java.net.URLStreamHandler; //導入依賴的package包/類
public static void registerHandler(final AssetManager manager) {
URL.setURLStreamHandlerFactory(protocol -> "assets".equals(protocol) ? new URLStreamHandler() {
protected URLConnection openConnection(URL url) throws IOException {
return new URLConnection(url) {
@Override
public void connect() throws IOException {
}
@Override
public InputStream getInputStream() throws IOException {
return manager.open(url.getFile());
}
};
}
} : null);
}
示例9: addRepository
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Add a new repository to the set of places this ClassLoader can look for
* classes to be loaded.
*
* @param repository Name of a source of classes to be loaded, such as a
* directory pathname, a JAR file pathname, or a ZIP file pathname
*
* @exception IllegalArgumentException if the specified repository is
* invalid or does not exist
*/
public void addRepository(String repository) {
if (debug >= 1)
log("addRepository(" + repository + ")");
// Add this repository to our underlying class loader
try {
URLStreamHandler streamHandler = null;
String protocol = parseProtocol(repository);
if (factory != null)
streamHandler = factory.createURLStreamHandler(protocol);
URL url = new URL(null, repository, streamHandler);
super.addURL(url);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e.toString());
}
// Add this repository to our internal list
addRepositoryInternal(repository);
}
示例10: convert
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Convert an array of String to an array of URL and return it.
*
* @param input The array of String to be converted
* @param factory Handler factory to use to generate the URLs
*/
protected static URL[] convert(String input[],
URLStreamHandlerFactory factory) {
URLStreamHandler streamHandler = null;
URL url[] = new URL[input.length];
for (int i = 0; i < url.length; i++) {
try {
String protocol = parseProtocol(input[i]);
if (factory != null)
streamHandler = factory.createURLStreamHandler(protocol);
else
streamHandler = null;
url[i] = new URL(null, input[i], streamHandler);
} catch (MalformedURLException e) {
url[i] = null;
}
}
return (url);
}
示例11: createParserFromUrl
import java.net.URLStreamHandler; //導入依賴的package包/類
@Test
public void createParserFromUrl() throws Exception {
class NullUrlConnection extends URLConnection {
protected NullUrlConnection(URL url) {
super(url);
}
@Override
public void connect() throws IOException {
}
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(new byte[0]);
}
};
class NullUrlStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL u) throws IOException {
return new NullUrlConnection(u);
}
};
assertThat(factory.createParser(new URL("foo", "bar", 99, "baz", new NullUrlStreamHandler())), instanceOf(IonParser.class));
}
示例12: testProvideServiceNotInModule
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Test redefineClass by attempting to update java.base to provide a service
* where the service provider class is not in the module.
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testProvideServiceNotInModule() {
Module baseModule = Object.class.getModule();
class MyProvider extends URLStreamHandlerProvider {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
return null;
}
}
// attempt to update java.base to provide MyProvider
Map<Class<?>, List<Class<?>>> extraProvides
= Map.of(URLStreamHandlerProvider.class, List.of(MyProvider.class));
redefineModule(baseModule, Set.of(), Map.of(), Map.of(), Set.of(), extraProvides);
}
示例13: createURLStreamHandler
import java.net.URLStreamHandler; //導入依賴的package包/類
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
// don't do classloading when called for protocols that are involved in classloading
if(protocol.equals("file") || protocol.equals("jar"))
return null;
String clsName = packageName + "." + protocol + ".Handler";
try
{
Class cls = Class.forName(clsName);
return (URLStreamHandler) cls.newInstance();
} catch (Throwable e)
{
// URLs are involved in classloading, evil things might happen
}
return null;
}
示例14: installGitProtocol
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Installs the GIT protocol that we use to identify certain file versions.
*/
protected void installGitProtocol() {
// Install protocol.
try {
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equals(GitRevisionURLHandler.GIT_PROTOCOL)) {
URLStreamHandler handler = new GitRevisionURLHandler();
return handler;
}
return null;
}
});
} catch (Throwable t) {
if (!t.getMessage().contains("factory already defined")) {
logger.info(t, t);
}
}
}
示例15: createURLStreamHandler
import java.net.URLStreamHandler; //導入依賴的package包/類
/**
* Creates a new URLStreamHandler instance with the specified protocol. Will
* return null if the protocol is not <code>jndi</code>.
*
* @param protocol
* the protocol (must be "jndi" here)
* @return a URLStreamHandler for the jndi protocol, or null if the protocol
* is not JNDI
*/
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (protocol.equals("jndi")) {
return new DirContextURLStreamHandler();
} else if (protocol.equals("classpath")) {
return new ClasspathURLStreamHandler();
} else {
for (URLStreamHandlerFactory factory : userFactories) {
URLStreamHandler handler = factory.createURLStreamHandler(protocol);
if (handler != null) {
return handler;
}
}
return null;
}
}