author | dholmes |
Wed, 06 Nov 2019 21:18:42 -0500 | |
changeset 58956 | 9a0a5e70eeb2 |
parent 54643 | 3edf22a7cbaf |
permissions | -rw-r--r-- |
54643
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
1 |
/* |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
4 |
* |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
8 |
* |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
13 |
* accompanied this code). |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
14 |
* |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
18 |
* |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
21 |
* questions. |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
22 |
*/ |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
23 |
|
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
24 |
/* @test |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
25 |
* @bug 8218280 |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
26 |
* @summary Make sure marking a line feed within a CRLF sequence works correctly |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
27 |
* @run testng MarkSplitCRLF |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
28 |
*/ |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
29 |
|
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
30 |
import java.io.IOException; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
31 |
import java.io.LineNumberReader; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
32 |
import java.io.Reader; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
33 |
import java.io.StringReader; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
34 |
|
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
35 |
import org.testng.annotations.Test; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
36 |
|
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
37 |
import static org.testng.Assert.*; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
38 |
|
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
39 |
public class MarkSplitCRLF { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
40 |
@Test |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
41 |
public static void testSpecifiedBufferSize() throws IOException { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
42 |
final String string = "foo\r\nbar"; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
43 |
try (Reader reader = |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
44 |
new LineNumberReader(new StringReader(string), 5)) { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
45 |
reader.read(); // 'f' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
46 |
reader.read(); // 'o' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
47 |
reader.read(); // 'o' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
48 |
reader.read(); // '\r' -> '\n' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
49 |
reader.mark(1); // mark position of '\n' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
50 |
reader.read(); // 'b' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
51 |
reader.reset(); // reset to '\n' position |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
52 |
assertEquals(reader.read(), 'b'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
53 |
assertEquals(reader.read(), 'a'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
54 |
assertEquals(reader.read(), 'r'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
55 |
} |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
56 |
} |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
57 |
|
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
58 |
@Test |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
59 |
public static void testCRNotFollowedByLF() throws IOException { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
60 |
final String string = "foo\rbar"; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
61 |
try (Reader reader = |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
62 |
new LineNumberReader(new StringReader(string), 5)) { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
63 |
reader.read(); // 'f' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
64 |
reader.read(); // 'o' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
65 |
reader.read(); // 'o' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
66 |
reader.read(); // '\r' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
67 |
reader.mark(1); // mark position of next character |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
68 |
reader.read(); // 'b' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
69 |
reader.reset(); // reset to position after '\r' |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
70 |
assertEquals(reader.read(), 'b'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
71 |
assertEquals(reader.read(), 'a'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
72 |
assertEquals(reader.read(), 'r'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
73 |
} |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
74 |
} |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
75 |
|
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
76 |
@Test |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
77 |
public static void testDefaultBufferSize() throws IOException { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
78 |
StringBuilder sb = new StringBuilder(8195); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
79 |
for (int i = 0; i < 8190; i++) { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
80 |
char c = (char)i; |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
81 |
sb.append(c); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
82 |
} |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
83 |
sb.append('\r'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
84 |
sb.append('\n'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
85 |
sb.append('X'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
86 |
sb.append('Y'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
87 |
sb.append('Z'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
88 |
final String string = sb.toString(); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
89 |
try (Reader reader = new LineNumberReader(new StringReader(string))) { |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
90 |
for (int i = 0; i < 8191; i++) reader.read(); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
91 |
reader.mark(1); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
92 |
reader.read(); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
93 |
reader.reset(); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
94 |
assertEquals(reader.read(), 'X'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
95 |
assertEquals(reader.read(), 'Y'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
96 |
assertEquals(reader.read(), 'Z'); |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
97 |
} |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
98 |
} |
3edf22a7cbaf
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents:
diff
changeset
|
99 |
} |