Java URL Load read(URL url, String charsetName)

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

Description

read

License

Open Source License

Declaration

public static Reader read(URL url, String charsetName) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w  w. ja  v  a  2s  . co m
 * Copyright (C) 2010 eXo Platform SAS.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URL;

public class Main {
    public static Reader read(URL url, String charsetName) throws IOException {
        if (url == null) {
            throw new NullPointerException();
        }
        return new InputStreamReader(url.openStream(), charsetName);
    }

    public static String read(Reader reader) throws IOException {
        if (reader == null) {
            throw new NullPointerException("No null reader accepted");
        }
        try {
            StringWriter writer = new StringWriter();
            char[] buffer = new char[256];
            for (int amount = reader.read(buffer); amount != -1; amount = reader.read(buffer)) {
                writer.write(buffer, 0, amount);
            }
            return writer.toString();
        } finally {
            safeClose(reader);
        }
    }

    public static byte[] read(InputStream in) throws IOException {
        if (in == null) {
            throw new NullPointerException("No null input stream accepted");
        }
        try {
            byte[] bytes = new byte[128];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            for (int s = in.read(bytes); s != -1; s = in.read(bytes)) {
                baos.write(bytes, 0, s);
            }
            bytes = baos.toByteArray();
            return bytes;
        } finally {
            safeClose(in);
        }
    }

    public static String toString(Throwable t) {
        StringWriter writer = new StringWriter();
        t.printStackTrace(new PrintWriter(writer));
        return writer.toString();
    }

    public static void safeClose(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ignore) {
            }
        }
    }
}

Related

  1. loadUrlIntoByteArray(String urlString)
  2. loadUrlToList(URL iUrl)
  3. loadURLToString(String url, String EOL)
  4. read(String url)
  5. read(URL file)
  6. readAsString(URL url)
  7. readAsStringUTF8(URL url)
  8. readBytes(URL url)
  9. readBytesFromURL(String url)