23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee/*
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee *
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * This code is free software; you can redistribute it and/or modify it
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * under the terms of the GNU General Public License version 2 only, as
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * published by the Free Software Foundation.
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee *
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * This code is distributed in the hope that it will be useful, but WITHOUT
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * version 2 for more details (a copy is included in the LICENSE file that
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * accompanied this code).
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee *
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * You should have received a copy of the GNU General Public License version
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * 2 along with this work; if not, write to the Free Software Foundation,
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee *
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * or visit www.oracle.com if you need additional information or have any
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * questions.
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee */
e77b06d21580f630e0a7c437495ab283d3672828tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee/* @test
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee @bug 4090383
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee @summary Ensure that BufferedReader's read method will fill the target array
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee whenever possible
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee */
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomeeimport java.io.IOException;
e642872b5a76c4c8654eaa68f64986a85c86ca37qiaoimport java.io.Reader;
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomeeimport java.io.BufferedReader;
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomeepublic class Fill {
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee /**
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * A simple Reader that is always ready but may read fewer than the
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee * requested number of characters
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee */
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee static class Source extends Reader {
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee int shortFall;
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee char next = 0;
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao Source(int shortFall) {
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee this.shortFall = shortFall;
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao }
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee public int read(char[] cbuf, int off, int len) throws IOException {
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao int n = len - shortFall;
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee for (int i = off; i < n; i++)
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee cbuf[i] = next++;
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao return n;
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee }
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee public boolean ready() {
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee return true;
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee }
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee public void close() throws IOException {
e77b06d21580f630e0a7c437495ab283d3672828tomee }
e77b06d21580f630e0a7c437495ab283d3672828tomee
e77b06d21580f630e0a7c437495ab283d3672828tomee }
e77b06d21580f630e0a7c437495ab283d3672828tomee
e77b06d21580f630e0a7c437495ab283d3672828tomee /**
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao * Test BufferedReader with an underlying source that always reads
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao * shortFall fewer characters than requested
e77b06d21580f630e0a7c437495ab283d3672828tomee */
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao static void go(int shortFall) throws Exception {
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao Reader r = new BufferedReader(new Source(shortFall), 10);
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee char[] cbuf = new char[8];
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee int n1 = r.read(cbuf);
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee int n2 = r.read(cbuf);
e642872b5a76c4c8654eaa68f64986a85c86ca37qiao System.err.println("Shortfall " + shortFall
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee + ": Read " + n1 + ", then " + n2 + " chars");
if (n1 != cbuf.length)
throw new Exception("First read returned " + n1);
if (n2 != cbuf.length)
throw new Exception("Second read returned " + n2);
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < 8; i++) go(i);
}
}