Beispiel #1
0
  /** Try and open the file as a resource from a jar file. */
  private static InputStream openResource(String resource) {
    // netscape requires that you must get resources in this
    // fashion. getSystemResourceAsStream will always fail.
    // this method will also fail if we try and open
    // a file that has an extension that isn't in the approved
    // list... in a word - ridiculous.
    FILE dummy = new FILE();

    InputStream inputStream = dummy.getClass().getResourceAsStream(resource);

    return inputStream;
  }
Beispiel #2
0
  /** Read the data from the specified FILE object. */
  public static int readFrom(FILE file) {
    int bytesRead = 0;

    while (file.read() != FILE.EOF) {
      bytesRead++;
    }

    return bytesRead;
  }
Beispiel #3
0
  /** Opens outputstream for writing. */
  public static FILE write(String file) {
    // see if we could shuffle the files
    // if not we can't open the file
    if (!shuffleVersions(file)) {
      return null;
    }

    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);
      FILE output = new FILE();
      output.outputStream = fileOutputStream;
      output.setCharactersInBuffer(0);
      return output;
    } catch (Exception e) {
      setException(e);
      return null;
    }
  }
Beispiel #4
0
  /** Convenience method to load a properties file. */
  public static Properties loadProperties(String resource) {
    Properties properties = new Properties();

    FILE propertyStream = open(resource);

    if (debug) {
      System.out.println("loadProperties from " + propertyStream);
    }

    if (propertyStream != null) {
      try {
        properties.load(propertyStream);
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        propertyStream.close();
      }
    }

    return properties;
  }
Beispiel #5
0
  /**
   * Return a FILE object for reading the specified resource.
   *
   * <p>If the resource looks like it is a URL name (begins with something like 'http:' or 'ftp:')
   * then an attempt will be made to open it as a URL. Otherwise the resource will be opened as a
   * file.
   */
  public static FILE open(String resource) {
    FILE input = null;

    if (debug) {
      System.out.println("FILE.open tryfiles=" + tryFiles);
      System.out.println("FILE.open resource=" + resource);
    }

    if (input == null
        && (resource.endsWith(".properties")
            || resource.endsWith(".gif")
            || resource.endsWith(".jpg")
            || resource.endsWith(".jpeg"))) {
      // instantiate a file so we can use
      // getResourceAsStream()
      // one last chance it might be in a jar file
      InputStream inputStream = null;

      if (debug) {
        System.out.println("attempt to open as absolute resource");
      }
      inputStream = openResource("/" + resource);

      if (inputStream == null) {
        if (debug) {
          System.out.println("attempt to open as relative resource");
        }
        inputStream = openResource(resource);
      }

      if (inputStream != null) {
        if (debug) {
          System.out.println("opened as resource");
        }
        input = new FILE(inputStream);
      }
    }

    if (input == null && documentBase != null) {
      if (debug) {
        System.out.println("documentBase " + documentBase);
      }

      try {
        URL url = new URL(documentBase, resource);
        if (debug) {
          System.out.println("attempting to open url " + url);
        }
        input = open(url);
        if (debug && input != null) {
          System.out.println("url successfully opened");
        }
      } catch (Exception e) {
        if (debug) {
          System.out.println("**** URL exception " + e);
        }
      }
    }

    if (input == null && codeBase != null) {
      if (debug) {
        System.out.println("codeBase " + codeBase);
      }

      try {
        URL url = new URL(codeBase, resource);
        if (debug) {
          System.out.println("attempting to open url " + url);
        }
        input = open(url);
        if (debug && input != null) {
          System.out.println("url successfully opened");
        }
      } catch (Exception e) {
        if (debug) {
          System.out.println("**** URL exception " + e);
        }
      }
    }

    if (input == null && stringIsURL(resource)) {
      try {
        if (debug) {
          System.out.println("attempting to open as absolute url");
        }
        input = openURL(resource);
        if (debug && input != null) {
          System.out.println("successfully opened as absolute url");
        }
      } catch (Exception e) {
        input = null;
        if (debug) {
          System.out.println("**** absolute url exception " + e);
        }
      }
    }

    if (input == null && tryFiles) {
      try {
        if (debug) {
          System.out.println("attempting to open as file");
        }
        input = openFile(resource);
        if (debug && input != null) {
          System.out.println("successfully opened as file");
        }
      } catch (Exception e) {
        input = null;
        if (debug) {
          System.out.println("**** open file exception " + e);
        }
      }
    }

    if (input != null && debug) {
      System.out.println("opened as resource");
    }

    if (resource.endsWith(".gz")) {
      try {
        GZIPInputStream gis = new GZIPInputStream(input.inputStream);

        input.inputStream = gis;
      } catch (Exception e) {
        input = null;
        if (debug) {
          System.out.println("failed to open gzip'ed inputStream");
          System.out.println(e);
        }
      }
    }

    // finally try and see if there is a resource with
    // .gz extension that corresponds to the resource
    // we were asked to open
    // i.e. we were asked to open 1abc.pdb, but it
    // didn't exist. Does 1abc.pdb.gz exist instead?
    if (input == null && !resource.endsWith(".gz")) {
      input = open(resource + ".gz");

      if (input != null) {
        return input;
      }
    }

    return input;
  }