author | hannesw |
Wed, 02 Apr 2014 18:26:57 +0200 | |
changeset 23767 | 7c0614b75e23 |
parent 20559 | 9244eb6d195b |
child 24206 | 40c6d45af73f |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 jdk.nashorn.internal.runtime; |
|
27 |
||
28 |
import java.io.ByteArrayOutputStream; |
|
29 |
import java.io.File; |
|
30 |
import java.io.IOError; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.Reader; |
|
34 |
import java.net.MalformedURLException; |
|
35 |
import java.net.URISyntaxException; |
|
36 |
import java.net.URL; |
|
37 |
import java.nio.charset.Charset; |
|
38 |
import java.nio.charset.StandardCharsets; |
|
39 |
import java.nio.file.Files; |
|
40 |
import java.nio.file.Path; |
|
41 |
import java.nio.file.Paths; |
|
23767 | 42 |
import java.security.MessageDigest; |
43 |
import java.security.NoSuchAlgorithmException; |
|
16147 | 44 |
import java.util.Arrays; |
45 |
import java.util.Objects; |
|
46 |
import jdk.nashorn.internal.parser.Token; |
|
47 |
||
48 |
/** |
|
49 |
* Source objects track the origin of JavaScript entities. |
|
50 |
* |
|
51 |
*/ |
|
52 |
public final class Source { |
|
53 |
/** |
|
54 |
* Descriptive name of the source as supplied by the user. Used for error |
|
55 |
* reporting to the user. For example, SyntaxError will use this to print message. |
|
56 |
* Used to implement __FILE__. Also used for SourceFile in .class for debugger usage. |
|
57 |
*/ |
|
58 |
private final String name; |
|
59 |
||
60 |
/** |
|
61 |
* Base directory the File or base part of the URL. Used to implement __DIR__. |
|
62 |
* Used to load scripts relative to the 'directory' or 'base' URL of current script. |
|
63 |
* This will be null when it can't be computed. |
|
64 |
*/ |
|
65 |
private final String base; |
|
66 |
||
67 |
/** Cached source content. */ |
|
68 |
private final char[] content; |
|
69 |
||
70 |
/** Length of source content. */ |
|
71 |
private final int length; |
|
72 |
||
73 |
/** Cached hash code */ |
|
74 |
private int hash; |
|
75 |
||
23767 | 76 |
/** Message digest */ |
77 |
private byte[] digest; |
|
78 |
||
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
79 |
/** Source URL if available */ |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
80 |
private final URL url; |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
81 |
|
16147 | 82 |
private static final int BUFSIZE = 8 * 1024; |
83 |
||
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
84 |
// Do *not* make this public ever! Trusts the URL and content. So has to be called |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
85 |
// from other public constructors. Note that this can not be some init method as |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
86 |
// we initialize final fields from here. |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
87 |
private Source(final String name, final String base, final char[] content, final URL url) { |
16147 | 88 |
this.name = name; |
89 |
this.base = base; |
|
90 |
this.content = content; |
|
91 |
this.length = content.length; |
|
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
92 |
this.url = url; |
16147 | 93 |
} |
94 |
||
95 |
/** |
|
96 |
* Constructor |
|
97 |
* |
|
98 |
* @param name source name |
|
99 |
* @param content contents as char array |
|
100 |
*/ |
|
101 |
public Source(final String name, final char[] content) { |
|
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
102 |
this(name, baseName(name, null), content, null); |
16147 | 103 |
} |
104 |
||
105 |
/** |
|
106 |
* Constructor |
|
107 |
* |
|
108 |
* @param name source name |
|
109 |
* @param content contents as string |
|
110 |
*/ |
|
111 |
public Source(final String name, final String content) { |
|
112 |
this(name, content.toCharArray()); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Constructor |
|
117 |
* |
|
118 |
* @param name source name |
|
119 |
* @param url url from which source can be loaded |
|
120 |
* |
|
121 |
* @throws IOException if source cannot be loaded |
|
122 |
*/ |
|
123 |
public Source(final String name, final URL url) throws IOException { |
|
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
124 |
this(name, baseURL(url, null), readFully(url), url); |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
125 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
126 |
|
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
127 |
/** |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
128 |
* Constructor |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
129 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
130 |
* @param name source name |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
131 |
* @param url url from which source can be loaded |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
132 |
* @param cs Charset used to convert bytes to chars |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
133 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
134 |
* @throws IOException if source cannot be loaded |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
135 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
136 |
public Source(final String name, final URL url, final Charset cs) throws IOException { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
137 |
this(name, baseURL(url, null), readFully(url, cs), url); |
16147 | 138 |
} |
139 |
||
140 |
/** |
|
141 |
* Constructor |
|
142 |
* |
|
143 |
* @param name source name |
|
144 |
* @param file file from which source can be loaded |
|
145 |
* |
|
146 |
* @throws IOException if source cannot be loaded |
|
147 |
*/ |
|
148 |
public Source(final String name, final File file) throws IOException { |
|
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
149 |
this(name, dirName(file, null), readFully(file), getURLFromFile(file)); |
16147 | 150 |
} |
151 |
||
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
152 |
/** |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
153 |
* Constructor |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
154 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
155 |
* @param name source name |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
156 |
* @param file file from which source can be loaded |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
157 |
* @param cs Charset used to convert bytes to chars |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
158 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
159 |
* @throws IOException if source cannot be loaded |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
160 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
161 |
public Source(final String name, final File file, final Charset cs) throws IOException { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
162 |
this(name, dirName(file, null), readFully(file, cs), getURLFromFile(file)); |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
163 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
164 |
|
16147 | 165 |
@Override |
166 |
public boolean equals(final Object obj) { |
|
167 |
if (this == obj) { |
|
168 |
return true; |
|
169 |
} |
|
170 |
||
171 |
if (!(obj instanceof Source)) { |
|
172 |
return false; |
|
173 |
} |
|
174 |
||
175 |
final Source src = (Source)obj; |
|
176 |
// Only compare content as a last resort measure |
|
20210
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19097
diff
changeset
|
177 |
return length == src.length && Objects.equals(url, src.url) && Objects.equals(name, src.name) && Arrays.equals(content, src.content); |
16147 | 178 |
} |
179 |
||
180 |
@Override |
|
181 |
public int hashCode() { |
|
182 |
int h = hash; |
|
183 |
if (h == 0) { |
|
184 |
h = hash = Arrays.hashCode(content) ^ Objects.hashCode(name); |
|
185 |
} |
|
186 |
return h; |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Fetch source content. |
|
191 |
* @return Source content. |
|
192 |
*/ |
|
193 |
public String getString() { |
|
194 |
return new String(content, 0, length); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Get the user supplied name of this script. |
|
199 |
* @return User supplied source name. |
|
200 |
*/ |
|
201 |
public String getName() { |
|
202 |
return name; |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Get the "directory" part of the file or "base" of the URL. |
|
207 |
* @return base of file or URL. |
|
208 |
*/ |
|
209 |
public String getBase() { |
|
210 |
return base; |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Fetch a portion of source content. |
|
215 |
* @param start start index in source |
|
216 |
* @param len length of portion |
|
217 |
* @return Source content portion. |
|
218 |
*/ |
|
219 |
public String getString(final int start, final int len) { |
|
220 |
return new String(content, start, len); |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Fetch a portion of source content associated with a token. |
|
225 |
* @param token Token descriptor. |
|
226 |
* @return Source content portion. |
|
227 |
*/ |
|
228 |
public String getString(final long token) { |
|
229 |
final int start = Token.descPosition(token); |
|
230 |
final int len = Token.descLength(token); |
|
231 |
return new String(content, start, len); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
235 |
* Returns the source URL of this script Source. Can be null if Source |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
236 |
* was created from a String or a char[]. |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
237 |
* |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
238 |
* @return URL source or null |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
239 |
*/ |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
240 |
public URL getURL() { |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
241 |
return url; |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
242 |
} |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
243 |
|
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
244 |
/** |
16147 | 245 |
* Find the beginning of the line containing position. |
246 |
* @param position Index to offending token. |
|
247 |
* @return Index of first character of line. |
|
248 |
*/ |
|
249 |
private int findBOLN(final int position) { |
|
250 |
for (int i = position - 1; i > 0; i--) { |
|
251 |
final char ch = content[i]; |
|
252 |
||
253 |
if (ch == '\n' || ch == '\r') { |
|
254 |
return i + 1; |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
return 0; |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Find the end of the line containing position. |
|
263 |
* @param position Index to offending token. |
|
264 |
* @return Index of last character of line. |
|
265 |
*/ |
|
266 |
private int findEOLN(final int position) { |
|
267 |
for (int i = position; i < length; i++) { |
|
268 |
final char ch = content[i]; |
|
269 |
||
270 |
if (ch == '\n' || ch == '\r') { |
|
271 |
return i - 1; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
return length - 1; |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* Return line number of character position. |
|
20559
9244eb6d195b
8025515: Performance issues with Source.getLine()
hannesw
parents:
20210
diff
changeset
|
280 |
* |
9244eb6d195b
8025515: Performance issues with Source.getLine()
hannesw
parents:
20210
diff
changeset
|
281 |
* <p>This method can be expensive for large sources as it iterates through |
9244eb6d195b
8025515: Performance issues with Source.getLine()
hannesw
parents:
20210
diff
changeset
|
282 |
* all characters up to {@code position}.</p> |
9244eb6d195b
8025515: Performance issues with Source.getLine()
hannesw
parents:
20210
diff
changeset
|
283 |
* |
16147 | 284 |
* @param position Position of character in source content. |
285 |
* @return Line number. |
|
286 |
*/ |
|
287 |
public int getLine(final int position) { |
|
288 |
// Line count starts at 1. |
|
289 |
int line = 1; |
|
290 |
||
291 |
for (int i = 0; i < position; i++) { |
|
292 |
final char ch = content[i]; |
|
293 |
// Works for both \n and \r\n. |
|
294 |
if (ch == '\n') { |
|
295 |
line++; |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
return line; |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Return column number of character position. |
|
304 |
* @param position Position of character in source content. |
|
305 |
* @return Column number. |
|
306 |
*/ |
|
307 |
public int getColumn(final int position) { |
|
308 |
// TODO - column needs to account for tabs. |
|
309 |
return position - findBOLN(position); |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Return line text including character position. |
|
314 |
* @param position Position of character in source content. |
|
315 |
* @return Line text. |
|
316 |
*/ |
|
317 |
public String getSourceLine(final int position) { |
|
318 |
// Find end of previous line. |
|
319 |
final int first = findBOLN(position); |
|
320 |
// Find end of this line. |
|
321 |
final int last = findEOLN(position); |
|
322 |
||
323 |
return new String(content, first, last - first + 1); |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* Get the content of this source as a char array |
|
328 |
* @return content |
|
329 |
*/ |
|
330 |
public char[] getContent() { |
|
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
331 |
return content.clone(); |
16147 | 332 |
} |
333 |
||
334 |
/** |
|
335 |
* Get the length in chars for this source |
|
336 |
* @return length |
|
337 |
*/ |
|
338 |
public int getLength() { |
|
339 |
return length; |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* Read all of the source until end of file. Return it as char array |
|
344 |
* |
|
345 |
* @param reader reader opened to source stream |
|
346 |
* @return source as content |
|
347 |
* |
|
348 |
* @throws IOException if source could not be read |
|
349 |
*/ |
|
350 |
public static char[] readFully(final Reader reader) throws IOException { |
|
351 |
final char[] arr = new char[BUFSIZE]; |
|
352 |
final StringBuilder sb = new StringBuilder(); |
|
353 |
||
354 |
try { |
|
355 |
int numChars; |
|
356 |
while ((numChars = reader.read(arr, 0, arr.length)) > 0) { |
|
357 |
sb.append(arr, 0, numChars); |
|
358 |
} |
|
359 |
} finally { |
|
360 |
reader.close(); |
|
361 |
} |
|
362 |
||
363 |
return sb.toString().toCharArray(); |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Read all of the source until end of file. Return it as char array |
|
368 |
* |
|
369 |
* @param file source file |
|
370 |
* @return source as content |
|
371 |
* |
|
372 |
* @throws IOException if source could not be read |
|
373 |
*/ |
|
374 |
public static char[] readFully(final File file) throws IOException { |
|
375 |
if (!file.isFile()) { |
|
376 |
throw new IOException(file + " is not a file"); //TODO localize? |
|
377 |
} |
|
378 |
return byteToCharArray(Files.readAllBytes(file.toPath())); |
|
379 |
} |
|
380 |
||
381 |
/** |
|
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
382 |
* Read all of the source until end of file. Return it as char array |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
383 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
384 |
* @param file source file |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
385 |
* @param cs Charset used to convert bytes to chars |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
386 |
* @return source as content |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
387 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
388 |
* @throws IOException if source could not be read |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
389 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
390 |
public static char[] readFully(final File file, final Charset cs) throws IOException { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
391 |
if (!file.isFile()) { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
392 |
throw new IOException(file + " is not a file"); //TODO localize? |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
393 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
394 |
|
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
395 |
final byte[] buf = Files.readAllBytes(file.toPath()); |
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
18337
diff
changeset
|
396 |
return (cs != null)? new String(buf, cs).toCharArray() : byteToCharArray(buf); |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
397 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
398 |
|
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
399 |
/** |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
400 |
* Read all of the source until end of stream from the given URL. Return it as char array |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
401 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
402 |
* @param url URL to read content from |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
403 |
* @return source as content |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
404 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
405 |
* @throws IOException if source could not be read |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
406 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
407 |
public static char[] readFully(final URL url) throws IOException { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
408 |
return readFully(url.openStream()); |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
409 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
410 |
|
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
411 |
/** |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
412 |
* Read all of the source until end of file. Return it as char array |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
413 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
414 |
* @param url URL to read content from |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
415 |
* @param cs Charset used to convert bytes to chars |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
416 |
* @return source as content |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
417 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
418 |
* @throws IOException if source could not be read |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
419 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
420 |
public static char[] readFully(final URL url, final Charset cs) throws IOException { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
421 |
return readFully(url.openStream(), cs); |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
422 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
423 |
|
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
424 |
/** |
23767 | 425 |
* Get a message digest for this source. |
426 |
* |
|
427 |
* @return a message digest for this source |
|
428 |
*/ |
|
429 |
public synchronized byte[] getDigest() { |
|
430 |
if (digest == null) { |
|
431 |
||
432 |
final byte[] bytes = new byte[content.length * 2]; |
|
433 |
||
434 |
for (int i = 0; i < content.length; i++) { |
|
435 |
bytes[i * 2] = (byte) (content[i] & 0x00ff); |
|
436 |
bytes[i * 2 + 1] = (byte) ((content[i] & 0xff00) >> 8); |
|
437 |
} |
|
438 |
||
439 |
try { |
|
440 |
final MessageDigest md = MessageDigest.getInstance("SHA-1"); |
|
441 |
if (name != null) { |
|
442 |
md.update(name.getBytes(StandardCharsets.UTF_8)); |
|
443 |
} |
|
444 |
if (base != null) { |
|
445 |
md.update(base.getBytes(StandardCharsets.UTF_8)); |
|
446 |
} |
|
447 |
if (url != null) { |
|
448 |
md.update(url.toString().getBytes(StandardCharsets.UTF_8)); |
|
449 |
} |
|
450 |
digest = md.digest(bytes); |
|
451 |
} catch (NoSuchAlgorithmException e) { |
|
452 |
throw new RuntimeException(e); |
|
453 |
} |
|
454 |
} |
|
455 |
return digest; |
|
456 |
} |
|
457 |
||
458 |
/** |
|
16147 | 459 |
* Get the base url. This is currently used for testing only |
460 |
* @param url a URL |
|
461 |
* @return base URL for url |
|
462 |
*/ |
|
463 |
public static String baseURL(final URL url) { |
|
464 |
return baseURL(url, null); |
|
465 |
} |
|
466 |
||
467 |
private static String baseURL(final URL url, final String defaultValue) { |
|
468 |
if (url.getProtocol().equals("file")) { |
|
469 |
try { |
|
470 |
final Path path = Paths.get(url.toURI()); |
|
471 |
final Path parent = path.getParent(); |
|
472 |
return (parent != null) ? (parent + File.separator) : defaultValue; |
|
473 |
} catch (final SecurityException | URISyntaxException | IOError e) { |
|
474 |
return defaultValue; |
|
475 |
} |
|
476 |
} |
|
477 |
||
478 |
// FIXME: is there a better way to find 'base' URL of a given URL? |
|
479 |
String path = url.getPath(); |
|
480 |
if (path.isEmpty()) { |
|
481 |
return defaultValue; |
|
482 |
} |
|
483 |
path = path.substring(0, path.lastIndexOf('/') + 1); |
|
484 |
final int port = url.getPort(); |
|
485 |
try { |
|
486 |
return new URL(url.getProtocol(), url.getHost(), port, path).toString(); |
|
487 |
} catch (final MalformedURLException e) { |
|
488 |
return defaultValue; |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
private static String dirName(final File file, final String defaultValue) { |
|
493 |
final String res = file.getParent(); |
|
494 |
return (res != null)? (res + File.separator) : defaultValue; |
|
495 |
} |
|
496 |
||
497 |
// fake directory like name |
|
498 |
private static String baseName(final String name, final String defaultValue) { |
|
499 |
int idx = name.lastIndexOf('/'); |
|
500 |
if (idx == -1) { |
|
501 |
idx = name.lastIndexOf('\\'); |
|
502 |
} |
|
503 |
return (idx != -1)? name.substring(0, idx + 1) : defaultValue; |
|
504 |
} |
|
505 |
||
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
506 |
private static char[] readFully(final InputStream is, final Charset cs) throws IOException { |
19097
f544a2ea40ef
8021262: Make nashorn access checks consistent with underlying dynalink
sundar
parents:
18337
diff
changeset
|
507 |
return (cs != null)? new String(readBytes(is), cs).toCharArray() : readFully(is); |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
508 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16241
diff
changeset
|
509 |
|
16147 | 510 |
private static char[] readFully(final InputStream is) throws IOException { |
511 |
return byteToCharArray(readBytes(is)); |
|
512 |
} |
|
513 |
||
514 |
private static char[] byteToCharArray(final byte[] bytes) { |
|
515 |
Charset cs = StandardCharsets.UTF_8; |
|
516 |
int start = 0; |
|
517 |
// BOM detection. |
|
518 |
if (bytes.length > 1 && bytes[0] == (byte)0xFE && bytes[1] == (byte)0xFF) { |
|
519 |
start = 2; |
|
520 |
cs = StandardCharsets.UTF_16BE; |
|
521 |
} else if (bytes.length > 1 && bytes[0] == (byte)0xFF && bytes[1] == (byte)0xFE) { |
|
522 |
start = 2; |
|
523 |
cs = StandardCharsets.UTF_16LE; |
|
524 |
} else if (bytes.length > 2 && bytes[0] == (byte)0xEF && bytes[1] == (byte)0xBB && bytes[2] == (byte)0xBF) { |
|
525 |
start = 3; |
|
526 |
cs = StandardCharsets.UTF_8; |
|
527 |
} else if (bytes.length > 3 && bytes[0] == (byte)0xFF && bytes[1] == (byte)0xFE && bytes[2] == 0 && bytes[3] == 0) { |
|
528 |
start = 4; |
|
529 |
cs = Charset.forName("UTF-32LE"); |
|
530 |
} else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte)0xFE && bytes[3] == (byte)0xFF) { |
|
531 |
start = 4; |
|
532 |
cs = Charset.forName("UTF-32BE"); |
|
533 |
} |
|
534 |
||
535 |
return new String(bytes, start, bytes.length - start, cs).toCharArray(); |
|
536 |
} |
|
537 |
||
538 |
static byte[] readBytes(final InputStream is) throws IOException { |
|
539 |
final byte[] arr = new byte[BUFSIZE]; |
|
540 |
try { |
|
541 |
try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) { |
|
542 |
int numBytes; |
|
543 |
while ((numBytes = is.read(arr, 0, arr.length)) > 0) { |
|
544 |
buf.write(arr, 0, numBytes); |
|
545 |
} |
|
546 |
return buf.toByteArray(); |
|
547 |
} |
|
548 |
} finally { |
|
549 |
is.close(); |
|
550 |
} |
|
551 |
} |
|
552 |
||
553 |
@Override |
|
554 |
public String toString() { |
|
555 |
return getName(); |
|
556 |
} |
|
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
557 |
|
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
558 |
private static URL getURLFromFile(final File file) { |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
559 |
try { |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
560 |
return file.toURI().toURL(); |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
561 |
} catch (final SecurityException | MalformedURLException ignored) { |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
562 |
return null; |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
563 |
} |
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16151
diff
changeset
|
564 |
} |
16147 | 565 |
} |