예제 #1
0
  public void testCopyStream() throws Exception {
    int[] size = {
      100,
      4096,
      8000,
      1023,
      1024,
      1025,
      2047,
      4096 * 3,
      4096 * 6,
      4096 * 6 - 1,
      4096 * 256 * 3,
      4096 * 256 * 6
    };
    for (int i = 0; i < size.length; i++) {
      byte[] data = new byte[size[i]];
      for (int j = 0; j < data.length; j++) data[j] = (byte) j;

      InputStream ins = new ByteArrayInputStream(data);
      ByteArrayOutputStream outs = new ByteArrayOutputStream();
      ClassPoolTail.copyStream(ins, outs);
      byte[] data2 = outs.toByteArray();
      if (data2.length != data.length) throw new Exception("bad size");

      for (int k = 0; k < data.length; k++)
        if (data[k] != data2[k]) throw new Exception("bad element");
    }
  }
예제 #2
0
  /**
   * Finds the specified class using <code>ClassPath</code>. If the source throws an exception, this
   * returns null.
   *
   * <p>This method can be overridden by a subclass of <code>Loader</code>. Note that the overridden
   * method must not throw an exception when it just fails to find a class file.
   *
   * @return null if the specified class could not be found.
   * @throws ClassNotFoundException if an exception is thrown while obtaining a class file.
   */
  protected Class findClass(String name) throws ClassNotFoundException {
    byte[] classfile;
    try {
      if (source != null) {
        if (translator != null) translator.onLoad(source, name);

        try {
          classfile = source.get(name).toBytecode();
        } catch (NotFoundException e) {
          return null;
        }
      } else {
        String jarname = "/" + name.replace('.', '/') + ".class";
        InputStream in = this.getClass().getResourceAsStream(jarname);
        if (in == null) return null;

        classfile = ClassPoolTail.readStream(in);
      }
    } catch (Exception e) {
      throw new ClassNotFoundException(
          "caught an exception while obtaining a class file for " + name, e);
    }

    int i = name.lastIndexOf('.');
    if (i != -1) {
      String pname = name.substring(0, i);
      if (getPackage(pname) == null)
        try {
          definePackage(pname, null, null, null, null, null, null, null);
        } catch (IllegalArgumentException e) {
          // ignore.  maybe the package object for the same
          // name has been created just right away.
        }
    }

    if (domain == null) return defineClass(name, classfile, 0, classfile.length);
    else return defineClass(name, classfile, 0, classfile.length, domain);
  }