1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8218280
  26  * @summary Make sure marking a line feed within a CRLF sequence works correctly
  27  * @run testng MarkSplitCRLF
  28  */
  29 
  30 import java.io.IOException;
  31 import java.io.LineNumberReader;
  32 import java.io.Reader;
  33 import java.io.StringReader;
  34 
  35 import org.testng.annotations.Test;
  36 
  37 import static org.testng.Assert.*;
  38 
  39 public class MarkSplitCRLF {
  40     @Test
  41     public static void testSpecifiedBufferSize() throws IOException {
  42         final String string = "foo\r\nbar";
  43         try (Reader reader =
  44             new LineNumberReader(new StringReader(string), 5)) {
  45             reader.read();  // 'f'
  46             reader.read();  // 'o'
  47             reader.read();  // 'o'
  48             reader.read();  // '\r' -> '\n'
  49             reader.mark(1); // mark position of '\n'
  50             reader.read();  // 'b'
  51             reader.reset(); // reset to '\n' position
  52             assertEquals(reader.read(), 'b');
  53             assertEquals(reader.read(), 'a');
  54             assertEquals(reader.read(), 'r');
  55         }
  56     }
  57 
  58     @Test
  59     public static void testDefaultBufferSize() throws IOException {
  60         StringBuilder sb = new StringBuilder(8195);
  61         for (int i = 0; i < 8190; i++) {
  62             char c = (char)i;
  63             sb.append(c);
  64         }
  65         sb.append('\r');
  66         sb.append('\n');
  67         sb.append('X');
  68         sb.append('Y');
  69         sb.append('Z');
  70         final String string = sb.toString();
  71         try (Reader reader = new LineNumberReader(new StringReader(string))) {
  72             for (int i = 0; i < 8191; i++) reader.read();
  73             reader.mark(1);
  74             reader.read();
  75             reader.reset();
  76             assertEquals(reader.read(), 'X');
  77             assertEquals(reader.read(), 'Y');
  78             assertEquals(reader.read(), 'Z');
  79         }
  80     }
  81 }