author | sundar |
Thu, 28 Jun 2018 18:04:19 +0530 | |
changeset 50878 | fb7800b66c92 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
1 |
/* |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
4 |
* |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
10 |
* |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
15 |
* accompanied this code). |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
16 |
* |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
20 |
* |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
23 |
* questions. |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
24 |
*/ |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
25 |
|
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
26 |
package jdk.nashorn.api.scripting; |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
27 |
|
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
28 |
import java.io.CharArrayReader; |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
29 |
import java.io.IOException; |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
30 |
import java.io.Reader; |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
31 |
import java.net.URL; |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
32 |
import java.nio.charset.Charset; |
28785
a503c972d4bd
8072595: nashorn should not use obj.getClass() for null checks
sundar
parents:
28317
diff
changeset
|
33 |
import java.util.Objects; |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
34 |
import jdk.nashorn.internal.runtime.Source; |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
35 |
|
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
36 |
/** |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
37 |
* A Reader that reads from a URL. Used to make sure that the reader |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
38 |
* reads content from given URL and can be trusted to do so. |
28317
0aeeae75e696
8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents:
25865
diff
changeset
|
39 |
* |
50878
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
40 |
* @deprecated Nashorn JavaScript script engine and APIs, and the jjs tool |
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
41 |
* are deprecated with the intent to remove them in a future release. |
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
42 |
* |
28317
0aeeae75e696
8068431: @since and @jdk.Exported are missing in jdk.nashorn.api.scripting classes and package-info.java files
sundar
parents:
25865
diff
changeset
|
43 |
* @since 1.8u40 |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
44 |
*/ |
50878
fb7800b66c92
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents:
47216
diff
changeset
|
45 |
@Deprecated(since="11", forRemoval=true) |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
46 |
public final class URLReader extends Reader { |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
47 |
// underlying URL |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
48 |
private final URL url; |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
49 |
// Charset used to convert |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
50 |
private final Charset cs; |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
51 |
|
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
52 |
// lazily initialized underlying reader for URL |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
53 |
private Reader reader; |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
54 |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16243
diff
changeset
|
55 |
/** |
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16243
diff
changeset
|
56 |
* Constructor |
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16243
diff
changeset
|
57 |
* |
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16243
diff
changeset
|
58 |
* @param url URL for this URLReader |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
59 |
* @throws NullPointerException if url is null |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16243
diff
changeset
|
60 |
*/ |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
61 |
public URLReader(final URL url) { |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
62 |
this(url, (Charset)null); |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
63 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
64 |
|
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
65 |
/** |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
66 |
* Constructor |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
67 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
68 |
* @param url URL for this URLReader |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
69 |
* @param charsetName Name of the Charset used to convert bytes to chars |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
70 |
* @throws NullPointerException if url is null |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
71 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
72 |
public URLReader(final URL url, final String charsetName) { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
73 |
this(url, Charset.forName(charsetName)); |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
74 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
75 |
|
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
76 |
/** |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
77 |
* Constructor |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
78 |
* |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
79 |
* @param url URL for this URLReader |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
80 |
* @param cs Charset used to convert bytes to chars |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
81 |
* @throws NullPointerException if url is null |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
82 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
83 |
public URLReader(final URL url, final Charset cs) { |
31094
7a9fa21da791
8085810: Return value of Objects.requireNonNull call can be used
sundar
parents:
28785
diff
changeset
|
84 |
this.url = Objects.requireNonNull(url); |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
85 |
this.cs = cs; |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
86 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
87 |
|
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
88 |
@Override |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
18337
diff
changeset
|
89 |
public int read(final char cbuf[], final int off, final int len) throws IOException { |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
90 |
return getReader().read(cbuf, off, len); |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
91 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
92 |
|
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
93 |
@Override |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
94 |
public void close() throws IOException { |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
95 |
getReader().close(); |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
96 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
97 |
|
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
98 |
/** |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
99 |
* URL of this reader |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
100 |
* @return the URL from which this reader reads. |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
101 |
*/ |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
102 |
public URL getURL() { |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
103 |
return url; |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
104 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
105 |
|
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
106 |
/** |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
107 |
* Charset used by this reader |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
108 |
* |
32534
b3ec7f3b3c2a
8136349: Typos patch for nashorn sources submitted on Sep 10, 2015
sundar
parents:
31094
diff
changeset
|
109 |
* @return the Charset used to convert bytes to chars |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
110 |
*/ |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
111 |
public Charset getCharset() { |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
112 |
return cs; |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
113 |
} |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
114 |
|
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
115 |
// lazily initialize char array reader using URL content |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
116 |
private Reader getReader() throws IOException { |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
117 |
synchronized (lock) { |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
118 |
if (reader == null) { |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
16256
diff
changeset
|
119 |
reader = new CharArrayReader(Source.readFully(url, cs)); |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
120 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
121 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
122 |
|
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
123 |
return reader; |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
124 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
diff
changeset
|
125 |
} |