コード例 #1
0
  public static void main(String[] args) throws Exception {
    ByteArrayOutputStream bout;
    ByteArrayInputStream bin;
    ObjectOutputStream oout;
    ObjectInputStream oin;
    FileInputStream fin;
    File foof;
    CustomOutputStream cout;
    CustomInputStream cin;

    // test for backwards compatibility
    bout = new ByteArrayOutputStream();
    foof = new File(System.getProperty("test.src", "."), "Foo.ser");
    fin = new FileInputStream(foof);
    while (fin.available() > 0) bout.write(fin.read());
    byte[] buf1 = bout.toByteArray();

    bout = new ByteArrayOutputStream();
    oout = new ObjectOutputStream(bout);
    Foo foo = new Foo();
    oout.writeObject(foo);
    oout.flush();
    byte[] buf2 = bout.toByteArray();

    if (!Arrays.equals(buf1, buf2)) throw new Error("Incompatible stream format (write)");

    fin = new FileInputStream(foof);
    oin = new ObjectInputStream(fin);
    Foo foocopy = (Foo) oin.readObject();
    if (!foo.equals(foocopy)) throw new Error("Incompatible stream format (read)");

    // make sure write hook not called when old protocol in use
    bout = new ByteArrayOutputStream();
    cout = new CustomOutputStream(bout);
    cout.useProtocolVersion(PROTOCOL_VERSION_1);
    cout.writeObject(foo);
    if (cout.hookCalled) throw new Error("write descriptor hook should not be called");

    // write custom class descriptor representations
    bout = new ByteArrayOutputStream();
    cout = new CustomOutputStream(bout);
    cout.writeObject(foo);
    cout.flush();
    bin = new ByteArrayInputStream(bout.toByteArray());
    cin = new CustomInputStream(bin);
    foocopy = (Foo) cin.readObject();
    if (!cout.hookCalled) throw new Error("write descriptor hook never called");
    if (!cin.hookCalled) throw new Error("read descriptor hook never called");
    if (!foo.equals(foocopy)) throw new Error("serialization failed when hooks active");
  }
コード例 #2
0
ファイル: Ex6_1.java プロジェクト: daherr/examples
  /** @param args the command line arguments */
  public static void main(String[] args) {
    int foo = 6;
    // String someString = null;
    String someString;
    String otherString;
    Foo f1, f2, f3;

    f1 = new Foo(10);
    f2 = f1;

    someString = "some string here, yo!";
    otherString = "yo yo yo it's all about the bass fishing";

    System.out.println("foo = " + foo);
    method();
    System.out.println("foo = " + foo);

    System.out.println("someString = " + someString);
    System.out.println("length of someString = " + someString.length());
    System.out.println("otherString = " + otherString);

    otherString = someString;
    System.out.println("otherString = " + otherString);
    otherString = otherString.concat(someString);
    System.out.println("otherString = " + otherString);
    System.out.println("someString = " + someString);

    System.out.println("f1.foo = " + f1.getFoo());
    System.out.println("f2.foo = " + f2.getFoo());

    f1.setFoo(30);
    System.out.println("f1.foo = " + f1.getFoo());
    System.out.println("f2.foo = " + f2.getFoo());

    if (f1 == f2) {
      System.out.println("f1 and f2 are the same object");
    }

    f2 = new Foo(30);

    f3 = new Foo(30);

    if (f1 == f3) {
      System.out.println("f1 and f3 are the same object");
    }

    if (f1.equals(f3)) {
      System.out.println("f1 and f3 have equivalent data in them");
    }
  }