Path: blob/master/test/jdk/java/io/StreamTokenizer/ReadAhead.java
41149 views
/*1* Copyright (c) 1999, 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 415073725@summary Ensure that StreamTokenizer does not read any further ahead26than is absolutely necessary27*/2829import java.io.ByteArrayInputStream;30import java.io.InputStream;31import java.io.InputStreamReader;32import java.io.Reader;33import java.io.StreamTokenizer;34import java.io.IOException;353637public class ReadAhead {383940/* An InputStream subclass that cannot read past a given limit */41private static class LimitedInputStream extends InputStream {4243private String input;44private int limit; /* Do not allow input[limit] to be read */45private int next = 0;4647public LimitedInputStream(String input, int limit) {48this.input = input;49this.limit = limit;50}5152public int read() throws IOException {53if (next >= limit)54throw new IOException("Attempted to read too far in stream");55return input.charAt(next++);56}5758}596061/* A Reader subclass that cannot read past a given limit */62private static class LimitedReader extends Reader {6364private String input;65private int limit; /* Do not allow input[limit] to be read */66private int next = 0;6768public LimitedReader(String input, int limit) {69this.input = input;70this.limit = limit;71}7273public int read() throws IOException {74if (next >= limit)75throw new IOException("Attempted to read too far in stream");76return input.charAt(next++);77}7879public int read(char[] b, int off, int len) throws IOException {80int top = off + len;81int i;82for (i = off; i < top; i++) {83int c = read();84if (c < 0) break;85b[i] = (char)c;86}87return i - off;88}8990public void close() { }9192}939495/* Interface for objects that can create new StreamTokenizers96with a given limited input */97private static interface StreamTokenizerMaker {98public StreamTokenizer create(String input, int limit);99}100101private static void fail(String why) throws Exception {102throw new Exception(why);103}104105private static void test(StreamTokenizer st) throws Exception {106st.eolIsSignificant(true);107int tt = st.nextToken();108if (tt != StreamTokenizer.TT_WORD) fail("expected TT_WORD");109if (!st.sval.equals("foo")) fail("expected word token \"foo\"");110tt = st.nextToken();111if (tt != StreamTokenizer.TT_EOL) fail("expected TT_EOL");112}113114private static void test(StreamTokenizerMaker stm) throws Exception {115test(stm.create("foo\nx", 4));116test(stm.create("foo\r\nx", 4));117}118119120public static void main(String[] args) throws Exception {121122/* InputStream case */123test(new StreamTokenizerMaker() {124public StreamTokenizer create(String input, int limit) {125return new StreamTokenizer(new LimitedInputStream(input, limit));126}});127128/* Reader case */129test(new StreamTokenizerMaker() {130public StreamTokenizer create(String input, int limit) {131return new StreamTokenizer(new LimitedReader(input, limit));132}});133134}135136}137138139