All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jgcomptech.tools.WebTools Maven / Gradle / Ivy

package com.jgcomptech.tools;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/** Web tools for completing tasks dealing with websites. */
public final class WebTools {
    /** HTML tools for handling html source code. */
    public static final class HTML {
        /**
         * Gets HTML source code from specified URL.
         * @param url URL to download from
         * @return HTML source code as string
         * @throws IOException if error occurs
         * @throws MalformedURLException if url is invalid
         */
        public static String getHTML(final String url) throws IOException {
            final URL newurl = new URL(url);
            final StringBuilder sb;

            try(BufferedInputStream buf = new BufferedInputStream(newurl.openStream())) {

                sb = new StringBuilder();

                int data;
                while((data = buf.read()) != -1) {
                    sb.append((char) data);
                }
            }

            return sb.toString();
        }

        /** Prevents instantiation of this utility class. */
        private HTML() { }
    }

    /** Prevents instantiation of this utility class. */
    private WebTools() { }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy