List of usage examples for java.net URL getContent
public final Object getContent() throws java.io.IOException
From source file:org.gtdfree.ApplicationHelper.java
public static final ImageIcon loadIcon(String resource) { try {/*w w w. j av a 2s . co m*/ URL url = ApplicationHelper.class.getClassLoader().getResource(resource); if (!(url.getContent() instanceof ImageProducer)) return null; return new ImageIcon(url); } catch (Exception e) { return null; } }
From source file:org.gtdfree.ApplicationHelper.java
public static final Image loadImage(String resource) { try {/* w w w. j a v a 2 s .co m*/ URL url = ApplicationHelper.class.getClassLoader().getResource(resource); if (!(url.getContent() instanceof ImageProducer)) return null; return new ImageIcon(url).getImage(); } catch (Exception e) { return null; } }
From source file:Main.java
static InputStream openRemoteInputStream(Uri uri) { URL finalUrl; try {// w ww . j a va 2s.c o m finalUrl = new URL(uri.toString()); } catch (MalformedURLException e) { e.printStackTrace(); return null; } HttpURLConnection connection; try { connection = (HttpURLConnection) finalUrl.openConnection(); } catch (IOException e) { e.printStackTrace(); return null; } connection.setInstanceFollowRedirects(false); int code; try { code = connection.getResponseCode(); } catch (IOException e) { e.printStackTrace(); return null; } if ((code == 301) || (code == 302) || (code == 303)) { String newLocation = connection.getHeaderField("Location"); return openRemoteInputStream(Uri.parse(newLocation)); } try { return (InputStream) finalUrl.getContent(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:net.dataforte.commons.resources.ClassUtils.java
/** * Returns all resources beneath a folder. Supports filesystem, JARs and JBoss VFS * // ww w . j a v a 2s .c o m * @param folder * @return * @throws IOException */ public static URL[] getResources(String folder) throws IOException { List<URL> urls = new ArrayList<URL>(); ArrayList<File> directories = new ArrayList<File>(); try { ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) { throw new IOException("Can't get class loader."); } // Ask for all resources for the path Enumeration<URL> resources = cld.getResources(folder); while (resources.hasMoreElements()) { URL res = resources.nextElement(); String resProtocol = res.getProtocol(); if (resProtocol.equalsIgnoreCase("jar")) { JarURLConnection conn = (JarURLConnection) res.openConnection(); JarFile jar = conn.getJarFile(); for (JarEntry e : Collections.list(jar.entries())) { if (e.getName().startsWith(folder) && !e.getName().endsWith("/")) { urls.add(new URL( joinUrl(res.toString(), "/" + e.getName().substring(folder.length() + 1)))); // FIXME: fully qualified name } } } else if (resProtocol.equalsIgnoreCase("vfszip") || resProtocol.equalsIgnoreCase("vfs")) { // JBoss 5+ try { Object content = res.getContent(); Method getChildren = content.getClass().getMethod("getChildren"); List<?> files = (List<?>) getChildren.invoke(res.getContent()); Method toUrl = null; for (Object o : files) { if (toUrl == null) { toUrl = o.getClass().getMethod("toURL"); } urls.add((URL) toUrl.invoke(o)); } } catch (Exception e) { throw new IOException("Error while scanning " + res.toString(), e); } } else if (resProtocol.equalsIgnoreCase("file")) { directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8"))); } else { throw new IOException("Unknown protocol for resource: " + res.toString()); } } } catch (NullPointerException x) { throw new IOException(folder + " does not appear to be a valid folder (Null pointer exception)"); } catch (UnsupportedEncodingException encex) { throw new IOException(folder + " does not appear to be a valid folder (Unsupported encoding)"); } // For every directory identified capture all the .class files for (File directory : directories) { if (directory.exists()) { // Get the list of the files contained in the package String[] files = directory.list(); for (String file : files) { urls.add(new URL("file:///" + joinPath(directory.getAbsolutePath(), file))); } } else { throw new IOException( folder + " (" + directory.getPath() + ") does not appear to be a valid folder"); } } URL[] urlsA = new URL[urls.size()]; urls.toArray(urlsA); return urlsA; }
From source file:net.sarangnamu.android.DrawableManager.java
private InputStream fetch(String urlString) throws MalformedURLException, IOException { URL url = new URL(urlString); return (InputStream) url.getContent(); }
From source file:net.dataforte.commons.resources.ClassUtils.java
/** * Returns all classes within the specified package. Supports filesystem, JARs and JBoss VFS * //from w w w .jav a2s . c om * @param folder * @return * @throws IOException */ public static Class<?>[] getClassesForPackage(String pckgname) throws ClassNotFoundException { // This will hold a list of directories matching the pckgname. // There may be more than one if a package is split over multiple // jars/paths List<Class<?>> classes = new ArrayList<Class<?>>(); ArrayList<File> directories = new ArrayList<File>(); try { ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) { throw new ClassNotFoundException("Can't get class loader."); } // Ask for all resources for the path Enumeration<URL> resources = cld.getResources(pckgname.replace('.', '/')); while (resources.hasMoreElements()) { URL res = resources.nextElement(); if (res.getProtocol().equalsIgnoreCase("jar")) { JarURLConnection conn = (JarURLConnection) res.openConnection(); JarFile jar = conn.getJarFile(); for (JarEntry e : Collections.list(jar.entries())) { if (e.getName().startsWith(pckgname.replace('.', '/')) && e.getName().endsWith(".class") && !e.getName().contains("$")) { String className = e.getName().replace("/", ".").substring(0, e.getName().length() - 6); classes.add(Class.forName(className, true, cld)); } } } else if (res.getProtocol().equalsIgnoreCase("vfszip")) { // JBoss 5+ try { Object content = res.getContent(); Method getChildren = content.getClass().getMethod("getChildren"); List<?> files = (List<?>) getChildren.invoke(res.getContent()); Method getPathName = null; for (Object o : files) { if (getPathName == null) { getPathName = o.getClass().getMethod("getPathName"); } String pathName = (String) getPathName.invoke(o); if (pathName.endsWith(".class")) { String className = pathName.replace("/", ".").substring(0, pathName.length() - 6); classes.add(Class.forName(className, true, cld)); } } } catch (Exception e) { throw new IOException("Error while scanning " + res.toString(), e); } } else { directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8"))); } } } catch (NullPointerException x) { throw new ClassNotFoundException( pckgname + " does not appear to be a valid package (Null pointer exception)", x); } catch (UnsupportedEncodingException encex) { throw new ClassNotFoundException( pckgname + " does not appear to be a valid package (Unsupported encoding)", encex); } catch (IOException ioex) { throw new ClassNotFoundException( "IOException was thrown when trying to get all resources for " + pckgname, ioex); } // For every directory identified capture all the .class files for (File directory : directories) { if (directory.exists()) { // Get the list of the files contained in the package String[] files = directory.list(); for (String file : files) { // we are only interested in .class files if (file.endsWith(".class")) { // removes the .class extension classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6))); } } } else { throw new ClassNotFoundException( pckgname + " (" + directory.getPath() + ") does not appear to be a valid package"); } } Class<?>[] classesA = new Class[classes.size()]; classes.toArray(classesA); return classesA; }
From source file:ste.xtest.net.BugFreeURL.java
@Test public void use_default_handler_for_not_stubbed_urls() throws Exception { URL u = new File("src/test/resources/html/documentlocation.html").toURI().toURL(); then(IOUtils.toString((InputStream) u.getContent())).contains("TODO write content"); }
From source file:Main.java
/** * Return an {@link InputStream} from the given url or null if failed to retrieve the content * //from w w w . j a va 2 s . co m * @param uri * @return */ static InputStream openRemoteInputStream(Uri uri) { java.net.URL finalUrl; try { finalUrl = new java.net.URL(uri.toString()); } catch (MalformedURLException e) { e.printStackTrace(); return null; } HttpURLConnection connection; try { connection = (HttpURLConnection) finalUrl.openConnection(); } catch (IOException e) { e.printStackTrace(); return null; } connection.setInstanceFollowRedirects(false); int code; try { code = connection.getResponseCode(); } catch (IOException e) { e.printStackTrace(); return null; } // permanent redirection if (code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_SEE_OTHER) { String newLocation = connection.getHeaderField("Location"); return openRemoteInputStream(Uri.parse(newLocation)); } try { return (InputStream) finalUrl.getContent(); } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:com.neudesic.mobile.pulse.ui.drawable.DrawableManager.java
public Object fetchImage(String address) throws MalformedURLException, IOException { URL url = new URL(address); Object content = url.getContent(); return content; }
From source file:org.jdal.gis.google.DirectionsService.java
public MultiLineString getRoute(RouteRequest request) { try {//w w w .j a v a 2s . c o m URL url = new URL(serverUrl + "?" + request.toString()); log.debug("Sending request: " + url.toString()); return parseResponse(url.getContent()); } catch (Exception e) { log.error(e); } return null; }