test/jdk/java/util/EnumSet/EnumSetClassSerialization.java
changeset 55697 7fd4446c02ee
equal deleted inserted replaced
55696:7e04ccc598e0 55697:7fd4446c02ee
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug     8227368
       
    27  * @summary Test deserialization of a stream containing EnumSet.class object
       
    28  */
       
    29 
       
    30 import java.io.ByteArrayOutputStream;
       
    31 import java.io.IOException;
       
    32 import java.io.InputStream;
       
    33 import java.io.ObjectInputStream;
       
    34 import java.io.ObjectOutputStream;
       
    35 import java.util.EnumSet;
       
    36 import java.util.stream.Collectors;
       
    37 import java.util.stream.IntStream;
       
    38 
       
    39 public class EnumSetClassSerialization {
       
    40 
       
    41     public static void main(String[] args) throws Exception {
       
    42         // EnumSet.class object serialized with JDK 8
       
    43         int[] bytes = {
       
    44             0xac, 0xed, 0x00, 0x05, 0x76, 0x72, 0x00, 0x11, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75, 0x74, 0x69,
       
    45             0x6c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0x0e, 0x03, 0x21, 0x6a, 0xcd, 0x8c, 0x29,
       
    46             0xdd, 0x02, 0x00, 0x02, 0x4c, 0x00, 0x0b, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x54, 0x79,
       
    47             0x70, 0x65, 0x74, 0x00, 0x11, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f,
       
    48             0x43, 0x6c, 0x61, 0x73, 0x73, 0x3b, 0x5b, 0x00, 0x08, 0x75, 0x6e, 0x69, 0x76, 0x65, 0x72, 0x73,
       
    49             0x65, 0x74, 0x00, 0x11, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f,
       
    50             0x45, 0x6e, 0x75, 0x6d, 0x3b, 0x78, 0x70
       
    51         };
       
    52 
       
    53         InputStream in = new InputStream() {
       
    54             int i = 0;
       
    55 
       
    56             @Override
       
    57             public int read() {
       
    58                 return i < bytes.length ? bytes[i++] & 0xFF : -1;
       
    59             }
       
    60         };
       
    61         ObjectInputStream ois = new ObjectInputStream(in);
       
    62 
       
    63         Object res = ois.readObject();
       
    64 
       
    65         if (res != EnumSet.class) {
       
    66             throw new AssertionError(
       
    67                 "Expected: " + EnumSet.class + ", got: " + res);
       
    68         }
       
    69     }
       
    70 
       
    71     /**
       
    72      * This class can be used to print out lines that constitute
       
    73      * the 'bytes' variable initializer in the test.
       
    74      */
       
    75     public static class Serializer {
       
    76         public static void main(String[] args) throws IOException {
       
    77             ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
    78             ObjectOutputStream oos = new ObjectOutputStream(baos);
       
    79             oos.writeObject(EnumSet.class);
       
    80             oos.close();
       
    81             byte[] bytes = baos.toByteArray();
       
    82             int bpl = 16;
       
    83             System.out.print(
       
    84                 IntStream
       
    85                     .range(0, (bytes.length + bpl - 1) / bpl)
       
    86                     .mapToObj(i -> IntStream
       
    87                         .range(
       
    88                             i * bpl,
       
    89                             Math.min(i * bpl + bpl, bytes.length)
       
    90                         )
       
    91                         .mapToObj(ii -> {
       
    92                             String s = Integer.toHexString(bytes[ii] & 0xFF);
       
    93                             return s.length() == 1 ? "0x0" + s : "0x" + s;
       
    94                         })
       
    95                         .collect(Collectors.joining(", "))
       
    96                     )
       
    97                     .collect(Collectors.joining(",\n  ", "int[] bytes = {\n  ", "\n};"))
       
    98             );
       
    99         }
       
   100     }
       
   101 }