8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
authorbpb
Mon, 29 Apr 2019 07:39:16 -0700
changeset 54643 3edf22a7cbaf
parent 54642 d3dcec24a469
child 54644 8d52b4c6f9d8
8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer. Reviewed-by: dfuchs, prappo
src/java.base/share/classes/java/io/LineNumberReader.java
test/jdk/java/io/LineNumberReader/MarkSplitCRLF.java
--- a/src/java.base/share/classes/java/io/LineNumberReader.java	Fri Apr 26 16:38:39 2019 +0800
+++ b/src/java.base/share/classes/java/io/LineNumberReader.java	Mon Apr 29 07:39:16 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -261,6 +261,11 @@
      */
     public void mark(int readAheadLimit) throws IOException {
         synchronized (lock) {
+            // If the most recently read character is '\r', then increment the
+            // read ahead limit as in this case if the next character is '\n',
+            // two characters would actually be read by the next read().
+            if (skipLF)
+                readAheadLimit++;
             super.mark(readAheadLimit);
             markedLineNumber = lineNumber;
             markedSkipLF     = skipLF;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/io/LineNumberReader/MarkSplitCRLF.java	Mon Apr 29 07:39:16 2019 -0700
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8218280
+ * @summary Make sure marking a line feed within a CRLF sequence works correctly
+ * @run testng MarkSplitCRLF
+ */
+
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+public class MarkSplitCRLF {
+    @Test
+    public static void testSpecifiedBufferSize() throws IOException {
+        final String string = "foo\r\nbar";
+        try (Reader reader =
+            new LineNumberReader(new StringReader(string), 5)) {
+            reader.read();  // 'f'
+            reader.read();  // 'o'
+            reader.read();  // 'o'
+            reader.read();  // '\r' -> '\n'
+            reader.mark(1); // mark position of '\n'
+            reader.read();  // 'b'
+            reader.reset(); // reset to '\n' position
+            assertEquals(reader.read(), 'b');
+            assertEquals(reader.read(), 'a');
+            assertEquals(reader.read(), 'r');
+        }
+    }
+
+    @Test
+    public static void testCRNotFollowedByLF() throws IOException {
+        final String string = "foo\rbar";
+        try (Reader reader =
+            new LineNumberReader(new StringReader(string), 5)) {
+            reader.read();  // 'f'
+            reader.read();  // 'o'
+            reader.read();  // 'o'
+            reader.read();  // '\r'
+            reader.mark(1); // mark position of next character
+            reader.read();  // 'b'
+            reader.reset(); // reset to position after '\r'
+            assertEquals(reader.read(), 'b');
+            assertEquals(reader.read(), 'a');
+            assertEquals(reader.read(), 'r');
+        }
+    }
+
+    @Test
+    public static void testDefaultBufferSize() throws IOException {
+        StringBuilder sb = new StringBuilder(8195);
+        for (int i = 0; i < 8190; i++) {
+            char c = (char)i;
+            sb.append(c);
+        }
+        sb.append('\r');
+        sb.append('\n');
+        sb.append('X');
+        sb.append('Y');
+        sb.append('Z');
+        final String string = sb.toString();
+        try (Reader reader = new LineNumberReader(new StringReader(string))) {
+            for (int i = 0; i < 8191; i++) reader.read();
+            reader.mark(1);
+            reader.read();
+            reader.reset();
+            assertEquals(reader.read(), 'X');
+            assertEquals(reader.read(), 'Y');
+            assertEquals(reader.read(), 'Z');
+        }
+    }
+}