Tue, 13 Jun 2017 16:30:24 -0700
Added tag jdk8u151-b01 for changeset 681ab42e7c73
sundar@1511 | 1 | /* |
sundar@1511 | 2 | * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
sundar@1511 | 3 | * |
sundar@1511 | 4 | * Redistribution and use in source and binary forms, with or without |
sundar@1511 | 5 | * modification, are permitted provided that the following conditions |
sundar@1511 | 6 | * are met: |
sundar@1511 | 7 | * |
sundar@1511 | 8 | * - Redistributions of source code must retain the above copyright |
sundar@1511 | 9 | * notice, this list of conditions and the following disclaimer. |
sundar@1511 | 10 | * |
sundar@1511 | 11 | * - Redistributions in binary form must reproduce the above copyright |
sundar@1511 | 12 | * notice, this list of conditions and the following disclaimer in the |
sundar@1511 | 13 | * documentation and/or other materials provided with the distribution. |
sundar@1511 | 14 | * |
sundar@1511 | 15 | * - Neither the name of Oracle nor the names of its |
sundar@1511 | 16 | * contributors may be used to endorse or promote products derived |
sundar@1511 | 17 | * from this software without specific prior written permission. |
sundar@1511 | 18 | * |
sundar@1511 | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
sundar@1511 | 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
sundar@1511 | 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
sundar@1511 | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
sundar@1511 | 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
sundar@1511 | 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
sundar@1511 | 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
sundar@1511 | 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
sundar@1511 | 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
sundar@1511 | 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
sundar@1511 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
sundar@1511 | 30 | */ |
sundar@1511 | 31 | |
sundar@1511 | 32 | // bind on a Java constructor |
sundar@1511 | 33 | |
sundar@1511 | 34 | // See Function.prototype.bind: |
sundar@1511 | 35 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind |
sundar@1511 | 36 | var bind = Function.prototype.bind; |
sundar@1511 | 37 | |
sundar@1511 | 38 | var URL = Java.type("java.net.URL"); |
sundar@1511 | 39 | |
sundar@1511 | 40 | // get the constructor that accepts URL, String parameters. |
sundar@1511 | 41 | // constructor signatures are properties of type object. |
sundar@1511 | 42 | var newURL = URL["(URL, String)"]; |
sundar@1511 | 43 | |
sundar@1511 | 44 | // bind "context" URL parameter. |
sundar@1511 | 45 | var TwitterURL = bind.call(newURL, null, new URL('https://www.twitter.com')); |
sundar@1511 | 46 | |
sundar@1511 | 47 | // now you can create context relative URLs using the bound constructor |
sundar@1511 | 48 | print(new TwitterURL("sundararajan_a")); |
sundar@1511 | 49 | |
sundar@1511 | 50 | // read the URL content and print (optional part) |
sundar@1511 | 51 | |
sundar@1511 | 52 | var BufferedReader = Java.type("java.io.BufferedReader"); |
sundar@1511 | 53 | var InputStreamReader = Java.type("java.io.InputStreamReader"); |
sundar@1511 | 54 | |
sundar@1511 | 55 | // function to retrieve text content of the given URL |
sundar@1511 | 56 | function readTextFromURL(url) { |
sundar@1511 | 57 | var str = ''; |
sundar@1511 | 58 | var u = new URL(url); |
sundar@1511 | 59 | var reader = new BufferedReader( |
sundar@1511 | 60 | new InputStreamReader(u.openStream())); |
sundar@1511 | 61 | try { |
sundar@1511 | 62 | reader.lines().forEach(function(x) str += x); |
sundar@1511 | 63 | return str; |
sundar@1511 | 64 | } finally { |
sundar@1511 | 65 | reader.close(); |
sundar@1511 | 66 | } |
sundar@1511 | 67 | } |
sundar@1511 | 68 | |
sundar@1511 | 69 | print(readTextFromURL(new TwitterURL("sundararajan_a"))); |