1 /*
   2  * Copyright (c) 2010, 2013, 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 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 public final class URLReader extends Reader {
  40     // underlying URL
  41     private final URL url;
  42     // Charset used to convert
  43     private final Charset cs;
  44 
  45     // lazily initialized underlying reader for URL
  46     private Reader reader;
  47 
  48     /**
  49      * Constructor
  50      *
  51      * @param url URL for this URLReader
  52      * @throws NullPointerException if url is null
  53      */
  54     public URLReader(final URL url) {
  55         this(url, (Charset)null);
  56     }
  57 
  58     /**
  59      * Constructor
  60      *
  61      * @param url URL for this URLReader
  62      * @param charsetName  Name of the Charset used to convert bytes to chars
  63      * @throws NullPointerException if url is null
  64      */
  65     public URLReader(final URL url, final String charsetName) {
  66         this(url, Charset.forName(charsetName));
  67     }
  68 
  69     /**
  70      * Constructor
  71      *
  72      * @param url URL for this URLReader
  73      * @param cs  Charset used to convert bytes to chars
  74      * @throws NullPointerException if url is null
  75      */
  76     public URLReader(final URL url, final Charset cs) {
  77         // null check
  78         url.getClass();
  79         this.url = url;
  80         this.cs  = cs;
  81     }
  82 
  83     @Override
  84     public int read(final char cbuf[], final int off, final int len) throws IOException {
  85         return getReader().read(cbuf, off, len);
  86     }
  87 
  88     @Override
  89     public void close() throws IOException {
  90         getReader().close();
  91     }
  92 
  93     /**
  94      * URL of this reader
  95      * @return the URL from which this reader reads.
  96      */
  97     public URL getURL() {
  98         return url;
  99     }
 100 
 101     /**
 102      * Charset used by this reader
 103      *
 104      * @return the Chartset used to convert bytes to chars
 105      */
 106     public Charset getCharset() {
 107         return cs;
 108     }
 109 
 110     // lazily initialize char array reader using URL content
 111     private Reader getReader() throws IOException {
 112         synchronized (lock) {
 113             if (reader == null) {
 114                 reader = new CharArrayReader(Source.readFully(url, cs));
 115             }
 116         }
 117 
 118         return reader;
 119     }
 120 }