88import java .net .URI ;
99import java .net .URISyntaxException ;
1010import java .net .URL ;
11+ import java .util .ArrayList ;
1112import java .util .Arrays ;
1213import java .util .HashMap ;
1314import java .util .HashSet ;
15+ import java .util .List ;
1416import java .util .Map ;
1517import java .util .zip .ZipEntry ;
1618import java .util .zip .ZipInputStream ;
@@ -231,11 +233,16 @@ public static void add(String name, String zipFile, String path) {
231233 }
232234
233235 private static HashSet <String > loadedAssets = new HashSet <>();
236+
237+ private static boolean debugging ;
234238
235239 public static boolean hasLoaded (String name ) {
236240 return loadedAssets .contains (name );
237241 }
238242
243+ public static void setDebugging (boolean tf ) {
244+ debugging = tf ;
245+ }
239246 /**
240247 * Completely reset the assets data.
241248 *
@@ -254,10 +261,21 @@ public static void add(String name, String zipFile, String[] paths) {
254261 private void _add (String name , String zipFile , String [] paths ) {
255262 if (hasLoaded (name )) {
256263 System .err .println ("Assets warning: Asset " + name + " already exists" );
264+ List <String > toRemove = new ArrayList <>();
265+ for (String key : assetsByPath .keySet ()) {
266+ if (assetsByPath .get (key ).name .equals (name )) {
267+ toRemove .add (key );
268+ }
269+ }
270+ for (int i = 0 ; i < toRemove .size (); i ++) {
271+ System .err .println ("Assets warning: removing " + assetsByPath .get (toRemove .get (i )));
272+ assetsByPath .remove (toRemove .get (i ));
273+ }
257274 }
258275 loadedAssets .add (name );
259276 for (int i = paths .length ; --i >= 0 ;) {
260277 assetsByPath .put (paths [i ], new Asset (name , zipFile , paths [i ]));
278+ System .out .println ("Assets: adding " + assetsByPath .get (paths [i ]));
261279 }
262280 resort ();
263281 }
@@ -325,35 +343,39 @@ public static InputStream getAssetStreamFromZip(String assetPath) {
325343
326344
327345 /**
328- * Get the contents of a path from a zip file asset as byte[], optionally loading
329- * the resource directly using a class loader.
346+ * Get the contents of a path from a zip file asset as byte[], optionally
347+ * loading the resource directly using a class loader.
330348 *
331349 * @param path
332350 * @param zipOnly
333351 * @return
334352 */
335353 private static byte [] getAssetBytes (String path , boolean zipOnly ) {
336354 byte [] bytes = null ;
355+ URL url = null ;
337356 try {
338- URL url = getInstance ()._getURLFromPath (path , true );
357+ url = getInstance ()._getURLFromPath (path , true );
339358 if (url == null && !zipOnly ) {
340359 url = getAbsoluteURL (path );
341- //url = Assets.class.getResource(path);
360+ // url = Assets.class.getResource(path);
342361 }
343- if (url == null )
344- return null ;
345- if (isJS ) {
346- bytes = jsutil .getURLBytes (url );
347- if (bytes == null ) {
348- url .openStream ();
362+ if (url != null ) {
363+ if (isJS ) {
349364 bytes = jsutil .getURLBytes (url );
365+ if (bytes == null ) {
366+ url .openStream ();
367+ bytes = jsutil .getURLBytes (url );
368+ }
369+ } else {
370+ bytes = getLimitedStreamBytes (url .openStream (), -1 , null );
350371 }
351- } else {
352- bytes = getLimitedStreamBytes (url .openStream (), -1 , null );
353372 }
354373 } catch (Throwable t ) {
355374 t .printStackTrace ();
356375 }
376+ if (debugging ) {
377+ System .out .println ("Assets.getAssetBytes " + path + " " + url + (bytes == null ? " null" : " " + bytes .length + " bytes" ));
378+ }
357379 return bytes ;
358380 }
359381
@@ -371,24 +393,25 @@ private static String getAssetString(String path, boolean zipOnly) {
371393 }
372394
373395 /**
374- * Get the contents of a path from a zip file asset as an InputStream, optionally
375- * loading the resource directly using a class loader.
396+ * Get the contents of a path from a zip file asset as an InputStream,
397+ * optionally loading the resource directly using a class loader.
376398 *
377399 * @param path
378400 * @param zipOnly
379401 * @return
380402 */
381403 private static InputStream getAssetStream (String path , boolean zipOnly ) {
404+ URL url = null ;
405+ url = getInstance ()._getURLFromPath (path , true );
406+ if (url == null && !zipOnly ) {
407+ url = Assets .class .getClassLoader ().getResource (path );
408+ }
409+ InputStream is = null ;
382410 try {
383- URL url = getInstance ()._getURLFromPath (path , true );
384- if (url == null && !zipOnly ) {
385- url = Assets .class .getClassLoader ().getResource (path );
386- }
387- if (url != null )
388- return url .openStream ();
411+ is = url .openStream ();
389412 } catch (Throwable t ) {
390413 }
391- return null ;
414+ return is ;
392415 }
393416 /**
394417 * Determine the path to an asset. If not found in a zip file asset, return the
@@ -428,15 +451,18 @@ private URL _getURLFromPath(String fullPath, boolean zipOnly) {
428451 if (isJS ) {
429452 jsutil .setURLBytes (url , jsutil .getZipBytes (ze ));
430453 }
431- return url ;
454+ break ;
432455 }
433456 }
434457 }
435- if (!zipOnly )
436- return getAbsoluteURL ((fullPath .startsWith ("TEMP/" ) ? "/" + fullPath : fullPath ));
458+ if (url == null && !zipOnly )
459+ url = getAbsoluteURL ((fullPath .startsWith ("TEMP/" ) ? "/" + fullPath : fullPath ));
437460 } catch (MalformedURLException e ) {
438461 }
439- return null ;
462+ if (debugging ) {
463+ System .out .println ("Assets.getURLFromPath " + url );
464+ }
465+ return url ;
440466 }
441467
442468 public static ZipEntry findZipEntry (URL url ) {
@@ -583,7 +609,7 @@ private void resort() {
583609 * @return
584610 * @throws IOException
585611 */
586- private static byte [] getLimitedStreamBytes (InputStream is , long n , OutputStream out ) throws IOException {
612+ private static byte [] getLimitedStreamBytes (InputStream is , int n , OutputStream out ) throws IOException {
587613
588614 // Note: You cannot use InputStream.available() to reliably read
589615 // zip data from the web.
0 commit comments