Path: blob/master/test/jdk/java/io/ByteArrayOutputStream/MaxCapacity.java
41149 views
/*1* Copyright (c) 2014, Google Inc. All rights reserved.2* Copyright (c) 2020, 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @bug 805594927* @summary Check that we can write (almost) Integer.MAX_VALUE bytes28* to a ByteArrayOutputStream.29* @requires (sun.arch.data.model == "64" & os.maxMemory >= 10g)30* @run main/timeout=1800/othervm -Xmx8g MaxCapacity31* @author Martin Buchholz32*/33import java.io.ByteArrayOutputStream;3435public class MaxCapacity {36public static void main(String[] args) {37long maxHeap = Runtime.getRuntime().maxMemory();38if (maxHeap < 3L * Integer.MAX_VALUE) {39System.out.printf("Skipping test; max memory %sM too small%n",40maxHeap/(1024*1024));41return;42}43ByteArrayOutputStream baos = new ByteArrayOutputStream();44for (long n = 0; ; n++) {45try {46baos.write((byte)'x');47} catch (Throwable t) {48// check data integrity while we're here49byte[] bytes = baos.toByteArray();50if (bytes.length != n)51throw new AssertionError("wrong length");52if (bytes[0] != 'x' ||53bytes[bytes.length - 1] != 'x')54throw new AssertionError("wrong contents");5556long gap = Integer.MAX_VALUE - n;57System.out.printf("gap=%dM %d%n", gap/(1024*1024), gap);58if (gap > 1024)59throw t;60// t.printStackTrace();61break;62}63}64}65}666768