URLReader.java revision 1423:c13179703f65
1114117Simp/* 250476Speter * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 31592Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4156813Sru * 5156813Sru * This code is free software; you can redistribute it and/or modify it 6241823Smarcel * under the terms of the GNU General Public License version 2 only, as 7241823Smarcel * published by the Free Software Foundation. Oracle designates this 853909Speter * particular file as subject to the "Classpath" exception as provided 9183242Ssam * by Oracle in the LICENSE file that accompanied this code. 1053909Speter * 1153909Speter * This code is distributed in the hope that it will be useful, but WITHOUT 1253909Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13272322Sdelphij * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14124587Sru * version 2 for more details (a copy is included in the LICENSE file that 15137675Sbz * accompanied this code). 16183242Ssam * 1753909Speter * You should have received a copy of the GNU General Public License version 1853909Speter * 2 along with this work; if not, write to the Free Software Foundation, 19143026Strhodes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2053909Speter * 21104385Smike * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2253909Speter * or visit www.oracle.com if you need additional information or have any 2353909Speter * questions. 2453909Speter */ 25143026Strhodes 26124587Srupackage jdk.nashorn.api.scripting; 2770922Sdougb 28124587Sruimport java.io.CharArrayReader; 29101808Sdwmaloneimport java.io.IOException; 30183242Ssamimport java.io.Reader; 31260024Sjmmvimport java.net.URL; 3253909Speterimport java.nio.charset.Charset; 33171173Smlaierimport java.util.Objects; 34200062Sedimport jdk.nashorn.internal.runtime.Source; 35137675Sbz 361592Srgrimes/** 37183242Ssam * A Reader that reads from a URL. Used to make sure that the reader 38183242Ssam * reads content from given URL and can be trusted to do so. 39183242Ssam * 40183242Ssam * @since 1.8u40 41183242Ssam */ 42183242Ssam@jdk.Exported 43183242Ssampublic final class URLReader extends Reader { 44183242Ssam // underlying URL 45272322Sdelphij private final URL url; 46272322Sdelphij // Charset used to convert 47272322Sdelphij private final Charset cs; 48272322Sdelphij 49156813Sru // lazily initialized underlying reader for URL 50137675Sbz private Reader reader; 51137675Sbz 52137675Sbz /** 53137675Sbz * Constructor 54183242Ssam * 55183242Ssam * @param url URL for this URLReader 56183242Ssam * @throws NullPointerException if url is null 57183242Ssam */ 58156813Sru public URLReader(final URL url) { 59171173Smlaier this(url, (Charset)null); 60126756Smlaier } 61126756Smlaier 62173220Syar /** 63124587Sru * Constructor 6453909Speter * 6553909Speter * @param url URL for this URLReader 66156813Sru * @param charsetName Name of the Charset used to convert bytes to chars 67143107Sru * @throws NullPointerException if url is null 68143026Strhodes */ 69143026Strhodes public URLReader(final URL url, final String charsetName) { 70143026Strhodes this(url, Charset.forName(charsetName)); 71156813Sru } 72124587Sru 73183242Ssam /** 7438101Speter * Constructor 7538101Speter * 76278710Sngie * @param url URL for this URLReader 77278710Sngie * @param cs Charset used to convert bytes to chars 78278710Sngie * @throws NullPointerException if url is null 79278710Sngie */ 80183242Ssam public URLReader(final URL url, final Charset cs) { 81183242Ssam this.url = Objects.requireNonNull(url); 82183242Ssam this.cs = cs; 83183242Ssam } 84260013Sjmmv 85260013Sjmmv @Override 86260024Sjmmv public int read(final char cbuf[], final int off, final int len) throws IOException { 87260013Sjmmv return getReader().read(cbuf, off, len); 88260013Sjmmv } 891592Srgrimes 90 @Override 91 public void close() throws IOException { 92 getReader().close(); 93 } 94 95 /** 96 * URL of this reader 97 * @return the URL from which this reader reads. 98 */ 99 public URL getURL() { 100 return url; 101 } 102 103 /** 104 * Charset used by this reader 105 * 106 * @return the Charset used to convert bytes to chars 107 */ 108 public Charset getCharset() { 109 return cs; 110 } 111 112 // lazily initialize char array reader using URL content 113 private Reader getReader() throws IOException { 114 synchronized (lock) { 115 if (reader == null) { 116 reader = new CharArrayReader(Source.readFully(url, cs)); 117 } 118 } 119 120 return reader; 121 } 122} 123