1/*
2 * Copyright (c) 1998, 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 4081733
26   @summary Make sure LineNumberReader returns right line number
27            when mark and reset are used
28   */
29
30
31import java.io.*;
32
33public class MarkReset {
34
35    /**
36     * This program creates a LineNumberReader and tries to find all
37     * the non-whitespace characters in the file.
38     */
39    public static void main(String[] args) throws Exception {
40        int n, line;
41
42        LineNumberReader reader = new LineNumberReader
43                                      (new StringReader("0\r\n1\r2\n3\r\n\r5\r\r7\n\n9"));
44        for (n = 0; n < 7; n++) {
45            skipWhiteSpace(reader);     /* Skip all whitespace */
46            int c = reader.read();      /* Read the non-whitespace character */
47            if (c < 0) {                /* Might be eof */
48                break;                  /* It is. Get out of the loop */
49            }
50            line = reader.getLineNumber();
51            if(line != (c - 48)) {
52                throw new Exception("Failed test : Line number expected "
53                                    + (c - 48)  + " got " + line );
54            }
55        }
56    }
57
58    /**
59     * Skip whitespace in the file. Mark and reset
60     */
61    private static void skipWhiteSpace(LineNumberReader reader) throws IOException {
62        while (true) {
63            /* Mark in case the character is not whitespace */
64            reader.mark(10);
65            /* Read the character */
66            int c = reader.read();
67            if (Character.isWhitespace((char) c)) {
68                /* Loop while in whitespace */
69                continue;
70            }
71
72            /* Return to the non-whitespace character */
73            reader.reset();
74            break;
75        }
76    }
77}
78