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 package test.java.util.EnumSet; 31 32 import java.io.ByteArrayOutputStream; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.ObjectInputStream; 36 import java.io.ObjectOutputStream; 37 import java.util.EnumSet; 38 import java.util.stream.Collectors; 39 import java.util.stream.IntStream; 40 41 public class EnumSetClassSerialization { 42 main(String[] args)43 public static void main(String[] args) throws Exception { 44 // EnumSet.class object serialized with JDK 8 45 int[] bytes = { 46 0xac, 0xed, 0x00, 0x05, 0x76, 0x72, 0x00, 0x11, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75, 0x74, 0x69, 47 0x6c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0x0e, 0x03, 0x21, 0x6a, 0xcd, 0x8c, 0x29, 48 0xdd, 0x02, 0x00, 0x02, 0x4c, 0x00, 0x0b, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x54, 0x79, 49 0x70, 0x65, 0x74, 0x00, 0x11, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 50 0x43, 0x6c, 0x61, 0x73, 0x73, 0x3b, 0x5b, 0x00, 0x08, 0x75, 0x6e, 0x69, 0x76, 0x65, 0x72, 0x73, 51 0x65, 0x74, 0x00, 0x11, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 52 0x45, 0x6e, 0x75, 0x6d, 0x3b, 0x78, 0x70 53 }; 54 55 InputStream in = new InputStream() { 56 int i = 0; 57 58 @Override 59 public int read() { 60 return i < bytes.length ? bytes[i++] & 0xFF : -1; 61 } 62 }; 63 ObjectInputStream ois = new ObjectInputStream(in); 64 65 Object res = ois.readObject(); 66 67 if (res != EnumSet.class) { 68 throw new AssertionError( 69 "Expected: " + EnumSet.class + ", got: " + res); 70 } 71 } 72 73 /** 74 * This class can be used to print out lines that constitute 75 * the 'bytes' variable initializer in the test. 76 */ 77 public static class Serializer { main(String[] args)78 public static void main(String[] args) throws IOException { 79 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 80 ObjectOutputStream oos = new ObjectOutputStream(baos); 81 oos.writeObject(EnumSet.class); 82 oos.close(); 83 byte[] bytes = baos.toByteArray(); 84 int bpl = 16; 85 System.out.print( 86 IntStream 87 .range(0, (bytes.length + bpl - 1) / bpl) 88 .mapToObj(i -> IntStream 89 .range( 90 i * bpl, 91 Math.min(i * bpl + bpl, bytes.length) 92 ) 93 .mapToObj(ii -> { 94 String s = Integer.toHexString(bytes[ii] & 0xFF); 95 return s.length() == 1 ? "0x0" + s : "0x" + s; 96 }) 97 .collect(Collectors.joining(", ")) 98 ) 99 .collect(Collectors.joining(",\n ", "int[] bytes = {\n ", "\n};")) 100 ); 101 } 102 } 103 } 104