Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/ByteArrayOutputStream/MaxCapacity.java
38821 views
/*1* Copyright (c) 2014 Google Inc. 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* @ignore This test has huge memory requirements26* @run main/timeout=1800/othervm -Xmx8g MaxCapacity27* @bug 805594928* @summary Check that we can write (almost) Integer.MAX_VALUE bytes29* to a ByteArrayOutputStream.30* @author Martin Buchholz31*/32import java.io.ByteArrayOutputStream;3334public class MaxCapacity {35public static void main(String[] args) {36long maxHeap = Runtime.getRuntime().maxMemory();37if (maxHeap < 3L * Integer.MAX_VALUE) {38System.out.printf("Skipping test; max memory %sM too small%n",39maxHeap/(1024*1024));40return;41}42ByteArrayOutputStream baos = new ByteArrayOutputStream();43for (long n = 0; ; n++) {44try {45baos.write((byte)'x');46} catch (Throwable t) {47// check data integrity while we're here48byte[] bytes = baos.toByteArray();49if (bytes.length != n)50throw new AssertionError("wrong length");51if (bytes[0] != 'x' ||52bytes[bytes.length - 1] != 'x')53throw new AssertionError("wrong contents");5455long gap = Integer.MAX_VALUE - n;56System.out.printf("gap=%dM %d%n", gap/(1024*1024), gap);57if (gap > 1024)58throw t;59// t.printStackTrace();60break;61}62}63}64}656667