Java URL Load load(URL url, boolean allowCache)

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

Description

Loads and returns data from the specified URL.

License

Open Source License

Declaration


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

Method Source Code

//package com.java2s;
/**//from ww w .ja  v  a 2s . c o  m
 * The utillib library.
 * More information is available at http://www.jinchess.com/.
 * Copyright (C) 2002 Alexander Maryanovsky.
 * All rights reserved.
 *
 * The utillib library 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 of the
 * License, or (at your option) any later version.
 *
 * The utillib library 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 utillib library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.EOFException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.net.URL;
import java.util.Hashtable;

public class Main {
    /**
     * Maps URLs to byte arrays of the data loaded from them.
     */

    private final static Hashtable urlCache = new Hashtable();

    /**
     * Loads and returns data from the specified URL.
     */

    public static byte[] load(URL url, boolean allowCache) throws IOException {
        InputStream in = inputStreamForURL(url, allowCache);
        try {
            return readToEnd(in);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }

    /**
     * Returns an <code>InpuStream</code> for reading the data at the specified
     * URL. If <code>allowCache</code> is <code>true</code>, and the URL is cached,
     * a <code>ByteArrayInpuStream</code> with the cached data is returned.
     */

    public static InputStream inputStreamForURL(URL url, boolean allowCache) throws IOException {
        byte[] cached = null;
        if (allowCache)
            cached = (byte[]) urlCache.get(url);
        return cached == null ? url.openStream() : new ByteArrayInputStream(cached);
    }

    /**
     * Reads from the given InputStream until its end and returns a byte array
     * of the contents. The input stream is not <code>close</code>d by this
     * method.
     */

    public static byte[] readToEnd(InputStream in) throws IOException {
        byte[] buf = new byte[2048];

        int amountRead = 0;
        int count = 0;
        while ((count = in.read(buf, amountRead, buf.length - amountRead)) > 0) {
            amountRead += count;

            if (amountRead == buf.length) {
                byte[] oldBuf = buf;
                buf = new byte[oldBuf.length * 2];
                System.arraycopy(oldBuf, 0, buf, 0, amountRead);
            }
        }

        byte[] arr = new byte[amountRead];
        System.arraycopy(buf, 0, arr, 0, amountRead);
        return arr;
    }

    /**
     * Reads the specified amount of bytes from the specified input stream and
     * returns the resulting array. Throws an <code>EOFException</code> if the
     * stream ends before the specified amount of bytes is read.
     */

    public static byte[] read(InputStream in, int amount) throws IOException {
        ByteArrayOutputStream buf = new ByteArrayOutputStream(amount);
        if (pump(in, buf, amount) != amount)
            throw new EOFException();

        return buf.toByteArray();
    }

    /**
     * Writes the bytes read from the given input stream into the given output
     * stream until the end of the input stream is reached. Returns the amount of
     * bytes actually read/written.
     */

    public static int pump(InputStream in, OutputStream out) throws IOException {
        return pump(in, out, new byte[2048]);
    }

    /**
     * Writes up to the given amount of bytes read from the given input stream 
     * into the given output stream until the end of the input stream is reached.
     * Returns the amount of bytes actually read/written.
     */

    public static int pump(InputStream in, OutputStream out, int amount) throws IOException {
        return pump(in, out, amount, new byte[2048]);
    }

    /**
     * Writes the bytes read from the given input stream into the given output
     * stream until the end of the input stream is reached. Returns the amount of
     * bytes actually read/written. Uses the given byte array as the buffer.
     */

    public static int pump(InputStream in, OutputStream out, byte[] buf) throws IOException {
        if (buf.length == 0)
            throw new IllegalArgumentException("Cannot use a 0 length buffer");

        int count;
        int amountRead = 0;
        while ((count = in.read(buf)) != -1) {
            out.write(buf, 0, count);
            amountRead += count;
        }

        return amountRead;
    }

    /**
     * Writes up to the given amount of bytes read from the given input stream 
     * into the given output stream until the end of the input stream is reached.
     * Returns the amount of bytes actually read/written. Uses the given byte array
     * as the buffer.
     */

    public static int pump(InputStream in, OutputStream out, int amount, byte[] buf) throws IOException {
        if (buf.length == 0)
            throw new IllegalArgumentException("Cannot use a 0 length buffer");

        int amountRead = 0;
        while (amount > 0) {
            int amountToRead = amount > buf.length ? buf.length : amount;
            int count = in.read(buf, 0, amountToRead);
            if (count == -1)
                break;

            out.write(buf, 0, count);
            amount -= count;
            amountRead += count;
        }

        return amountRead;
    }
}

Related

  1. load(Iterable urls)
  2. load(String fileOrURL)
  3. load(URL pUrl)
  4. load(URL url)
  5. load(URL url)
  6. loadByteArray(URL url)
  7. loadFile(String url)
  8. loadFile(URL resource)
  9. loadFile(URL url)