blob: 0b66668e7ec6d5b6459e0e5cba180771926e52c5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4151072
26 * @summary Ensure that BufferedReader's methods handle the new line character
27 * following the carriage return correctly after a readLine
28 * operation that resulted in reading a line terminated by a
29 * carriage return (\r).
30 */
31import java.io.*;
32
33public class ReadLine {
34
35 public static void main(String[] args) throws IOException {
36 // Make sure that the reader does not wait for additional characters to
37 // be read after reading a new line.
38 BufferedReader reader;
39 String[][] strings = {
40 {"CR/LF\r\n", "CR/LF"},
41 {"LF-Only\n", "LF-Only"},
42 {"CR-Only\r", "CR-Only"},
43 {"CR/LF line\r\nMore data", "More data"}
44 };
45
46 // test 0 "CR/LF\r\n"
47 // test 1 "LF-Only\n"
48 // test 2 "CR-Only\r"
49 for (int i = 0; i < 3; i++) {
50 reader = new BufferedReader(new
51 BoundedReader(strings[i][0]), strings[i][0].length());
52 if (!reader.readLine().equals(strings[i][1]))
53 throw new RuntimeException("Read incorrect text");
54 }
55
56
57 // Now test the mark and reset operations. Consider two cases.
58 // 1. For lines ending with CR only.
59 markResetTest("Lot of textual data\rMore textual data\n",
60 "More textual data");
61
62 // 2. Now for lines ending with CR/LF
63 markResetTest("Lot of textual data\r\nMore textual data\n",
64 "More textual data");
65
66 // 3. Now for lines ending with LF only
67 markResetTest("Lot of textual data\nMore textual data\n",
68 "More textual data");
69
70 // Need to ensure behavior of read() after a readLine() read of a CR/LF
71 // terminated line.
72 // 1. For lines ending with CR/LF only.
73
74 // uses "CR/LF line\r\nMore data"
75 reader = new BufferedReader(new
76 BoundedReader(strings[3][0]), strings[3][0].length());
77 reader.readLine();
78 if (reader.read() != 'M')
79 throw new RuntimeException("Read() failed");
80
81
82 // Need to ensure that a read(char[], int, int) following a readLine()
83 // read of a CR/LF terminated line behaves correctly.
84
85 // uses "CR/LF line\r\nMore data"
86 reader = new BufferedReader(new
87 BoundedReader(strings[3][0]), strings[3][0].length());
88 reader.readLine();
89
90 char[] buf = new char[9];
91 reader.read(buf, 0, 9);
92 String newStr = new String(buf);
93 if (!newStr.equals(strings[3][1]))
94 throw new RuntimeException("Read(char[],int,int) failed");
95 }
96
97 static void markResetTest(String inputStr, String resetStr)
98 throws IOException {
99 BufferedReader reader = new BufferedReader(new
100 BoundedReader(inputStr), inputStr.length());
101 System.out.println("> " + reader.readLine());
102 reader.mark(30);
103 System.out.println("......Marking stream .....");
104 String str = reader.readLine();
105 System.out.println("> " + str);
106 reader.reset();
107 String newStr = reader.readLine();
108 System.out.println("reset> " + newStr);
109
110 // Make sure that the reset point was set correctly.
111 if (!newStr.equals(resetStr))
112 throw new RuntimeException("Mark/Reset failed");
113 }
114
115
116 private static class BoundedReader extends Reader{
117
118 private char[] content;
119 private int limit;
120 private int pos = 0;
121
122 public BoundedReader(String content) {
123 this.limit = content.length();
124 this.content = new char[limit];
125 content.getChars(0, limit, this.content, 0);
126 }
127
128 public int read() throws IOException {
129 if (pos >= limit)
130 throw new RuntimeException("Read past limit");
131 return content[pos++];
132 }
133
134 public int read(char[] buf, int offset, int length)
135 throws IOException
136 {
137 int oldPos = pos;
138 for (int i = offset; i < length; i++) {
139 buf[i] = (char)read();
140 }
141 return (pos - oldPos);
142 }
143
144 public void close() {}
145 }
146
147}