jdk/test/java/io/SequenceInputStream/LotsOfStreams.java
changeset 23344 70f819173479
equal deleted inserted replaced
23343:bc82981ff581 23344:70f819173479
       
     1 /*
       
     2  * Copyright (c) 2014, 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 /* @test
       
    25  * @bug 7011804
       
    26  * @summary SequenceInputStream#read() was implemented recursivly,
       
    27  *          which may cause stack overflow
       
    28  */
       
    29 
       
    30 import java.io.ByteArrayInputStream;
       
    31 import java.io.InputStream;
       
    32 import java.io.SequenceInputStream;
       
    33 import java.util.Enumeration;
       
    34 
       
    35 public class LotsOfStreams {
       
    36 
       
    37     static final int MAX_SUBSTREAMS = 32000;
       
    38 
       
    39     public static void main(String[] argv) throws Exception {
       
    40         try (InputStream stream =
       
    41                 new SequenceInputStream(new LOSEnumeration())) {
       
    42             stream.read();
       
    43         }
       
    44         try (InputStream stream =
       
    45                 new SequenceInputStream(new LOSEnumeration())) {
       
    46             byte[] b = new byte[1];
       
    47             stream.read(b, 0, 1);
       
    48         }
       
    49     }
       
    50 
       
    51     static class LOSEnumeration
       
    52             implements Enumeration<InputStream> {
       
    53 
       
    54         private static InputStream inputStream =
       
    55                 new ByteArrayInputStream(new byte[0]);
       
    56         private int left = MAX_SUBSTREAMS;
       
    57 
       
    58         public boolean hasMoreElements() {
       
    59             return (left > 0);
       
    60         }
       
    61         public InputStream nextElement() {
       
    62             left--;
       
    63             return inputStream;
       
    64         }
       
    65     }
       
    66 }