Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/BufferedInputStream/LargeCopyWithMark.java
38812 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. 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/* @test24* @bug 712931225* @summary BufferedInputStream calculates negative array size with large26* streams and mark27* @library /lib/testlibrary28* @build jdk.testlibrary.*29* @run main/othervm LargeCopyWithMark30*/3132import java.io.BufferedInputStream;33import java.io.IOException;34import java.io.InputStream;35import java.io.OutputStream;36import static jdk.testlibrary.ProcessTools.*;373839public class LargeCopyWithMark {4041public static void main(String[] args) throws Exception {42if (! System.getProperty("os.arch").contains("64")) {43System.out.println("Test runs on 64 bit platforms");44return;45}46ProcessBuilder pb = createJavaProcessBuilder("-Xmx4G",47"-ea:LargeCopyWithMark$Child",48"LargeCopyWithMark$Child");49int res = pb.inheritIO().start().waitFor();50if (res != 0) {51throw new AssertionError("Test failed: exit code = " + res);52}53}5455public static class Child {56static final int BUFF_SIZE = 8192;57static final int BIS_BUFF_SIZE = Integer.MAX_VALUE / 2 + 100;58static final long BYTES_TO_COPY = 2L * Integer.MAX_VALUE;5960static {61assert BIS_BUFF_SIZE * 2 < 0 : "doubling must overflow";62}6364public static void main(String[] args) throws Exception {65byte[] buff = new byte[BUFF_SIZE];6667try (InputStream myis = new MyInputStream(BYTES_TO_COPY);68InputStream bis = new BufferedInputStream(myis, BIS_BUFF_SIZE);69OutputStream myos = new MyOutputStream()) {7071// will require a buffer bigger than BIS_BUFF_SIZE72bis.mark(BIS_BUFF_SIZE + 100);7374for (;;) {75int count = bis.read(buff, 0, BUFF_SIZE);76if (count == -1)77break;78myos.write(buff, 0, count);79}80} catch (java.lang.NegativeArraySizeException e) {81e.printStackTrace();82System.exit(11);83} catch (Exception e) {84e.printStackTrace();85}86}87}88}8990class MyInputStream extends InputStream {91private long bytesLeft;92public MyInputStream(long bytesLeft) {93this.bytesLeft = bytesLeft;94}95@Override public int read() throws IOException {96return 0;97}98@Override public int read(byte[] b) throws IOException {99return read(b, 0, b.length);100}101@Override public int read(byte[] b, int off, int len) throws IOException {102if (bytesLeft <= 0)103return -1;104long result = Math.min(bytesLeft, (long)len);105bytesLeft -= result;106return (int)result;107}108@Override public int available() throws IOException {109return (bytesLeft > 0) ? 1 : 0;110}111}112113class MyOutputStream extends OutputStream {114@Override public void write(int b) throws IOException {}115@Override public void write(byte[] b) throws IOException {}116@Override public void write(byte[] b, int off, int len) throws IOException {}117}118119120