Path: blob/master/test/jdk/java/io/LineNumberReader/MarkReset.java
41149 views
/*1* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 408173325@summary Make sure LineNumberReader returns right line number26when mark and reset are used27*/282930import java.io.*;3132public class MarkReset {3334/**35* This program creates a LineNumberReader and tries to find all36* the non-whitespace characters in the file.37*/38public static void main(String[] args) throws Exception {39int n, line;4041LineNumberReader reader = new LineNumberReader42(new StringReader("0\r\n1\r2\n3\r\n\r5\r\r7\n\n9"));43for (n = 0; n < 7; n++) {44skipWhiteSpace(reader); /* Skip all whitespace */45int c = reader.read(); /* Read the non-whitespace character */46if (c < 0) { /* Might be eof */47break; /* It is. Get out of the loop */48}49line = reader.getLineNumber();50if(line != (c - 48)) {51throw new Exception("Failed test : Line number expected "52+ (c - 48) + " got " + line );53}54}55}5657/**58* Skip whitespace in the file. Mark and reset59*/60private static void skipWhiteSpace(LineNumberReader reader) throws IOException {61while (true) {62/* Mark in case the character is not whitespace */63reader.mark(10);64/* Read the character */65int c = reader.read();66if (Character.isWhitespace((char) c)) {67/* Loop while in whitespace */68continue;69}7071/* Return to the non-whitespace character */72reader.reset();73break;74}75}76}777879