import java.io.InputStream; /* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 7103957 * @summary Ensure that no NegativeArraySizeException is thrown. * @run main NegativeArraySizeTest */ public class NegativeArraySizeTest { public static void main(String[] args) throws Exception { // Derive the path to the java VM. String separator = System.getProperty("file.separator"); String classpath = System.getProperty("java.class.path"); String javapath = System.getProperty("java.home") + separator + "bin" + separator + "java"; // Build the process to exec the SubVM class forcing the // Integer.IntegerCache.high setting. ProcessBuilder processBuilder = new ProcessBuilder(javapath, "-Djava.lang.Integer.IntegerCache.high=2147483519", "-classpath", classpath, SubVM.class.getName()); // Force subprocess I/O to go over a pipe. processBuilder.redirectError(ProcessBuilder.Redirect.PIPE); processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE); // Spawn and wait for SubVM process. Process process = processBuilder.start(); process.waitFor(); // Need to check both error and output stream of SubVM Process // as the stream to which the exception string is written appears // to vary depending on which version of the VM is run. // Check subprocess error stream for NegativeArraySizeException. byte[] b = new byte[2048]; InputStream errorStream = process.getErrorStream(); int len; if ((len = errorStream.read(b, 0, b.length)) > 0) { String s = new String(b, 0, len); if (s.indexOf("NegativeArraySizeException") != -1) { throw new RuntimeException("NegativeArraySizeTest failed!"); } } // Check subprocess output stream for NegativeArraySizeException. InputStream outputStream = process.getInputStream(); if ((len = outputStream.read(b, 0, b.length)) > 0) { String s = new String(b, 0, len); if (s.indexOf("NegativeArraySizeException") != -1) { throw new RuntimeException("NegativeArraySizeTest failed!"); } } } } class SubVM { public static void main(String[] args) { Integer i = 0; } }