Path: blob/master/test/jdk/java/util/EnumSet/EnumSetClassSerialization.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 822736826* @summary Test deserialization of a stream containing EnumSet.class object27*/2829import java.io.ByteArrayOutputStream;30import java.io.IOException;31import java.io.InputStream;32import java.io.ObjectInputStream;33import java.io.ObjectOutputStream;34import java.util.EnumSet;35import java.util.stream.Collectors;36import java.util.stream.IntStream;3738public class EnumSetClassSerialization {3940public static void main(String[] args) throws Exception {41// EnumSet.class object serialized with JDK 842int[] bytes = {430xac, 0xed, 0x00, 0x05, 0x76, 0x72, 0x00, 0x11, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75, 0x74, 0x69,440x6c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0x0e, 0x03, 0x21, 0x6a, 0xcd, 0x8c, 0x29,450xdd, 0x02, 0x00, 0x02, 0x4c, 0x00, 0x0b, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x54, 0x79,460x70, 0x65, 0x74, 0x00, 0x11, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f,470x43, 0x6c, 0x61, 0x73, 0x73, 0x3b, 0x5b, 0x00, 0x08, 0x75, 0x6e, 0x69, 0x76, 0x65, 0x72, 0x73,480x65, 0x74, 0x00, 0x11, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f,490x45, 0x6e, 0x75, 0x6d, 0x3b, 0x78, 0x7050};5152InputStream in = new InputStream() {53int i = 0;5455@Override56public int read() {57return i < bytes.length ? bytes[i++] & 0xFF : -1;58}59};60ObjectInputStream ois = new ObjectInputStream(in);6162Object res = ois.readObject();6364if (res != EnumSet.class) {65throw new AssertionError(66"Expected: " + EnumSet.class + ", got: " + res);67}68}6970/**71* This class can be used to print out lines that constitute72* the 'bytes' variable initializer in the test.73*/74public static class Serializer {75public static void main(String[] args) throws IOException {76ByteArrayOutputStream baos = new ByteArrayOutputStream();77ObjectOutputStream oos = new ObjectOutputStream(baos);78oos.writeObject(EnumSet.class);79oos.close();80byte[] bytes = baos.toByteArray();81int bpl = 16;82System.out.print(83IntStream84.range(0, (bytes.length + bpl - 1) / bpl)85.mapToObj(i -> IntStream86.range(87i * bpl,88Math.min(i * bpl + bpl, bytes.length)89)90.mapToObj(ii -> {91String s = Integer.toHexString(bytes[ii] & 0xFF);92return s.length() == 1 ? "0x0" + s : "0x" + s;93})94.collect(Collectors.joining(", "))95)96.collect(Collectors.joining(",\n ", "int[] bytes = {\n ", "\n};"))97);98}99}100}101102103