jdk/test/java/io/SequenceInputStream/LotsOfStreams.java
author igerasim
Thu, 13 Mar 2014 07:52:17 +0400
changeset 23344 70f819173479
permissions -rw-r--r--
7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     1
/*
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     4
 *
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     8
 *
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    13
 * accompanied this code).
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    14
 *
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    18
 *
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    21
 * questions.
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    22
 */
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    23
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    24
/* @test
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    25
 * @bug 7011804
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    26
 * @summary SequenceInputStream#read() was implemented recursivly,
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    27
 *          which may cause stack overflow
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    28
 */
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    29
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    30
import java.io.ByteArrayInputStream;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    31
import java.io.InputStream;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    32
import java.io.SequenceInputStream;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    33
import java.util.Enumeration;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    34
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    35
public class LotsOfStreams {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    36
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    37
    static final int MAX_SUBSTREAMS = 32000;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    38
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    39
    public static void main(String[] argv) throws Exception {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    40
        try (InputStream stream =
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    41
                new SequenceInputStream(new LOSEnumeration())) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    42
            stream.read();
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    43
        }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    44
        try (InputStream stream =
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    45
                new SequenceInputStream(new LOSEnumeration())) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    46
            byte[] b = new byte[1];
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    47
            stream.read(b, 0, 1);
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    48
        }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    49
    }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    50
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    51
    static class LOSEnumeration
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    52
            implements Enumeration<InputStream> {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    53
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    54
        private static InputStream inputStream =
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    55
                new ByteArrayInputStream(new byte[0]);
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    56
        private int left = MAX_SUBSTREAMS;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    57
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    58
        public boolean hasMoreElements() {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    59
            return (left > 0);
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    60
        }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    61
        public InputStream nextElement() {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    62
            left--;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    63
            return inputStream;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    64
        }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    65
    }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
diff changeset
    66
}