Java URL Load readURL(String URL)

Here you can find the source of readURL(String URL)

Description

read URL

License

Open Source License

Declaration

public static String readURL(String URL) throws IOException 

Method Source Code

//package com.java2s;
/* /*w  ww  . j a  v a 2s  .c o  m*/
    
 Created on Mar 4, 2005
    
 The Bungee View applet lets you search, browse, and data-mine an image collection.  
 Copyright (C) 2006  Mark Derthick
    
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.  See gpl.html.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
 You may also contact the author at 
 mad@cs.cmu.edu, 
 or at
 Mark Derthick
 Carnegie-Mellon University
 Human-Computer Interaction Institute
 Pittsburgh, PA 15213
    
 */

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;
import java.io.StringWriter;

import java.net.URL;

public class Main {
    public static String readURL(String URL) throws IOException {
        URL url = new URL(URL);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        return ReaderToString(in);
    }

    static String ReaderToString(BufferedReader in) {
        String result = null;
        if (in != null) {
            StringBuffer buf = new StringBuffer();
            String line;
            try {
                while ((line = in.readLine()) != null) {
                    if (buf.length() > 0)
                        buf.append("\n");
                    buf.append(line);
                }
                in.close();
                result = buf.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static Object[] append(Object[] a1, Object[] a2, Class<?> type) {
        if (a1 == null || a1.length == 0)
            return a2;
        else if (a2 == null || a2.length == 0)
            return a1;
        else {
            Object[] a = (Object[]) java.lang.reflect.Array.newInstance(type, a1.length + a2.length);
            System.arraycopy(a1, 0, a, 0, a1.length);
            System.arraycopy(a2, 0, a, a1.length, a2.length);
            return a;
        }
    }

    public static String[] append(String[] a1, String[] a2) {
        if (a1 == null)
            return a2;
        else if (a2 == null)
            return a1;
        else {
            String[] a = new String[a1.length + a2.length];
            System.arraycopy(a1, 0, a, 0, a1.length);
            System.arraycopy(a2, 0, a, a1.length, a2.length);
            return a;
        }
    }

    public static int[] append(int[] a1, int[] a2) {
        if (a1 == null)
            return a2;
        else if (a2 == null)
            return a1;
        else {
            int[] a = new int[a1.length + a2.length];
            System.arraycopy(a1, 0, a, 0, a1.length);
            System.arraycopy(a2, 0, a, a1.length, a2.length);
            return a;
        }
    }

    public static double[] append(double[] a1, double[] a2) {
        if (a1 == null)
            return a2;
        else if (a2 == null)
            return a1;
        else {
            double[] a = new double[a1.length + a2.length];
            System.arraycopy(a1, 0, a, 0, a1.length);
            System.arraycopy(a2, 0, a, a1.length, a2.length);
            return a;
        }
    }

    public static String printStackTrace() {
        (new RuntimeException("Relax.  There's no error, we're just printing the stack trace")).printStackTrace();
        return null;
    }

    public static String printStackTrace(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }
}

Related

  1. readTextURL(URL url)
  2. readURL(final URL fileURL)
  3. readURL(final URL url)
  4. readUrl(final URL url)
  5. readUrl(final URL url)
  6. readURL(String url)
  7. readURL(String url)
  8. readURL(String url, String charsetName)
  9. ReadURL(String URLAddress)