java.net.URLConnection.getContent()

Alan Bateman alan.bateman at oracle.com
Sat Jun 14 07:21:28 UTC 2025


On 13/06/2025 23:11, Philip Race wrote:
> java.net.URLConnection has
> public Object getContent();
>
> It uses the desktop module to find handlers for image and audio data
>
> Briefly, the desktop module
>     "provides java.net.ContentHandlerFactory with
>         sun.awt.www.content.MultimediaContentHandlers;"
>
> That knows about several audio and image mime types.
>
> And URLConnection passes a mimetype string to the ContentHandlerFactory
>
> If it is one of the mimetypes known to the desktop provider 
> URLConnection gets returned one of
> URLImageSource
> java.awt.Image
> java.applet.AudioClip
>
> But the return type of getContent() is just java.lang.Object and 
> nothing is specified.
>
> How does anything use this API ?
>
> The reason this comes up is that when removing the Applet API, this 
> needs to transition to something
> other than AudioClip - but who would notice if it got back null 
> instead ? Or just a byte[] of the raw data ?
> Or something else ?
>
> Is this API actually used ? Or useful ?

This is a JDK 1.0 era API. The intent seems to be to test the return 
with instanceof to test for types that the caller can deal with. The 
1-arg overload allows the caller to provider an array of the types that 
it can deal with. Ironically, the addition of pattern matching for 
instanceof and other work on patterns makes it easier to use.

I did a quick search to see if there is a mapping/table anywhere on MIME 
type to Java class but don't see it.

In any case, with the removal of java.applet then it can't return an 
AudioClip, and existing code that expects an AudioClip won't compile or 
run. Looking at JavaSoundAudioClip then it doesn't look like there are 
other audio types implemented. I can't case to a SoundClip or other 
sound types, right? So I think dropping it should be okay, meaning null 
will be returned.

-Alan




More information about the net-dev mailing list