Exemplo n.º 1
0
  public static void main(String[] args) throws ClassNotFoundException, IOException {

    /*
     * process command line arguments
     */

    if (args.length < 1) {
      usage();
      return;
    }

    boolean verbose = false;

    for (int i = 0; i < args.length; ++i) {
      String arg = args[i];
      if (arg.startsWith("-")) {
        switch (arg.charAt(1)) {
          case 'p':
            protocolVersion = Integer.decode(arg.substring(2)).intValue();
            break;

          case 's':
            serialize = true;
            serialStreamFileName = arg.substring(2);
            break;

          case 'd':
            deserialize = true;
            serialStreamFileName = arg.substring(2);
            break;

          case 'n':
            instances = Integer.decode(arg.substring(2)).intValue();
            instances = Math.min(instances, 1);
            break;

          case 'v':
            verbose = true;
            break;

          default:
            System.err.println("unknown option \"" + arg + "\"");
            usage();
            return;
        }
      } else {
        if (testClassName == null) testClassName = arg;
        else {
          System.err.println("invalid argument \"" + arg + "\"");
          return;
        }
      }
    }

    Class testClass = null;
    if (testClassName != null)
      try {
        testClass = Class.forName(testClassName);
      } catch (ClassNotFoundException e) {
      }

    if (serialStreamFileName == null) {
      // If no filename specified only makes sense to perform
      // both serialization and deserialization.
      serialize = deserialize = true;
    }

    /*
     * create object to be serialized
     */

    ByteArrayOutputStream baos = null;
    if (serialize) {
      OutputStream os = null;
      if (serialStreamFileName == null) {
        os = baos = new ByteArrayOutputStream(1024);
      } else os = new FileOutputStream(serialStreamFileName);

      try {
        Object obj;
        ObjectOutputStream out = new VerboseObjectOutputStream(os, verbose);
        try {
          out.useProtocolVersion(protocolVersion);
        } catch (NoSuchMethodError e) {
          // JVM 1.1 does not have this method.
        }
        if (testClass == null) {
          if (testClassName != null) throw new ClassNotFoundException(testClassName);
          else throw new Error("No classname provided to serialize");
        }
        for (int i = 0; i < instances; i++) {
          try {
            obj = testClass.newInstance();
            long start = System.currentTimeMillis();
            out.writeObject(obj);
            long duration = System.currentTimeMillis() - start;
            System.out.println(
                "Time " + duration + " millisecs " + "(" + (duration / 1000.0) + " secs)");
            System.out.println("Serialize " + obj.toString());
          } catch (IllegalAccessException e) {
            System.err.println("Class " + testClassName + " does not have a public constructor.");
            return;
          } catch (InstantiationException e) {
            System.err.println("Exception creating instance of " + testClassName + ":");
            e.printStackTrace();
            return;
          }
        }

        out.close();
      } catch (IOException e) {
        System.err.println("Exception occurred during serialization: ");
        e.printStackTrace();
        return;
      }
    }

    if (deserialize) {
      InputStream is = null;
      if (baos != null) {
        is = new ByteArrayInputStream(baos.toByteArray());
      } else if (serialStreamFileName != null) {
        is = new FileInputStream(serialStreamFileName);
      } else {
        System.err.println("No input stream specified on commandline.");
        return;
      }

      ObjectInputStream in = null;
      try {
        in = new VerboseObjectInputStream(is, verbose);
        long start = System.currentTimeMillis();
        for (int i = 0; i < instances; i++) {
          try {
            Object obj = in.readObject();
            System.out.println("DeSerialize " + obj.toString());
          } catch (OptionalDataException e) {
            if (!e.eof) System.out.println("Skipping " + e.length + " bytes");
            in.skip(e.length);
          }
        }
        long duration = System.currentTimeMillis() - start;
        System.out.println(
            "Time " + duration + " millisecs " + "(" + (duration / 1000.0) + " secs)");
      } catch (EOFException e) {
      } catch (IOException e) {
        System.err.println("Exception occurred reading object. ");
        e.printStackTrace();
      } finally {
        if (in != null) {
          in.close();
        }
      }
    }
    System.exit(0);
  }