From: <ma...@us...> - 2007-10-24 21:25:57
|
Revision: 2908 http://java-game-lib.svn.sourceforge.net/java-game-lib/?rev=2908&view=rev Author: matzon Date: 2007-10-24 14:25:54 -0700 (Wed, 24 Oct 2007) Log Message: ----------- reworked urlconnection.getInputStream to threaded usage, to avoid Opera issue (detective work: kappaOne) Modified Paths: -------------- trunk/LWJGL/src/java/org/lwjgl/util/applet/AppletLoader.java Modified: trunk/LWJGL/src/java/org/lwjgl/util/applet/AppletLoader.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/util/applet/AppletLoader.java 2007-10-24 21:23:32 UTC (rev 2907) +++ trunk/LWJGL/src/java/org/lwjgl/util/applet/AppletLoader.java 2007-10-24 21:25:54 UTC (rev 2908) @@ -44,6 +44,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Method; @@ -468,7 +469,7 @@ state = STATE_CHECKING_CACHE; - percentage = 5; + percentage = 5; try { if(debugMode) { @@ -677,7 +678,7 @@ urlconnection = urlList[i].openConnection(); String currentFile = getFileName(urlList[i]); - InputStream inputstream = urlconnection.getInputStream(); + InputStream inputstream = getJarInputStream(currentFile, urlconnection); FileOutputStream fos = new FileOutputStream(path + currentFile); @@ -691,11 +692,62 @@ percentage = initialPercentage + ((currentSizeDownload * 55) / totalSizeDownload); subtaskMessage = "Retrieving: " + currentFile + " " + ((currentSizeDownload * 100) / totalSizeDownload) + "%"; } + + inputstream.close(); + fos.close(); } subtaskMessage = ""; } /** + * Retrieves a jar files input stream. This method exists primarily to fix an Opera hang in getInputStream + * @param urlconnection connection to get input stream from + * @return InputStream or null if not possible + */ + private InputStream getJarInputStream(final String currentFile, final URLConnection urlconnection) throws Exception { + final InputStream[] is = new InputStream[1]; + + // try to get the input stream 3 times. + // Wait at most 5 seconds before interrupting the thread + for (int j = 0; j < 3 && is[0] == null; j++) { + Thread t = new Thread() { + public void run() { + try { + is[0] = urlconnection.getInputStream(); + } catch (IOException e) { + /* ignored */ + } + } + }; + t.setName("JarInputStreamThread"); + t.start(); + + int iterationCount = 0; + while(is == null && iterationCount++ < 5) { + try { + t.join(1000); + } catch (InterruptedException inte) { + /* ignored */ + } + } + + try { + t.interrupt(); + t.join(); + } catch (InterruptedException inte) { + /* ignored */ + } + } + + if(is[0] == null) { + throw new Exception("Unable to get input stream for " + currentFile); + } + + + return is[0]; + } + + /** * This method will extract all file from the native jar and extract them * to the subdirectory called "natives" in the local path, will also check * to see if the native jar files is signed properly This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |