import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ReadObjectTest {

    static class FakeObject11 implements Serializable {
        static int counter;

        private static final long serialVersionUID = 7249069246763182397L;

        private void readObject(java.io.ObjectInputStream s) {
            Object l0 = null;
            Object l1 = null;
            Object l2 = null;
            Object l3 = null;
            Object l4 = null;
            Object l5 = null;
            Object l6 = null;
            Object l7 = null;
            Object l8 = null;
            Object l9 = null;
            Object l10 = null;
            if (counter++ > 100000) {
                for (int i = 0; i < 1000000; i++) {
                }
                throw new InternalError();
            }
        }
    }

    static class FakeObject12 implements Serializable {
        static int counter;

        private static final long serialVersionUID = 7249069246763182397L;

        private void readObject(java.io.ObjectInputStream s) {
            Object l0 = null;
            Object l1 = null;
            Object l2 = null;
            Object l3 = null;
            Object l4 = null;
            Object l5 = null;
            Object l6 = null;
            Object l7 = null;
            Object l8 = null;
            Object l9 = null;
            Object l10 = null;
            Object l11 = null;
            if (counter++ > 100000) {
                for (int i = 0; i < 1000000; i++) {
                }
                throw new InternalError();
            }
        }
    }

    static class FakeObject22 implements Serializable {
        static int counter;

        private static final long serialVersionUID = 7249069246763182397L;

        private void readObject(java.io.ObjectInputStream s) {
            Object l0 = null;
            Object l1 = null;
            Object l2 = null;
            Object l3 = null;
            Object l4 = null;
            Object l5 = null;
            Object l6 = null;
            Object l7 = null;
            Object l8 = null;
            Object l9 = null;
            Object l10 = null;
            Object l11 = null;
            Object l12 = null;
            Object l13 = null;
            Object l14 = null;
            Object l15 = null;
            Object l16 = null;
            Object l17 = null;
            Object l18 = null;
            Object l19 = null;
            Object l20 = null;
            Object l21 = null;
            if (counter++ > 100000) {
                for (int i = 0; i < 1000000; i++) {
                }
                throw new InternalError();
            }
        }
    }

    static class FakeObject21 implements Serializable {
        static int counter;

        private static final long serialVersionUID = 7249069246763182397L;

        private void readObject(java.io.ObjectInputStream s) {
            Object l0 = null;
            Object l1 = null;
            Object l2 = null;
            Object l3 = null;
            Object l4 = null;
            Object l5 = null;
            Object l6 = null;
            Object l7 = null;
            Object l8 = null;
            Object l9 = null;
            Object l10 = null;
            Object l11 = null;
            Object l12 = null;
            Object l13 = null;
            Object l14 = null;
            Object l15 = null;
            Object l16 = null;
            Object l17 = null;
            Object l18 = null;
            Object l19 = null;
            Object l20 = null;
            if (counter++ > 100000) {
                for (int i = 0; i < 1000000; i++) {
                }
                throw new InternalError();
            }
        }
    }

    static class FakeObject23 implements Serializable {
        static int counter;

        private static final long serialVersionUID = 7249069246763182397L;

        private void readObject(java.io.ObjectInputStream s) {
            Object l0 = null;
            Object l1 = null;
            Object l2 = null;
            Object l3 = null;
            Object l4 = null;
            Object l5 = null;
            Object l6 = null;
            Object l7 = null;
            Object l8 = null;
            Object l9 = null;
            Object l10 = null;
            Object l11 = null;
            Object l12 = null;
            Object l13 = null;
            Object l14 = null;
            Object l15 = null;
            Object l16 = null;
            Object l17 = null;
            Object l18 = null;
            Object l19 = null;
            Object l20 = null;
            Object l21 = null;
            Object l22 = null;
            if (counter++ > 100000) {
                for (int i = 0; i < 1000000; i++) {
                }
                throw new InternalError();
            }
        }
    }

    static class FakeObject24 implements Serializable {
        static int counter;

        private static final long serialVersionUID = 7249069246763182397L;

        private void readObject(java.io.ObjectInputStream s) {
            Object l0 = null;
            Object l1 = null;
            Object l2 = null;
            Object l3 = null;
            Object l4 = null;
            Object l5 = null;
            Object l6 = null;
            Object l7 = null;
            Object l8 = null;
            Object l9 = null;
            Object l10 = null;
            Object l11 = null;
            Object l12 = null;
            Object l13 = null;
            Object l14 = null;
            Object l15 = null;
            Object l16 = null;
            Object l17 = null;
            Object l18 = null;
            Object l19 = null;
            Object l20 = null;
            Object l21 = null;
            Object l22 = null;
            Object l23 = null;
            if (counter++ > 100000) {
                for (int i = 0; i < 1000000; i++) {
                }
                throw new InternalError();
            }
        }
    }

    static class FakeObject25 implements Serializable {
        static int counter;

        private static final long serialVersionUID = 7249069246763182397L;

        private void readObject(java.io.ObjectInputStream s) {
            Object l0 = null;
            Object l1 = null;
            Object l2 = null;
            Object l3 = null;
            Object l4 = null;
            Object l5 = null;
            Object l6 = null;
            Object l7 = null;
            Object l8 = null;
            Object l9 = null;
            Object l10 = null;
            Object l11 = null;
            Object l12 = null;
            Object l13 = null;
            Object l14 = null;
            Object l15 = null;
            Object l16 = null;
            Object l17 = null;
            Object l18 = null;
            Object l19 = null;
            Object l20 = null;
            Object l21 = null;
            Object l22 = null;
            Object l23 = null;
            Object l24 = null;
            if (counter++ > 100000) {
                for (int i = 0; i < 1000000; i++) {
                }
                throw new InternalError();
            }
        }
    }

    public void readFakeObject(int i) {
        try {
            switch (i) {
                case 11:
                    testReadObject(new FakeObject11());
                    break;
                case 12:
                    testReadObject(new FakeObject12());
                    break;
                case 21:
                    testReadObject(new FakeObject21());
                    break;
                case 22:
                    testReadObject(new FakeObject22());
                    break;
                case 23:
                    testReadObject(new FakeObject23());
                    break;
                case 24:
                    testReadObject(new FakeObject24());
                    break;
                case 25:
                    testReadObject(new FakeObject25());
                    break;
            }
        } catch (InternalError e) {
            // expected exception
        }
    }

    public static void main(String[] args) {
        ReadObjectTest object = new ReadObjectTest();
        if (args.length == 0) {
            for (int i = 0; i < 26; i++) {
                object.readFakeObject(i);
            }
        } else {
            object.readFakeObject(Integer.parseInt(args[0]));
        }
    }

    private static void testReadObject(Object map) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(map);
            oos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        byte[] array = out.toByteArray();
        for (int i = 0; i < 1000000; i++) {
            try (ObjectInputStream s = new ObjectInputStream(new ByteArrayInputStream(array))) {
                s.readObject();
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

