Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/java/ScannerInputReader.java
38918 views
/*1* Copyright (c) 1995, 2003, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.java;2627import java.io.IOException;28import java.io.InputStream;29import java.io.InputStreamReader;30import java.io.BufferedReader;31import java.io.FilterReader;32import java.io.UnsupportedEncodingException;3334/**35* An input stream for java programs. The stream treats either "\n", "\r"36* or "\r\n" as the end of a line, it always returns \n. It also parses37* UNICODE characters expressed as \uffff. However, if it sees "\\", the38* second slash cannot begin a unicode sequence. It keeps track of the current39* position in the input stream.40*41* WARNING: The contents of this source file are not part of any42* supported API. Code that depends on them does so at its own risk:43* they are subject to change or removal without notice.44*45* @author Arthur van Hoff46*/4748public49class ScannerInputReader extends FilterReader implements Constants {50// A note. This class does not really properly subclass FilterReader.51// Since this class only overrides the single character read method,52// and not the multi-character read method, any use of the latter53// will not work properly. Any attempt to use this code outside of54// the compiler should take that into account.55//56// For efficiency, it might be worth moving this code to Scanner and57// getting rid of this class.5859Environment env;60long pos;6162private long chpos;63private int pushBack = -1;6465public ScannerInputReader(Environment env, InputStream in)66throws UnsupportedEncodingException67{68// ScannerInputStream has been modified to no longer use69// BufferedReader. It now does its own buffering for70// performance.71super(env.getCharacterEncoding() != null ?72new InputStreamReader(in, env.getCharacterEncoding()) :73new InputStreamReader(in));7475// Start out the buffer empty.76currentIndex = 0;77numChars = 0;7879this.env = env;80chpos = Scanner.LINEINC;81}8283//------------------------------------------------------------84// Buffering code.8586// The size of our buffer.87private static final int BUFFERLEN = 10 * 1024;8889// A character buffer.90private final char[] buffer = new char[BUFFERLEN];9192// The index of the next character to be "read" from the buffer.93private int currentIndex;9495// The number of characters in the buffer. -1 if EOF is reached.96private int numChars;9798/**99* Get the next character from our buffer.100* Note: this method has been inlined by hand in the `read' method101* below. Any changes made to this method should be equally applied102* to that code.103*/104private int getNextChar() throws IOException {105// Check to see if we have either run out of characters in our106// buffer or gotten to EOF on a previous call.107if (currentIndex >= numChars) {108numChars = in.read(buffer);109if (numChars == -1) {110// We have reached EOF.111return -1;112}113114// No EOF. currentIndex points to first char in buffer.115currentIndex = 0;116}117118return buffer[currentIndex++];119}120121//------------------------------------------------------------122123public int read(char[] buffer, int off, int len) {124throw new CompilerError(125"ScannerInputReader is not a fully implemented reader.");126}127128public int read() throws IOException {129pos = chpos;130chpos += Scanner.OFFSETINC;131132int c = pushBack;133if (c == -1) {134getchar: try {135// Here the call...136// c = getNextChar();137// has been inlined by hand for performance.138139if (currentIndex >= numChars) {140numChars = in.read(buffer);141if (numChars == -1) {142// We have reached EOF.143c = -1;144break getchar;145}146147// No EOF. currentIndex points to first char in buffer.148currentIndex = 0;149}150c = buffer[currentIndex++];151152} catch (java.io.CharConversionException e) {153env.error(pos, "invalid.encoding.char");154// this is fatal error155return -1;156}157} else {158pushBack = -1;159}160161// parse special characters162switch (c) {163case -2:164// -2 is a special code indicating a pushback of a backslash that165// definitely isn't the start of a unicode sequence.166return '\\';167168case '\\':169if ((c = getNextChar()) != 'u') {170pushBack = (c == '\\' ? -2 : c);171return '\\';172}173// we have a unicode sequence174chpos += Scanner.OFFSETINC;175while ((c = getNextChar()) == 'u') {176chpos += Scanner.OFFSETINC;177}178179// unicode escape sequence180int d = 0;181for (int i = 0 ; i < 4 ; i++, chpos += Scanner.OFFSETINC, c = getNextChar()) {182switch (c) {183case '0': case '1': case '2': case '3': case '4':184case '5': case '6': case '7': case '8': case '9':185d = (d << 4) + c - '0';186break;187188case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':189d = (d << 4) + 10 + c - 'a';190break;191192case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':193d = (d << 4) + 10 + c - 'A';194break;195196default:197env.error(pos, "invalid.escape.char");198pushBack = c;199return d;200}201}202pushBack = c;203204// To read the following line, switch \ and /...205// Handle /u000a, /u000A, /u000d, /u000D properly as206// line terminators as per JLS 3.4, even though they are encoded207// (this properly respects the order given in JLS 3.2).208switch (d) {209case '\n':210chpos += Scanner.LINEINC;211return '\n';212case '\r':213if ((c = getNextChar()) != '\n') {214pushBack = c;215} else {216chpos += Scanner.OFFSETINC;217}218chpos += Scanner.LINEINC;219return '\n';220default:221return d;222}223224case '\n':225chpos += Scanner.LINEINC;226return '\n';227228case '\r':229if ((c = getNextChar()) != '\n') {230pushBack = c;231} else {232chpos += Scanner.OFFSETINC;233}234chpos += Scanner.LINEINC;235return '\n';236237default:238return c;239}240}241}242243244