Path: blob/master/test/jdk/java/io/charStreams/BufferedReaderMark.java
41149 views
/*1* Copyright (c) 1997, 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@summary General tests for BufferedReader.mark25*/2627import java.io.*;282930public class BufferedReaderMark {3132static int bufferSize = -1;33static int inputSize = -1;3435static String d2(int n) {36String s = Integer.toString(n);37if (s.length() < 2)38return s + " ";39else40return s;41}4243static void fail(String s) {44throw new RuntimeException(s);45}4647public static void main(String[] args) throws Exception {48PrintStream log = System.err;4950if (bufferSize == -1)51bufferSize = 7;52if (inputSize == -1)53inputSize = bufferSize * 3;5455try {56Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize);57char buf[] = new char[bufferSize];58if (in.read(buf) != bufferSize)59fail("Read failed");60in.reset();61fail("reset() didn't throw");62}63catch (IOException x) {64if (x.getMessage().equals("Stream not marked"))65log.println("Unmarked-stream test OK");66else67fail(x.toString());68}6970boolean err = false;71try {72Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize);73char buf[] = new char[bufferSize];74if (in.read() == -1)75fail("Read 1 failed");76in.mark(bufferSize);77while (in.read() != -1);78in.reset();79fail("reset() didn't throw");80}81catch (IOException x) {82if (x.getMessage().equals("Mark invalid")) {83err = true;84log.println("Invalid-mark test OK");85}86else {87log.println("Invalid-mark test failed: " + x);88fail(x.toString());89}90}91if (! err)92fail("Invalid-mark test failed: Exception not thrown");9394int c;95for (int off = 0; off < bufferSize * 2; off++) {96for (int ra = 0; ra <= inputSize + 2; ra++) {97Reader in = new BufferedReader(new ABCReader(inputSize), bufferSize);9899log.print(d2(off) + ", " + d2(ra) + " mark: ");100for (int i = 0; i < off; i++) {101if ((c = in.read()) == -1) {102log.print("<EOF>");103break;104}105log.print((char) c);106}107in.mark(ra);108for (int i = 0; i < ra; i++) {109if ((c = in.read()) == -1) {110log.print("<EOF>");111break;112}113log.print((char) c);114}115log.println();116117log.print(d2(off) + ", " + d2(ra) + " reset: ");118in.reset();119for (int i = 0; i < off; i++)120log.print(' ');121for (;;) {122if ((c = in.read()) == -1) {123log.print("<EOF>");124break;125}126log.print((char) c);127}128log.println();129130}131}132}133134}135136137