Ejemplo n.º 1
0
 /**
  * delivers file contents and directory listing for a ZIP/JAR file into sb
  *
  * @param name
  * @param header
  * @param fileData
  * @return name of entry
  */
 private String getObjectAsSections(String name, String header, Map<String, String> fileData) {
   if (name == null) return null;
   String[] subFileList = null;
   boolean asBinaryString = false;
   String name0 = name.replace('\\', '/');
   if (name.indexOf(":asBinaryString") >= 0) {
     asBinaryString = true;
     name = name.substring(0, name.indexOf(":asBinaryString"));
   }
   SB sb = null;
   if (fileData.containsKey(name0)) return name0;
   if (name.indexOf("#JMOL_MODEL ") >= 0) {
     fileData.put(name0, name0 + "\n");
     return name0;
   }
   String fullName = name;
   if (name.indexOf("|") >= 0) {
     subFileList = PT.split(name, "|");
     name = subFileList[0];
   }
   BufferedInputStream bis = null;
   try {
     Object t =
         getBufferedInputStreamOrErrorMessageFromName(name, fullName, false, false, null, false);
     if (t instanceof String) {
       fileData.put(name0, (String) t + "\n");
       return name0;
     }
     bis = (BufferedInputStream) t;
     if (JmolBinary.isCompoundDocumentS(bis)) {
       JmolDocument doc = (JmolDocument) Interface.getOptionInterface("io2.CompoundDocument");
       doc.setStream(bis, true);
       doc.getAllDataMapped(name.replace('\\', '/'), "Molecule", fileData);
     } else if (JmolBinary.isZipS(bis)) {
       JmolBinary.getAllZipData(bis, subFileList, name.replace('\\', '/'), "Molecule", fileData);
     } else if (asBinaryString) {
       // used for Spartan binary file reading
       JmolDocument bd = (JmolDocument) Interface.getOptionInterface("io2.BinaryDocument");
       bd.setStream(bis, false);
       sb = new SB();
       // note -- these headers must match those in ZipUtil.getAllData and
       // CompoundDocument.getAllData
       if (header != null) sb.append("BEGIN Directory Entry " + name0 + "\n");
       try {
         while (true) sb.append(Integer.toHexString(bd.readByte() & 0xFF)).appendC(' ');
       } catch (Exception e1) {
         sb.appendC('\n');
       }
       if (header != null) sb.append("\nEND Directory Entry " + name0 + "\n");
       fileData.put(name0, sb.toString());
     } else {
       BufferedReader br =
           JmolBinary.getBufferedReader(
               JmolBinary.isGzipS(bis)
                   ? new BufferedInputStream(JmolBinary.newGZIPInputStream(bis))
                   : bis,
               null);
       String line;
       sb = new SB();
       if (header != null) sb.append("BEGIN Directory Entry " + name0 + "\n");
       while ((line = br.readLine()) != null) {
         sb.append(line);
         sb.appendC('\n');
       }
       br.close();
       if (header != null) sb.append("\nEND Directory Entry " + name0 + "\n");
       fileData.put(name0, sb.toString());
     }
   } catch (Exception ioe) {
     fileData.put(name0, ioe.toString());
   }
   if (bis != null)
     try {
       bis.close();
     } catch (Exception e) {
       //
     }
   if (!fileData.containsKey(name0)) fileData.put(name0, "FILE NOT FOUND: " + name0 + "\n");
   return name0;
 }
Ejemplo n.º 2
0
  public Object getUnzippedReaderOrStreamFromName(
      String name,
      byte[] bytes,
      boolean allowZipStream,
      boolean forceInputStream,
      boolean isTypeCheckOnly,
      boolean doSpecialLoad,
      Map<String, Object> htParams) {
    String[] subFileList = null;
    String[] info = (bytes == null && doSpecialLoad ? getSpartanFileList(name) : null);
    String name00 = name;
    if (info != null) {
      if (isTypeCheckOnly) return info;
      if (info[2] != null) {
        String header = info[1];
        Map<String, String> fileData = new Hashtable<String, String>();
        if (info.length == 3) {
          // we need information from the output file, info[2]
          String name0 = getObjectAsSections(info[2], header, fileData);
          fileData.put("OUTPUT", name0);
          info = JmolBinary.spartanFileList(name, fileData.get(name0));
          if (info.length == 3) {
            // might have a second option
            name0 = getObjectAsSections(info[2], header, fileData);
            fileData.put("OUTPUT", name0);
            info = JmolBinary.spartanFileList(info[1], fileData.get(name0));
          }
        }
        // load each file individually, but return files IN ORDER
        SB sb = new SB();
        if (fileData.get("OUTPUT") != null) sb.append(fileData.get(fileData.get("OUTPUT")));
        String s;
        for (int i = 2; i < info.length; i++) {
          name = info[i];
          name = getObjectAsSections(name, header, fileData);
          Logger.info("reading " + name);
          s = fileData.get(name);
          sb.append(s);
        }
        s = sb.toString();
        if (spardirCache == null) spardirCache = new Hashtable<String, byte[]>();
        spardirCache.put(name00.replace('\\', '/'), s.getBytes());
        return JmolBinary.getBR(s);
      }
      // continuing...
      // here, for example, for an SPT file load that is not just a type check
      // (type check is only for application file opening and drag-drop to
      // determine if
      // script or load command should be used)
    }

    if (bytes == null && pngjCache != null) {
      bytes = JmolBinary.getCachedPngjBytes(this, name);
      if (bytes != null && htParams != null) htParams.put("sourcePNGJ", Boolean.TRUE);
    }
    String fullName = name;
    if (name.indexOf("|") >= 0) {
      subFileList = PT.split(name, "|");
      if (bytes == null) Logger.info("FileManager opening 3 " + name);
      name = subFileList[0];
    }
    Object t =
        (bytes == null
            ? getBufferedInputStreamOrErrorMessageFromName(
                name, fullName, true, false, null, !forceInputStream)
            : JmolBinary.getBIS(bytes));
    try {
      if (t instanceof String) return t;
      if (t instanceof BufferedReader) return t;
      BufferedInputStream bis = JmolBinary.getUnzippedInputStream((BufferedInputStream) t);
      if (JmolBinary.isCompoundDocumentS(bis)) {
        JmolDocument doc = (JmolDocument) Interface.getOptionInterface("io2.CompoundDocument");
        doc.setStream(bis, true);
        return JmolBinary.getBR(doc.getAllDataFiles("Molecule", "Input").toString());
      }
      if (JmolBinary.isPickleS(bis)) return bis;
      bis = JmolBinary.checkPngZipStream(bis);
      if (JmolBinary.isZipS(bis)) {
        if (allowZipStream) return JmolBinary.newZipInputStream(bis);
        Object o = JmolBinary.getZipFileContents(bis, subFileList, 1, forceInputStream);
        return (o instanceof String ? JmolBinary.getBR((String) o) : o);
      }
      return (forceInputStream ? bis : JmolBinary.getBufferedReader(bis, null));
    } catch (Exception ioe) {
      return ioe.toString();
    }
  }