* The AssetInfo.openStream() must create a new stream each time (required by javadoc/spec) git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8592 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
d8682c94c3
commit
177c88d645
@ -0,0 +1,66 @@ |
||||
package com.jme3.asset.plugins; |
||||
|
||||
import com.jme3.asset.AssetInfo; |
||||
import com.jme3.asset.AssetKey; |
||||
import com.jme3.asset.AssetLoadException; |
||||
import com.jme3.asset.AssetManager; |
||||
import com.jme3.asset.AssetNotFoundException; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
|
||||
/** |
||||
* Handles loading of assets from a URL |
||||
* |
||||
* @author Kirill Vainer |
||||
*/ |
||||
public class UrlAssetInfo extends AssetInfo { |
||||
|
||||
private URL url; |
||||
private InputStream in; |
||||
|
||||
public static UrlAssetInfo create(AssetManager assetManager, AssetKey key, URL url) throws IOException { |
||||
// Check if URL can be reached. This will throw
|
||||
// IOException which calling code will handle.
|
||||
URLConnection conn = url.openConnection(); |
||||
conn.setUseCaches(false); |
||||
InputStream in = conn.getInputStream(); |
||||
|
||||
// For some reason url cannot be reached?
|
||||
if (in == null){ |
||||
return null; |
||||
}else{ |
||||
return new UrlAssetInfo(assetManager, key, url, in); |
||||
} |
||||
} |
||||
|
||||
private UrlAssetInfo(AssetManager assetManager, AssetKey key, URL url, InputStream in) throws IOException { |
||||
super(assetManager, key); |
||||
this.url = url; |
||||
this.in = in; |
||||
} |
||||
|
||||
public boolean hasInitialConnection(){ |
||||
return in != null; |
||||
} |
||||
|
||||
@Override |
||||
public InputStream openStream() { |
||||
if (in != null){ |
||||
// Reuse the already existing stream (only once)
|
||||
InputStream in2 = in; |
||||
in = null; |
||||
return in2; |
||||
}else{ |
||||
// Create a new stream for subsequent invocations.
|
||||
try { |
||||
URLConnection conn = url.openConnection(); |
||||
conn.setUseCaches(false); |
||||
return conn.getInputStream(); |
||||
} catch (IOException ex) { |
||||
throw new AssetLoadException("Failed to read URL " + url, ex); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue