Path: blob/master/test/jdk/java/io/BufferedInputStream/LargeCopyWithMark.java
41152 views
/*1* Copyright (c) 2013, 2021, 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* @requires (sun.arch.data.model == "64" & os.maxMemory > 4g)26* @summary BufferedInputStream calculates negative array size with large27* streams and mark28* @run main/othervm -Xmx4G -Xlog:gc,gc+heap,gc+ergo+heap -XX:+CrashOnOutOfMemoryError29-XX:+IgnoreUnrecognizedVMOptions -XX:+G1ExitOnExpansionFailure30-Xlog:cds=debug31LargeCopyWithMark32*/3334import java.io.BufferedInputStream;35import java.io.IOException;36import java.io.InputStream;37import java.io.OutputStream;3839public class LargeCopyWithMark {4041static final int BUFF_SIZE = 8192;42static final int BIS_BUFF_SIZE = Integer.MAX_VALUE / 2 + 100;43static final long BYTES_TO_COPY = 2L * Integer.MAX_VALUE;4445static {46assert BIS_BUFF_SIZE * 2 < 0 : "doubling must overflow";47}4849public static void main(String[] args) throws Exception {50byte[] buff = new byte[BUFF_SIZE];5152try (InputStream myis = new MyInputStream(BYTES_TO_COPY);53InputStream bis = new BufferedInputStream(myis, BIS_BUFF_SIZE);54OutputStream myos = new MyOutputStream()) {5556// will require a buffer bigger than BIS_BUFF_SIZE57bis.mark(BIS_BUFF_SIZE + 100);5859for (;;) {60int count = bis.read(buff, 0, BUFF_SIZE);61if (count == -1)62break;63myos.write(buff, 0, count);64}65}66}67}6869class MyInputStream extends InputStream {70private long bytesLeft;71public MyInputStream(long bytesLeft) {72this.bytesLeft = bytesLeft;73}74@Override public int read() throws IOException {75return 0;76}77@Override public int read(byte[] b) throws IOException {78return read(b, 0, b.length);79}80@Override public int read(byte[] b, int off, int len) throws IOException {81if (bytesLeft <= 0)82return -1;83long result = Math.min(bytesLeft, (long)len);84bytesLeft -= result;85return (int)result;86}87@Override public int available() throws IOException {88return (bytesLeft > 0) ? 1 : 0;89}90}9192class MyOutputStream extends OutputStream {93@Override public void write(int b) throws IOException {}94@Override public void write(byte[] b) throws IOException {}95@Override public void write(byte[] b, int off, int len) throws IOException {}96}979899