Java Code Examples for java.net.URL#getContent()

The following examples show how to use java.net.URL#getContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Beans.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public synchronized Image getImage(URL url) {
    Object o = imageCache.get(url);
    if (o != null) {
        return (Image)o;
    }
    try {
        o = url.getContent();
        if (o == null) {
            return null;
        }
        if (o instanceof Image) {
            imageCache.put(url, o);
            return (Image) o;
        }
        // Otherwise it must be an ImageProducer.
        Image img = target.createImage((java.awt.image.ImageProducer)o);
        imageCache.put(url, img);
        return img;

    } catch (Exception ex) {
        return null;
    }
}
 
Example 2
Source File: DownloadFileRequest.java    From Broadsheet.ie-Android with MIT License 6 votes vote down vote up
@Override
public File loadDataFromNetwork() throws IOException {
    int bytesRead;
    URL url = new URL(source);

    File destFile = new File(destinationFolder, Uri.parse(source).getLastPathSegment());
    FileOutputStream fileOutput = new FileOutputStream(destFile);

    InputStream inputStream = (InputStream) url.getContent();
    byte[] buffer = new byte[MAX_BUFFER_SIZE];
    while ((bytesRead = inputStream.read(buffer)) > 0) {
        fileOutput.write(buffer, 0, bytesRead);
    }
    fileOutput.close();

    return destFile;
}
 
Example 3
Source File: MarkerIconDialog.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Preview the image when it is a local image file.
 * 
 * @param uri
 *            Image absolute path without "file:///"
 */
private ImageStatus preview( String uri )
{
	
	hasUriImagePreviewed=true;	
	try
	{
		URL url = new URL( uri );
		// there's no need to enable the ok button when processing
		url.getContent( );
		if ( previewCanvas.loadImage( url ) != null )
		{
			emptyExceptionText();
		}
		return ImageStatus.IMAGE_CAN_DISPLAY;
	}
	catch (MalformedURLException malEx){
		showMessage(Messages.getString( "MarkerIconDialog.Exception.InvalidURL" ));//$NON-NLS-1$
		return ImageStatus.IMAGE_URL_INVALID;
	}	
	catch ( Exception ex )
	{				
		showMessage(Messages.getString("MarkerIconDialog.Exception.ImageNotAvailable"));  //$NON-NLS-1$
		return ImageStatus.IMAGE_CANNOT_DISPLAY;
	}		
}
 
Example 4
Source File: Beans.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public synchronized Image getImage(URL url) {
    Object o = imageCache.get(url);
    if (o != null) {
        return (Image)o;
    }
    try {
        o = url.getContent();
        if (o == null) {
            return null;
        }
        if (o instanceof Image) {
            imageCache.put(url, o);
            return (Image) o;
        }
        // Otherwise it must be an ImageProducer.
        Image img = target.createImage((java.awt.image.ImageProducer)o);
        imageCache.put(url, img);
        return img;

    } catch (Exception ex) {
        return null;
    }
}
 
Example 5
Source File: Beans.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioClip getAudioClip(URL url) {
    // We don't currently support audio clips in the Beans.instantiate
    // applet context, unless by some luck there exists a URL content
    // class that can generate an AudioClip from the audio URL.
    try {
        return (AudioClip) url.getContent();
    } catch (Exception ex) {
        return null;
    }
}
 
Example 6
Source File: SimpleBeanInfo.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This is a utility method to help in loading icon images.
 * It takes the name of a resource file associated with the
 * current object's class file and loads an image object
 * from that file.  Typically images will be GIFs.
 * <p>
 * @param resourceName  A pathname relative to the directory
 *          holding the class file of the current class.  For example,
 *          "wombat.gif".
 * @return  an image object.  May be null if the load failed.
 */
public Image loadImage(final String resourceName) {
    try {
        final URL url = getClass().getResource(resourceName);
        if (url != null) {
            final ImageProducer ip = (ImageProducer) url.getContent();
            if (ip != null) {
                return Toolkit.getDefaultToolkit().createImage(ip);
            }
        }
    } catch (final Exception ignored) {
    }
    return null;
}
 
Example 7
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "urls")
public void testGetContent(String urlString, boolean exists) throws Exception {
    URL url = new URL(urlString);
    try {
        Object obj = url.getContent();
        assertTrue(obj != null);
        if (!exists) fail("IOException expected");
    } catch (IOException ioe) {
        if (exists) fail("IOException not expected");
    }
}
 
Example 8
Source File: SimpleBeanInfo.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This is a utility method to help in loading icon images.
 * It takes the name of a resource file associated with the
 * current object's class file and loads an image object
 * from that file.  Typically images will be GIFs.
 * <p>
 * @param resourceName  A pathname relative to the directory
 *          holding the class file of the current class.  For example,
 *          "wombat.gif".
 * @return  an image object.  May be null if the load failed.
 */
public Image loadImage(final String resourceName) {
    try {
        final URL url = getClass().getResource(resourceName);
        if (url != null) {
            final ImageProducer ip = (ImageProducer) url.getContent();
            if (ip != null) {
                return Toolkit.getDefaultToolkit().createImage(ip);
            }
        }
    } catch (final Exception ignored) {
    }
    return null;
}
 
Example 9
Source File: Beans.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public AudioClip getAudioClip(URL url) {
    // We don't currently support audio clips in the Beans.instantiate
    // applet context, unless by some luck there exists a URL content
    // class that can generate an AudioClip from the audio URL.
    try {
        return (AudioClip) url.getContent();
    } catch (Exception ex) {
        return null;
    }
}
 
Example 10
Source File: Beans.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public AudioClip getAudioClip(URL url) {
    // We don't currently support audio clips in the Beans.instantiate
    // applet context, unless by some luck there exists a URL content
    // class that can generate an AudioClip from the audio URL.
    try {
        return (AudioClip) url.getContent();
    } catch (Exception ex) {
        return null;
    }
}
 
Example 11
Source File: Beans.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioClip getAudioClip(URL url) {
    // We don't currently support audio clips in the Beans.instantiate
    // applet context, unless by some luck there exists a URL content
    // class that can generate an AudioClip from the audio URL.
    try {
        return (AudioClip) url.getContent();
    } catch (Exception ex) {
        return null;
    }
}
 
Example 12
Source File: SimpleBeanInfo.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This is a utility method to help in loading icon images.
 * It takes the name of a resource file associated with the
 * current object's class file and loads an image object
 * from that file.  Typically images will be GIFs.
 * <p>
 * @param resourceName  A pathname relative to the directory
 *          holding the class file of the current class.  For example,
 *          "wombat.gif".
 * @return  an image object.  May be null if the load failed.
 */
public Image loadImage(final String resourceName) {
    try {
        final URL url = getClass().getResource(resourceName);
        if (url != null) {
            final ImageProducer ip = (ImageProducer) url.getContent();
            if (ip != null) {
                return Toolkit.getDefaultToolkit().createImage(ip);
            }
        }
    } catch (final Exception ignored) {
    }
    return null;
}
 
Example 13
Source File: SimpleBeanInfo.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * This is a utility method to help in loading icon images. It takes the
 * name of a resource file associated with the current object's class file
 * and loads an image object from that file. Typically images will be GIFs.
 *
 * @param  resourceName A pathname relative to the directory holding the
 *         class file of the current class. For example, "wombat.gif".
 * @return an image object or null if the resource is not found or the
 *         resource could not be loaded as an Image
 */
public Image loadImage(final String resourceName) {
    try {
        final URL url = getClass().getResource(resourceName);
        if (url != null) {
            final ImageProducer ip = (ImageProducer) url.getContent();
            if (ip != null) {
                return Toolkit.getDefaultToolkit().createImage(ip);
            }
        }
    } catch (final Exception ignored) {
    }
    return null;
}
 
Example 14
Source File: SimpleBeanInfo.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * This is a utility method to help in loading icon images.
 * It takes the name of a resource file associated with the
 * current object's class file and loads an image object
 * from that file.  Typically images will be GIFs.
 * <p>
 * @param resourceName  A pathname relative to the directory
 *          holding the class file of the current class.  For example,
 *          "wombat.gif".
 * @return  an image object.  May be null if the load failed.
 */
public Image loadImage(final String resourceName) {
    try {
        final URL url = getClass().getResource(resourceName);
        if (url != null) {
            final ImageProducer ip = (ImageProducer) url.getContent();
            if (ip != null) {
                return Toolkit.getDefaultToolkit().createImage(ip);
            }
        }
    } catch (final Exception ignored) {
    }
    return null;
}
 
Example 15
Source File: SimpleBeanInfo.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This is a utility method to help in loading icon images.
 * It takes the name of a resource file associated with the
 * current object's class file and loads an image object
 * from that file.  Typically images will be GIFs.
 * <p>
 * @param resourceName  A pathname relative to the directory
 *          holding the class file of the current class.  For example,
 *          "wombat.gif".
 * @return  an image object.  May be null if the load failed.
 */
public Image loadImage(final String resourceName) {
    try {
        final URL url = getClass().getResource(resourceName);
        if (url != null) {
            final ImageProducer ip = (ImageProducer) url.getContent();
            if (ip != null) {
                return Toolkit.getDefaultToolkit().createImage(ip);
            }
        }
    } catch (final Exception ignored) {
    }
    return null;
}
 
Example 16
Source File: Beans.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioClip getAudioClip(URL url) {
    // We don't currently support audio clips in the Beans.instantiate
    // applet context, unless by some luck there exists a URL content
    // class that can generate an AudioClip from the audio URL.
    try {
        return (AudioClip) url.getContent();
    } catch (Exception ex) {
        return null;
    }
}
 
Example 17
Source File: Beans.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public AudioClip getAudioClip(URL url) {
    // We don't currently support audio clips in the Beans.instantiate
    // applet context, unless by some luck there exists a URL content
    // class that can generate an AudioClip from the audio URL.
    try {
        return (AudioClip) url.getContent();
    } catch (Exception ex) {
        return null;
    }
}
 
Example 18
Source File: SimpleBeanInfo.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * This is a utility method to help in loading icon images.
 * It takes the name of a resource file associated with the
 * current object's class file and loads an image object
 * from that file.  Typically images will be GIFs.
 * <p>
 * @param resourceName  A pathname relative to the directory
 *          holding the class file of the current class.  For example,
 *          "wombat.gif".
 * @return  an image object.  May be null if the load failed.
 */
public Image loadImage(final String resourceName) {
    try {
        final URL url = getClass().getResource(resourceName);
        if (url != null) {
            final ImageProducer ip = (ImageProducer) url.getContent();
            if (ip != null) {
                return Toolkit.getDefaultToolkit().createImage(ip);
            }
        }
    } catch (final Exception ignored) {
    }
    return null;
}
 
Example 19
Source File: Beans.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public AudioClip getAudioClip(URL url) {
    // We don't currently support audio clips in the Beans.instantiate
    // applet context, unless by some luck there exists a URL content
    // class that can generate an AudioClip from the audio URL.
    try {
        return (AudioClip) url.getContent();
    } catch (Exception ex) {
        return null;
    }
}
 
Example 20
Source File: GetContentType.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {
    URL url = ClassLoader.getSystemResource("foo/bar");
    InputStream is = (InputStream) url.getContent();
    if (is == null)
        throw new Exception("Failed to get content.");
}