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.api.scripting;
27
28 import java.io.CharArrayReader;
29 import java.io.IOException;
30 import java.io.Reader;
31 import java.net.URL;
32 import java.nio.charset.Charset;
33 import jdk.nashorn.internal.runtime.Source;
34
35 /**
36 * A Reader that reads from a URL. Used to make sure that the reader
37 * reads content from given URL and can be trusted to do so.
38 *
39 * @since 1.8u40
40 */
41 @jdk.Exported
42 public final class URLReader extends Reader {
43 // underlying URL
44 private final URL url;
45 // Charset used to convert
46 private final Charset cs;
47
48 // lazily initialized underlying reader for URL
49 private Reader reader;
50
51 /**
52 * Constructor
60
61 /**
62 * Constructor
63 *
64 * @param url URL for this URLReader
65 * @param charsetName Name of the Charset used to convert bytes to chars
66 * @throws NullPointerException if url is null
67 */
68 public URLReader(final URL url, final String charsetName) {
69 this(url, Charset.forName(charsetName));
70 }
71
72 /**
73 * Constructor
74 *
75 * @param url URL for this URLReader
76 * @param cs Charset used to convert bytes to chars
77 * @throws NullPointerException if url is null
78 */
79 public URLReader(final URL url, final Charset cs) {
80 // null check
81 url.getClass();
82 this.url = url;
83 this.cs = cs;
84 }
85
86 @Override
87 public int read(final char cbuf[], final int off, final int len) throws IOException {
88 return getReader().read(cbuf, off, len);
89 }
90
91 @Override
92 public void close() throws IOException {
93 getReader().close();
94 }
95
96 /**
97 * URL of this reader
98 * @return the URL from which this reader reads.
99 */
100 public URL getURL() {
101 return url;
|
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.api.scripting;
27
28 import java.io.CharArrayReader;
29 import java.io.IOException;
30 import java.io.Reader;
31 import java.net.URL;
32 import java.nio.charset.Charset;
33 import java.util.Objects;
34 import jdk.nashorn.internal.runtime.Source;
35
36 /**
37 * A Reader that reads from a URL. Used to make sure that the reader
38 * reads content from given URL and can be trusted to do so.
39 *
40 * @since 1.8u40
41 */
42 @jdk.Exported
43 public final class URLReader extends Reader {
44 // underlying URL
45 private final URL url;
46 // Charset used to convert
47 private final Charset cs;
48
49 // lazily initialized underlying reader for URL
50 private Reader reader;
51
52 /**
53 * Constructor
61
62 /**
63 * Constructor
64 *
65 * @param url URL for this URLReader
66 * @param charsetName Name of the Charset used to convert bytes to chars
67 * @throws NullPointerException if url is null
68 */
69 public URLReader(final URL url, final String charsetName) {
70 this(url, Charset.forName(charsetName));
71 }
72
73 /**
74 * Constructor
75 *
76 * @param url URL for this URLReader
77 * @param cs Charset used to convert bytes to chars
78 * @throws NullPointerException if url is null
79 */
80 public URLReader(final URL url, final Charset cs) {
81 Objects.requireNonNull(url);
82 this.url = url;
83 this.cs = cs;
84 }
85
86 @Override
87 public int read(final char cbuf[], final int off, final int len) throws IOException {
88 return getReader().read(cbuf, off, len);
89 }
90
91 @Override
92 public void close() throws IOException {
93 getReader().close();
94 }
95
96 /**
97 * URL of this reader
98 * @return the URL from which this reader reads.
99 */
100 public URL getURL() {
101 return url;
|