Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/java/ScannerInputReader.java
38918 views
1
/*
2
* Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.tools.java;
27
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.InputStreamReader;
31
import java.io.BufferedReader;
32
import java.io.FilterReader;
33
import java.io.UnsupportedEncodingException;
34
35
/**
36
* An input stream for java programs. The stream treats either "\n", "\r"
37
* or "\r\n" as the end of a line, it always returns \n. It also parses
38
* UNICODE characters expressed as \uffff. However, if it sees "\\", the
39
* second slash cannot begin a unicode sequence. It keeps track of the current
40
* position in the input stream.
41
*
42
* WARNING: The contents of this source file are not part of any
43
* supported API. Code that depends on them does so at its own risk:
44
* they are subject to change or removal without notice.
45
*
46
* @author Arthur van Hoff
47
*/
48
49
public
50
class ScannerInputReader extends FilterReader implements Constants {
51
// A note. This class does not really properly subclass FilterReader.
52
// Since this class only overrides the single character read method,
53
// and not the multi-character read method, any use of the latter
54
// will not work properly. Any attempt to use this code outside of
55
// the compiler should take that into account.
56
//
57
// For efficiency, it might be worth moving this code to Scanner and
58
// getting rid of this class.
59
60
Environment env;
61
long pos;
62
63
private long chpos;
64
private int pushBack = -1;
65
66
public ScannerInputReader(Environment env, InputStream in)
67
throws UnsupportedEncodingException
68
{
69
// ScannerInputStream has been modified to no longer use
70
// BufferedReader. It now does its own buffering for
71
// performance.
72
super(env.getCharacterEncoding() != null ?
73
new InputStreamReader(in, env.getCharacterEncoding()) :
74
new InputStreamReader(in));
75
76
// Start out the buffer empty.
77
currentIndex = 0;
78
numChars = 0;
79
80
this.env = env;
81
chpos = Scanner.LINEINC;
82
}
83
84
//------------------------------------------------------------
85
// Buffering code.
86
87
// The size of our buffer.
88
private static final int BUFFERLEN = 10 * 1024;
89
90
// A character buffer.
91
private final char[] buffer = new char[BUFFERLEN];
92
93
// The index of the next character to be "read" from the buffer.
94
private int currentIndex;
95
96
// The number of characters in the buffer. -1 if EOF is reached.
97
private int numChars;
98
99
/**
100
* Get the next character from our buffer.
101
* Note: this method has been inlined by hand in the `read' method
102
* below. Any changes made to this method should be equally applied
103
* to that code.
104
*/
105
private int getNextChar() throws IOException {
106
// Check to see if we have either run out of characters in our
107
// buffer or gotten to EOF on a previous call.
108
if (currentIndex >= numChars) {
109
numChars = in.read(buffer);
110
if (numChars == -1) {
111
// We have reached EOF.
112
return -1;
113
}
114
115
// No EOF. currentIndex points to first char in buffer.
116
currentIndex = 0;
117
}
118
119
return buffer[currentIndex++];
120
}
121
122
//------------------------------------------------------------
123
124
public int read(char[] buffer, int off, int len) {
125
throw new CompilerError(
126
"ScannerInputReader is not a fully implemented reader.");
127
}
128
129
public int read() throws IOException {
130
pos = chpos;
131
chpos += Scanner.OFFSETINC;
132
133
int c = pushBack;
134
if (c == -1) {
135
getchar: try {
136
// Here the call...
137
// c = getNextChar();
138
// has been inlined by hand for performance.
139
140
if (currentIndex >= numChars) {
141
numChars = in.read(buffer);
142
if (numChars == -1) {
143
// We have reached EOF.
144
c = -1;
145
break getchar;
146
}
147
148
// No EOF. currentIndex points to first char in buffer.
149
currentIndex = 0;
150
}
151
c = buffer[currentIndex++];
152
153
} catch (java.io.CharConversionException e) {
154
env.error(pos, "invalid.encoding.char");
155
// this is fatal error
156
return -1;
157
}
158
} else {
159
pushBack = -1;
160
}
161
162
// parse special characters
163
switch (c) {
164
case -2:
165
// -2 is a special code indicating a pushback of a backslash that
166
// definitely isn't the start of a unicode sequence.
167
return '\\';
168
169
case '\\':
170
if ((c = getNextChar()) != 'u') {
171
pushBack = (c == '\\' ? -2 : c);
172
return '\\';
173
}
174
// we have a unicode sequence
175
chpos += Scanner.OFFSETINC;
176
while ((c = getNextChar()) == 'u') {
177
chpos += Scanner.OFFSETINC;
178
}
179
180
// unicode escape sequence
181
int d = 0;
182
for (int i = 0 ; i < 4 ; i++, chpos += Scanner.OFFSETINC, c = getNextChar()) {
183
switch (c) {
184
case '0': case '1': case '2': case '3': case '4':
185
case '5': case '6': case '7': case '8': case '9':
186
d = (d << 4) + c - '0';
187
break;
188
189
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
190
d = (d << 4) + 10 + c - 'a';
191
break;
192
193
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
194
d = (d << 4) + 10 + c - 'A';
195
break;
196
197
default:
198
env.error(pos, "invalid.escape.char");
199
pushBack = c;
200
return d;
201
}
202
}
203
pushBack = c;
204
205
// To read the following line, switch \ and /...
206
// Handle /u000a, /u000A, /u000d, /u000D properly as
207
// line terminators as per JLS 3.4, even though they are encoded
208
// (this properly respects the order given in JLS 3.2).
209
switch (d) {
210
case '\n':
211
chpos += Scanner.LINEINC;
212
return '\n';
213
case '\r':
214
if ((c = getNextChar()) != '\n') {
215
pushBack = c;
216
} else {
217
chpos += Scanner.OFFSETINC;
218
}
219
chpos += Scanner.LINEINC;
220
return '\n';
221
default:
222
return d;
223
}
224
225
case '\n':
226
chpos += Scanner.LINEINC;
227
return '\n';
228
229
case '\r':
230
if ((c = getNextChar()) != '\n') {
231
pushBack = c;
232
} else {
233
chpos += Scanner.OFFSETINC;
234
}
235
chpos += Scanner.LINEINC;
236
return '\n';
237
238
default:
239
return c;
240
}
241
}
242
}
243
244