Path: blob/master/test/jdk/java/io/BufferedInputStream/Fill.java
41149 views
/*1* Copyright (c) 1998, 2006, 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 409038325@summary Ensure that BufferedInputStream's read method will fill the target26array whenever possible27*/282930import java.io.IOException;31import java.io.InputStream;32import java.io.BufferedInputStream;333435public class Fill {3637/**38* A simple InputStream that is always ready but may read fewer than the39* requested number of bytes40*/41static class Source extends InputStream {4243int shortFall;44byte next = 0;4546Source(int shortFall) {47this.shortFall = shortFall;48}4950public int read() throws IOException {51return next++;52}5354public int read(byte[] buf, int off, int len) throws IOException {55int n = len - shortFall;56for (int i = off; i < n; i++)57buf[i] = next++;58return n;59}6061public int available() {62return Integer.MAX_VALUE;63}6465public void close() throws IOException {66}6768}6970/**71* Test BufferedInputStream with an underlying source that always reads72* shortFall fewer bytes than requested73*/74static void go(int shortFall) throws Exception {7576InputStream r = new BufferedInputStream(new Source(shortFall), 10);77byte[] buf = new byte[8];7879int n1 = r.read(buf);80int n2 = r.read(buf);81System.err.println("Shortfall " + shortFall82+ ": Read " + n1 + ", then " + n2 + " bytes");83if (n1 != buf.length)84throw new Exception("First read returned " + n1);85if (n2 != buf.length)86throw new Exception("Second read returned " + n2);8788}8990public static void main(String[] args) throws Exception {91for (int i = 0; i < 8; i++) go(i);92}9394}959697