Path: blob/master/test/jdk/java/io/BufferedReader/ReadLine.java
41152 views
/*1* Copyright (c) 1999, 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 415107225* @summary Ensure that BufferedReader's methods handle the new line character26* following the carriage return correctly after a readLine27* operation that resulted in reading a line terminated by a28* carriage return (\r).29*/30import java.io.*;3132public class ReadLine {3334public static void main(String[] args) throws IOException {35// Make sure that the reader does not wait for additional characters to36// be read after reading a new line.37BufferedReader reader;38String[][] strings = {39{"CR/LF\r\n", "CR/LF"},40{"LF-Only\n", "LF-Only"},41{"CR-Only\r", "CR-Only"},42{"CR/LF line\r\nMore data", "More data"}43};4445// test 0 "CR/LF\r\n"46// test 1 "LF-Only\n"47// test 2 "CR-Only\r"48for (int i = 0; i < 3; i++) {49reader = new BufferedReader(new50BoundedReader(strings[i][0]), strings[i][0].length());51if (!reader.readLine().equals(strings[i][1]))52throw new RuntimeException("Read incorrect text");53}545556// Now test the mark and reset operations. Consider two cases.57// 1. For lines ending with CR only.58markResetTest("Lot of textual data\rMore textual data\n",59"More textual data");6061// 2. Now for lines ending with CR/LF62markResetTest("Lot of textual data\r\nMore textual data\n",63"More textual data");6465// 3. Now for lines ending with LF only66markResetTest("Lot of textual data\nMore textual data\n",67"More textual data");6869// Need to ensure behavior of read() after a readLine() read of a CR/LF70// terminated line.71// 1. For lines ending with CR/LF only.7273// uses "CR/LF line\r\nMore data"74reader = new BufferedReader(new75BoundedReader(strings[3][0]), strings[3][0].length());76reader.readLine();77if (reader.read() != 'M')78throw new RuntimeException("Read() failed");798081// Need to ensure that a read(char[], int, int) following a readLine()82// read of a CR/LF terminated line behaves correctly.8384// uses "CR/LF line\r\nMore data"85reader = new BufferedReader(new86BoundedReader(strings[3][0]), strings[3][0].length());87reader.readLine();8889char[] buf = new char[9];90reader.read(buf, 0, 9);91String newStr = new String(buf);92if (!newStr.equals(strings[3][1]))93throw new RuntimeException("Read(char[],int,int) failed");94}9596static void markResetTest(String inputStr, String resetStr)97throws IOException {98BufferedReader reader = new BufferedReader(new99BoundedReader(inputStr), inputStr.length());100System.out.println("> " + reader.readLine());101reader.mark(30);102System.out.println("......Marking stream .....");103String str = reader.readLine();104System.out.println("> " + str);105reader.reset();106String newStr = reader.readLine();107System.out.println("reset> " + newStr);108109// Make sure that the reset point was set correctly.110if (!newStr.equals(resetStr))111throw new RuntimeException("Mark/Reset failed");112}113114115private static class BoundedReader extends Reader{116117private char[] content;118private int limit;119private int pos = 0;120121public BoundedReader(String content) {122this.limit = content.length();123this.content = new char[limit];124content.getChars(0, limit, this.content, 0);125}126127public int read() throws IOException {128if (pos >= limit)129throw new RuntimeException("Read past limit");130return content[pos++];131}132133public int read(char[] buf, int offset, int length)134throws IOException135{136int oldPos = pos;137for (int i = offset; i < length; i++) {138buf[i] = (char)read();139}140return (pos - oldPos);141}142143public void close() {}144}145146}147148149