7

How can I convert an inputStream to a URL? Here is my code:

InputStream song1 = this.getClass().getClassLoader().getResourceAsStream("/songs/BrokenAngel.mp3");
URL song1Url = song1.toUrl(); //<- pseudo code
MediaLocator ml = new MediaLocator(song1Url);
Player p;
try {
    p = Manager.createPlayer(ml);
    p.start();
} catch (NoPlayerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Meesh
  • 379
  • 1
  • 13
sanchixx
  • 275
  • 2
  • 5
  • 12

5 Answers5

4

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.

  1. 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.

  2. 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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • But then i get a nullpointer exeption: `Exception in thread "main" java.lang.NullPointerException at javax.media.MediaLocator.(MediaLocator.java:37) at com.ymail.sanchixx.sanchixx.Interface.initComposant(Interface.java:39) at com.ymail.sanchixx.sanchixx.Interface.(Interface.java:31) at com.ymail.sanchixx.sanchixx.Main.main(Main.java:8)` – sanchixx Oct 13 '13 at 18:29
  • Could you update your post, with the new code, so we can locate the new error? – vallentin Oct 13 '13 at 18:32
2

Note that this approach requires the mp3 to be within your application's sub-directory called songs. You can also use relative pathing for the /songs/BrokenAngel.mp3 part (../../ or something like that. But it takes your applications directory as base!

    File appDir = new File(System.getProperty("user.dir"));
    URI uri = new URI(appDir.toURI()+"/songs/BrokenAngel.mp3");
    // just to check if the file exists
    File file = new File(uri);
    System.out.println(file.exists())
    URL song1Url = uri.toURL();
Roman Vottner
  • 12,213
  • 5
  • 46
  • 63
1

I think what you want is ClassLoader.findResource(String)

That should return a properly formatted jar:// URL. I haven't tested it myself though, so beware

torquestomp
  • 3,304
  • 19
  • 26
0

Try this

URI uri = new URI("/songs/BrokenAngel.mp3");
URL song1Url = uri.toURL();

MediaLocator ml = new MediaLocator(song1Url);
Player p;
try {
    p = Manager.createPlayer(ml);
    p.start();
} catch (NoPlayerException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Chien Cao
  • 1
  • 2
  • `Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute at java.net.URI.toURL(Unknown Source) at com.ymail.sanchixx.sanchixx.Interface.initComposant(Interface.java:49) at com.ymail.sanchixx.sanchixx.Interface.(Interface.java:33) at com.ymail.sanchixx.sanchixx.Main.main(Main.java:8)` – sanchixx Oct 13 '13 at 18:37
-3

Try this

InputStream input = new URL("http://www.somewebsite.com/a.txt").openStream();
Scientist
  • 1,458
  • 2
  • 15
  • 31