test/jdk/java/io/Serializable/classDescHooks/ExternLoopback.java
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
equal deleted inserted replaced
58564:218a1a642c6f 58565:baa5969ecf34
     1 /*
     1 /*
     2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    30 
    30 
    31 import java.io.*;
    31 import java.io.*;
    32 import java.util.*;
    32 import java.util.*;
    33 
    33 
    34 class LoopbackOutputStream extends ObjectOutputStream {
    34 class LoopbackOutputStream extends ObjectOutputStream {
    35     LinkedList descs;
    35     LinkedList<ObjectStreamClass> descs;
    36 
    36 
    37     LoopbackOutputStream(OutputStream out, LinkedList descs)
    37     LoopbackOutputStream(OutputStream out, LinkedList<ObjectStreamClass> descs)
    38         throws IOException
    38         throws IOException
    39     {
    39     {
    40         super(out);
    40         super(out);
    41         this.descs = descs;
    41         this.descs = descs;
    42     }
    42     }
    47         descs.add(desc);
    47         descs.add(desc);
    48     }
    48     }
    49 }
    49 }
    50 
    50 
    51 class LoopbackInputStream extends ObjectInputStream {
    51 class LoopbackInputStream extends ObjectInputStream {
    52     LinkedList descs;
    52     LinkedList<ObjectStreamClass> descs;
    53 
    53 
    54     LoopbackInputStream(InputStream in, LinkedList descs) throws IOException {
    54     LoopbackInputStream(InputStream in, LinkedList<ObjectStreamClass> descs) throws IOException {
    55         super(in);
    55         super(in);
    56         this.descs = descs;
    56         this.descs = descs;
    57     }
    57     }
    58 
    58 
    59     protected ObjectStreamClass readClassDescriptor()
    59     protected ObjectStreamClass readClassDescriptor()
    60         throws IOException, ClassNotFoundException
    60         throws IOException, ClassNotFoundException
    61     {
    61     {
    62         return (ObjectStreamClass) descs.removeFirst();
    62         return descs.removeFirst();
    63     }
    63     }
    64 }
    64 }
    65 
    65 
    66 public class ExternLoopback implements Externalizable {
    66 public class ExternLoopback implements Externalizable {
       
    67     private static final long serialVersionUID = 1L;
    67 
    68 
    68     String a, b, c;
    69     String a, b, c;
    69 
    70 
    70     public ExternLoopback() {
    71     public ExternLoopback() {
    71     }
    72     }
    98         }
    99         }
    99         ExternLoopback other = (ExternLoopback) obj;
   100         ExternLoopback other = (ExternLoopback) obj;
   100         return streq(a, other.a) && streq(b, other.b) && streq(c, other.c);
   101         return streq(a, other.a) && streq(b, other.b) && streq(c, other.c);
   101     }
   102     }
   102 
   103 
       
   104     public int hashCode() {
       
   105         return a.hashCode();
       
   106     }
       
   107 
   103     static boolean streq(String s1, String s2) {
   108     static boolean streq(String s1, String s2) {
   104         return (s1 != null) ? s1.equals(s2) : (s2 == null);
   109         return (s1 != null) ? s1.equals(s2) : (s2 == null);
   105     }
   110     }
   106 
   111 
   107     public static void main(String[] args) throws Exception {
   112     public static void main(String[] args) throws Exception {
   108         ExternLoopback lb = new ExternLoopback("foo", "bar", "baz");
   113         ExternLoopback lb = new ExternLoopback("foo", "bar", "baz");
   109         LinkedList descs = new LinkedList();
   114         LinkedList<ObjectStreamClass> descs = new LinkedList<>();
   110         ByteArrayOutputStream bout = new ByteArrayOutputStream();
   115         ByteArrayOutputStream bout = new ByteArrayOutputStream();
   111         LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs);
   116         LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs);
   112         lout.writeObject(lb);
   117         lout.writeObject(lb);
   113         lout.close();
   118         lout.close();
   114 
   119