I am not sure you really want to do this. If you need URL for your specific task just do the following:
URL url = this.getClass().getClassLoader().getResource("/songs/BrokenAngel.mp3");
If however you retrieve input stream in one part of you code, then pass it to another module and there want to find what was the source URL for this input stream it is "almost" impossible. The problem is that you get BufferedInputStream
that wraps FileInputStream
that does not store the information about it source. You cannot retrieve it even using reflection. If you really want to do this you can do the following.
implement you own UrlInputStream extends InputStream
the gets into constructor URL
, stores it in class varible, creates input stream by invocation of url.openStream()
and wraps it.
Now you can use your stream as usual input stream until you have to retrieve the URL. At this point you have to cast it to your UrlInputStream
and call getUrl()
method that you will implement.