Java URL Read getTextFromURL(URL url)

Here you can find the source of getTextFromURL(URL url)

Description

Reads the content of a URL as text using the default connect and read timeouts.

License

Apache License

Parameter

Parameter Description
url The url to get the text from

Return

a string representing the text read from the passed URL

Declaration

public static String getTextFromURL(URL url) 

Method Source Code


//package com.java2s;
/*/*from   w w  w  . ja  v  a2s  .  co m*/
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /** Text line separator */
    public static final String EOL = System.getProperty("line.separator", "\n");
    /** The system property to retrieve the default client connect timeout in ms.  */
    public static final String DEFAULT_CONNECT_TO = "sun.net.client.defaultConnectTimeout";
    /** The system property to retrieve the default client read timeout in ms.  */
    public static final String DEFAULT_READ_TO = "sun.net.client.defaultReadTimeout";

    /**
     * Reads the content of a URL as text using the default connect and read timeouts.
     * @param url The url to get the text from
     * @return a string representing the text read from the passed URL
     */
    public static String getTextFromURL(URL url) {
        return getTextFromURL(url, defaultConnectTimeout(), defaultReadTimeout());
    }

    /**
     * Reads the content of a URL as text using the default connect and read timeouts.
     * @param urlStr The url stringy to get the text from
     * @return a string representing the text read from the passed URL
     */
    public static String getTextFromURL(final CharSequence urlStr) {
        return getTextFromURL(toURL(urlStr), defaultConnectTimeout(), defaultReadTimeout());
    }

    /**
     * Reads the content of a URL as text
     * @param url The url to get the text from
     * @param timeout The connect and read timeout in ms.
     * @return a string representing the text read from the passed URL
     */
    public static String getTextFromURL(URL url, int timeout) {
        return getTextFromURL(url, timeout, timeout);
    }

    /**
     * Reads the content of a URL as text
     * @param url The url to get the text from
     * @param cTimeout The connect timeout in ms.
     * @param rTimeout The read timeout in ms.
     * @return a string representing the text read from the passed URL
     */
    public static String getTextFromURL(URL url, int cTimeout, int rTimeout) {
        StringBuilder b = new StringBuilder();
        InputStreamReader isr = null;
        BufferedReader br = null;
        InputStream is = null;
        URLConnection connection = null;
        try {
            connection = url.openConnection();
            connection.setConnectTimeout(cTimeout);
            connection.setReadTimeout(rTimeout);
            connection.connect();
            is = connection.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                b.append(line).append(EOL);
            }
            return b.toString();
        } catch (Exception e) {
            throw new RuntimeException("Failed to read source of [" + url + "]", e);
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (Exception e) {
                    /* No Op */}
            if (isr != null)
                try {
                    isr.close();
                } catch (Exception e) {
                    /* No Op */}
            if (is != null)
                try {
                    is.close();
                } catch (Exception e) {
                    /* No Op */}
        }
    }

    /**
     * Returns the default URL connect timeout in ms,
     * @return the connect timeout in ms,
     */
    protected static int defaultConnectTimeout() {
        return Integer.parseInt(System.getProperty(DEFAULT_CONNECT_TO, "0"));
    }

    /**
     * Returns the default URL read timeout in ms,
     * @return the read timeout in ms,
     */
    protected static int defaultReadTimeout() {
        return Integer.parseInt(System.getProperty(DEFAULT_READ_TO, "0"));
    }

    /**
     * Returns the URL for the passed file
     * @param file the file to get the URL for
     * @return a URL for the passed file
     */
    public static URL toURL(File file) {
        try {
            return nvl(file, "Passed file was null").toURI().toURL();
        } catch (Exception e) {
            throw new RuntimeException("Failed to get URL for file [" + file + "]", e);
        }
    }

    /**
     * Creates a URL from the passed string 
     * @param urlStr A char sequence containing a URL representation
     * @return a URL
     */
    public static URL toURL(final CharSequence urlStr) {
        if (urlStr == null || urlStr.toString().trim().isEmpty())
            throw new IllegalArgumentException("The passed URL stringy was null or empty");
        try {
            if (isFile(urlStr))
                return toURL(new File(urlStr.toString()).getAbsoluteFile());
            return new URL(nvl(urlStr, "Passed string was null").toString());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create URL from string [" + urlStr + "]", e);
        }
    }

    /**
     * Creates a URI from the passed string 
     * @param uriStr A char sequence containing a URI representation
     * @return a URI
     */
    public static URI toURI(CharSequence uriStr) {
        try {
            return new URI(nvl(uriStr, "Passed string was null").toString());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create URL from string [" + uriStr + "]", e);
        }
    }

    /**
     * Tests the passed object for nullness. Throws an {@link IllegalArgumentException} if the object is null 
     * @param t  The object to test
     * @param message The message to associate with the exception, Ignored if null
     * @return the object passed in if not null
     */
    public static <T> T nvl(T t, String message) {
        if (t == null)
            throw new IllegalArgumentException(message != null ? message : "Null parameter");
        return t;
    }

    /**
     * Determines if the passed stringy represents an existing file name
     * @param urlStr The stringy to test
     * @return true if the passed stringy represents an existing file name, false otherwise
     */
    public static boolean isFile(final CharSequence urlStr) {
        if (urlStr == null || urlStr.toString().trim().isEmpty())
            throw new IllegalArgumentException("The passed URL stringy was null or empty");
        return new File(urlStr.toString().trim()).exists();
    }
}

Related

  1. getText(HttpURLConnection conn)
  2. getText(String url)
  3. getText(String url)
  4. getText(URL url)
  5. getTextFromURL(final String url)
  6. getTextFromURL(URL url)
  7. getURL(final URL url)
  8. getURL(String url)
  9. getURL(URL url, String params)