Path: blob/master/test/jdk/java/io/BufferedReader/Fill.java
41152 views
/*1* Copyright (c) 1998, 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 BufferedReader's read method will fill the target array26whenever possible27*/282930import java.io.IOException;31import java.io.Reader;32import java.io.BufferedReader;333435public class Fill {3637/**38* A simple Reader that is always ready but may read fewer than the39* requested number of characters40*/41static class Source extends Reader {4243int shortFall;44char next = 0;4546Source(int shortFall) {47this.shortFall = shortFall;48}4950public int read(char[] cbuf, int off, int len) throws IOException {51int n = len - shortFall;52for (int i = off; i < n; i++)53cbuf[i] = next++;54return n;55}5657public boolean ready() {58return true;59}6061public void close() throws IOException {62}6364}6566/**67* Test BufferedReader with an underlying source that always reads68* shortFall fewer characters than requested69*/70static void go(int shortFall) throws Exception {7172Reader r = new BufferedReader(new Source(shortFall), 10);73char[] cbuf = new char[8];7475int n1 = r.read(cbuf);76int n2 = r.read(cbuf);77System.err.println("Shortfall " + shortFall78+ ": Read " + n1 + ", then " + n2 + " chars");79if (n1 != cbuf.length)80throw new Exception("First read returned " + n1);81if (n2 != cbuf.length)82throw new Exception("Second read returned " + n2);8384}8586public static void main(String[] args) throws Exception {87for (int i = 0; i < 8; i++) go(i);88}8990}919293