Java URL Load load(URL url)

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

Description

load

License

Open Source License

Declaration

public static byte[] load(URL url) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w w . j a v a 2  s  .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 byte[] load(URL url) throws IOException {
        return read(url.openStream());
    }

    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. load(Iterable urls)
  2. load(String fileOrURL)
  3. load(URL pUrl)
  4. load(URL url)
  5. load(URL url)
  6. load(URL url, boolean allowCache)
  7. loadByteArray(URL url)
  8. loadFile(String url)
  9. loadFile(URL resource)