MarkReset.java revision 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * This code is free software; you can redistribute it and/or modify it
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * under the terms of the GNU General Public License version 2 only, as
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * published by the Free Software Foundation.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * This code is distributed in the hope that it will be useful, but WITHOUT
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * version 2 for more details (a copy is included in the LICENSE file that
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * accompanied this code).
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * You should have received a copy of the GNU General Public License version
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * 2 along with this work; if not, write to the Free Software Foundation,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
d036ca9de6fb0fb6f9ac715f7f76e2172faaf6ccCharles Sparey * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d036ca9de6fb0fb6f9ac715f7f76e2172faaf6ccCharles Sparey * CA 95054 USA or visit www.sun.com if you need additional information or
d036ca9de6fb0fb6f9ac715f7f76e2172faaf6ccCharles Sparey * have any questions.
d036ca9de6fb0fb6f9ac715f7f76e2172faaf6ccCharles Sparey */
d036ca9de6fb0fb6f9ac715f7f76e2172faaf6ccCharles Sparey
d036ca9de6fb0fb6f9ac715f7f76e2172faaf6ccCharles Sparey/* @test
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster @bug 4081733
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster @summary Make sure LineNumberReader returns right line number
when mark and reset are used
*/
import java.io.*;
public class MarkReset {
/**
* This program creates a LineNumberReader and tries to find all
* the non-whitespace characters in the file.
*/
public static void main(String[] args) throws Exception {
int n, line;
LineNumberReader reader = new LineNumberReader
(new StringReader("0\r\n1\r2\n3\r\n\r5\r\r7\n\n9"));
for (n = 0; n < 7; n++) {
skipWhiteSpace(reader); /* Skip all whitespace */
int c = reader.read(); /* Read the non-whitespace character */
if (c < 0) { /* Might be eof */
break; /* It is. Get out of the loop */
}
line = reader.getLineNumber();
if(line != (c - 48)) {
throw new Exception("Failed test : Line number expected "
+ (c - 48) + " got " + line );
}
}
}
/**
* Skip whitespace in the file. Mark and reset
*/
private static void skipWhiteSpace(LineNumberReader reader) throws IOException {
while (true) {
/* Mark in case the character is not whitespace */
reader.mark(10);
/* Read the character */
int c = reader.read();
if (Character.isWhitespace((char) c)) {
/* Loop while in whitespace */
continue;
}
/* Return to the non-whitespace character */
reader.reset();
break;
}
}
}