Java URL Load readURL(String url, String charsetName)

Here you can find the source of readURL(String url, String charsetName)

Description

read URL

License

Open Source License

Declaration

public static String readURL(String url, String charsetName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static String readURL(String url, String charsetName) {
        try {/*from   w  w  w.  ja va2s.  co  m*/
            java.net.URL u = new java.net.URL(url);
            return readAsText(u.openStream(), charsetName);
        } catch (Exception exp) {
            throw new RuntimeException("readURL error!", exp);
        }
    }

    public static String readAsText(String fileName, String charsetName) {
        InputStream is = null;
        try {
            is = openFile(fileName);
            return readAsText(is, charsetName);
        } catch (Exception exp) {
            throw new RuntimeException("readTextFile error, file: " + fileName, exp);
        } finally {
            close(is);
        }
    }

    public static String readAsText(InputStream is, String charsetName) {
        try {
            StringBuffer sb = new StringBuffer();
            char[] buffer = new char[10000];
            InputStreamReader isr = null;
            if (charsetName != null) {
                isr = new InputStreamReader(is, charsetName);
            } else {
                isr = new InputStreamReader(is);
            }
            int length;
            while ((length = isr.read(buffer)) != -1) {
                sb.append(buffer, 0, length);
            }
            return sb.toString();
        } catch (Exception exp) {
            throw new RuntimeException("readTextInputStream error!", exp);
        }
    }

    public static InputStream openFile(File file) {
        try {
            InputStream is = new FileInputStream(file);
            return is;
        } catch (Exception exp) {
            throw new RuntimeException("openFile error, file: " + file, exp);
        }
    }

    public static InputStream openFile(String name) {
        try {
            return openFile(new File(name));
        } catch (Exception exp) {
            throw new RuntimeException("openFile error, file: " + name, exp);
        }
    }

    public static void close(Closeable resource) {
        try {
            if (resource != null) {
                resource.close();
            }
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

    public static byte[] read(File file) {
        return read(openFile(file));
    }

    public static byte[] read(InputStream is) {
        List<byte[]> bufferList = new ArrayList<byte[]>();
        List<Integer> lengthList = new ArrayList<Integer>();
        int total = 0;
        try {
            do {
                byte[] buffer = new byte[10000];
                int length;
                length = is.read(buffer);
                if (length == -1) {
                    break;
                }
                total += length;
                bufferList.add(buffer);
                lengthList.add(length);
            } while (true);
            byte[] result = new byte[total];
            int offset = 0;
            for (int i = 0; i < bufferList.size(); i++) {
                byte[] buf = (byte[]) bufferList.get(i);
                int len = (Integer) lengthList.get(i);
                System.arraycopy(buf, 0, result, offset, len);
                offset += len;
            }
            return result;
        } catch (Exception exp) {
            throw new RuntimeException("readInputStream error!", exp);
        }
    }

    public static byte[] read(File file, int offset, int length) {
        BufferedInputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(file));
            byte[] result = new byte[length];
            is.skip(offset);
            int len = is.read(result);
            if (len < length) {
                result = Arrays.copyOfRange(result, 0, len);
            }
            return result;
        } catch (Exception exp) {
            throw new RuntimeException("writeFile error, file: " + file, exp);
        } finally {
            close(is);
        }
    }
}

Related

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