Example #1
0
 Map<String, Integer> cacheList() {
   Map<String, Integer> map = new Hashtable<String, Integer>();
   for (Map.Entry<String, Object> entry : cache.entrySet())
     map.put(
         entry.getKey(),
         Integer.valueOf(
             PT.isAB(entry.getValue())
                 ? ((byte[]) entry.getValue()).length
                 : entry.getValue().toString().length()));
   return map;
 }
Example #2
0
 Object getBufferedReaderOrErrorMessageFromName(
     String name, String[] fullPathNameReturn, boolean isBinary, boolean doSpecialLoad) {
   Object data = cacheGet(name, false);
   boolean isBytes = PT.isAB(data);
   byte[] bytes = (isBytes ? (byte[]) data : null);
   if (name.startsWith("cache://")) {
     if (data == null) return "cannot read " + name;
     if (isBytes) {
       bytes = (byte[]) data;
     } else {
       return JmolBinary.getBR((String) data);
     }
   }
   String[] names = classifyName(name, true);
   if (names == null) return "cannot read file name: " + name;
   if (fullPathNameReturn != null) fullPathNameReturn[0] = names[0].replace('\\', '/');
   return getUnzippedReaderOrStreamFromName(
       names[0], bytes, false, isBinary, false, doSpecialLoad, null);
 }
Example #3
0
 void loadImage(String name, String echoName) {
   Object image = null;
   Object info = null;
   String fullPathName = "";
   while (true) {
     if (name == null) break;
     String[] names = classifyName(name, true);
     if (names == null) {
       fullPathName = "cannot read file name: " + name;
       break;
     }
     GenericPlatform apiPlatform = viewer.apiPlatform;
     fullPathName = names[0].replace('\\', '/');
     if (fullPathName.indexOf("|") > 0) {
       Object ret = getFileAsBytes(fullPathName, null, true);
       if (!PT.isAB(ret)) {
         fullPathName = "" + ret;
         break;
       }
       image = (viewer.isJS ? ret : apiPlatform.createImage(ret));
     } else if (viewer.isJS) {
     } else if (urlTypeIndex(fullPathName) >= 0) {
       try {
         image = apiPlatform.createImage(new URL((URL) null, fullPathName, null));
       } catch (Exception e) {
         fullPathName = "bad URL: " + fullPathName;
         break;
       }
     } else {
       image = apiPlatform.createImage(fullPathName);
     }
     /** @j2sNative info = [echoName, fullPathName]; */
     {
       if (image == null) break;
     }
     try {
       if (!apiPlatform.waitForDisplay(info, image)) {
         image = null;
         break;
       }
       /**
        * note -- JavaScript just returns immediately, because we must wait for the image to load,
        * and it is single-threaded
        *
        * @j2sNative fullPathName = null; break;
        */
       {
         if (apiPlatform.getImageWidth(image) < 1) {
           fullPathName = "invalid or missing image " + fullPathName;
           image = null;
         }
         break;
       }
     } catch (Exception e) {
       System.out.println(e.toString());
       fullPathName = e.toString() + " opening 4 " + fullPathName;
       image = null;
       break;
     }
   }
   viewer.loadImageData(image, fullPathName, echoName, null);
 }
Example #4
0
 public Object getBufferedInputStreamOrErrorMessageFromName(
     String name,
     String fullName,
     boolean showMsg,
     boolean checkOnly,
     byte[] outputBytes,
     boolean allowReader) {
   byte[] cacheBytes = null;
   if (outputBytes == null) {
     cacheBytes =
         (fullName == null || pngjCache == null
             ? null
             : JmolBinary.getCachedPngjBytes(this, fullName));
     if (cacheBytes == null) cacheBytes = (byte[]) cacheGet(name, true);
   }
   BufferedInputStream bis = null;
   Object ret = null;
   String errorMessage = null;
   try {
     if (cacheBytes == null) {
       boolean isPngjBinaryPost = (name.indexOf("?POST?_PNGJBIN_") >= 0);
       boolean isPngjPost = (isPngjBinaryPost || name.indexOf("?POST?_PNGJ_") >= 0);
       if (name.indexOf("?POST?_PNG_") > 0 || isPngjPost) {
         String[] errMsg = new String[1];
         byte[] bytes = viewer.getImageAsBytes(isPngjPost ? "PNGJ" : "PNG", 0, 0, -1, errMsg);
         if (errMsg[0] != null) return errMsg[0];
         if (isPngjBinaryPost) {
           outputBytes = bytes;
           name = PT.simpleReplace(name, "?_", "=_");
         } else {
           name = new SB().append(name).append("=").appendSB(Base64.getBase64(bytes)).toString();
         }
       }
       int iurl = urlTypeIndex(name);
       boolean isURL = (iurl >= 0);
       String post = null;
       if (isURL && (iurl = name.indexOf("?POST?")) >= 0) {
         post = name.substring(iurl + 6);
         name = name.substring(0, iurl);
       }
       boolean isApplet = (appletDocumentBaseURL != null);
       if (name.indexOf(".png") >= 0 && pngjCache == null && viewer.cachePngFiles())
         JmolBinary.cachePngjFile(this, null);
       if (isApplet || isURL) {
         if (isApplet && isURL && appletProxy != null)
           name = appletProxy + "?url=" + urlEncode(name);
         URL url =
             (isApplet
                 ? new URL(appletDocumentBaseURL, name, null)
                 : new URL((URL) null, name, null));
         if (checkOnly) return null;
         name = url.toString();
         if (showMsg && name.toLowerCase().indexOf("password") < 0)
           Logger.info("FileManager opening 1 " + name);
         ret = viewer.apiPlatform.getBufferedURLInputStream(url, outputBytes, post);
         byte[] bytes = null;
         if (ret instanceof SB) {
           SB sb = (SB) ret;
           if (allowReader && !JmolBinary.isBase64(sb)) return JmolBinary.getBR(sb.toString());
           bytes = JmolBinary.getBytesFromSB(sb);
         } else if (PT.isAB(ret)) {
           bytes = (byte[]) ret;
         }
         if (bytes != null) ret = JmolBinary.getBIS(bytes);
       } else if ((cacheBytes = (byte[]) cacheGet(name, true)) == null) {
         if (showMsg) Logger.info("FileManager opening 2 " + name);
         ret = viewer.apiPlatform.getBufferedFileInputStream(name);
       }
       if (ret instanceof String) return ret;
     }
     bis = (cacheBytes == null ? (BufferedInputStream) ret : JmolBinary.getBIS(cacheBytes));
     if (checkOnly) {
       bis.close();
       bis = null;
     }
     return bis;
   } catch (Exception e) {
     try {
       if (bis != null) bis.close();
     } catch (IOException e1) {
     }
     errorMessage = "" + e;
   }
   return errorMessage;
 }