Path: blob/master/test/jdk/java/io/BufferedReader/Ready.java
41149 views
/*1* Copyright (c) 2000, 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 432998525* @summary Ensure that BufferedReader's ready() method handles the new line26* character following the carriage return correctly and returns the right27* value so that a read operation after a ready() does not block unnecessarily.28*/29import java.io.*;3031public class Ready {3233public static void main(String[] args) throws IOException {34BufferedReader reader;35String[] strings = {36"LF-Only\n",37"LF-Only\n",38"CR/LF\r\n",39"CR/LF\r\n",40"CR-Only\r",41"CR-Only\r",42"CR/LF line\r\nMore data.\r\n",43"CR/LF line\r\nMore data.\r\n"44};4546// The buffer sizes are chosen such that the boundary conditions are47// tested.48int[] bufsizes = { 7, 8, 6, 5, 7, 8, 11, 10};4950for (int i = 0; i < strings.length; i++) {51reader = new BufferedReader(new BoundedReader(strings[i]),52bufsizes[i]);53while (reader.ready()) {54String str = reader.readLine();55System.out.println("read>>" + str);56}57}58}59606162private static class BoundedReader extends Reader{6364private char[] content;65private int limit;66private int pos = 0;6768public BoundedReader(String content) {69this.limit = content.length();70this.content = new char[limit];71content.getChars(0, limit, this.content, 0);72}7374public int read() throws IOException {75if (pos >= limit)76throw new RuntimeException("Hit infinite wait condition");77return content[pos++];78}7980public int read(char[] buf, int offset, int length)81throws IOException82{83if (pos >= limit)84throw new RuntimeException("Hit infinite wait condition");85int oldPos = pos;86int readlen = (length > (limit - pos)) ? (limit - pos) : length;87for (int i = offset; i < readlen; i++) {88buf[i] = (char)read();89}9091return (pos - oldPos);92}9394public void close() {}9596public boolean ready() {97if (pos < limit)98return true;99else100return false;101}102}103104}105106107