Here you can find the source of loadText(URL url, boolean allowCache)
Parameter | Description |
---|---|
IOException | if the given URL does not exist or an I/O error occurswhile accessing it. |
public static String loadText(URL url, boolean allowCache) throws IOException
//package com.java2s; /**/* w w w. j a v a 2 s .c om*/ * 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(); /** * Reads all the information from the given InputStream and returns it as * plain text by using the default system encoding. Note that this method * doesn't close the given InputStream, that is left to the user. */ public static String loadText(InputStream in) throws IOException { return new String(readToEnd(in)); } /** * Loads the text from the given URL and returns it as a string. * * @throws IOException if the given URL does not exist or an I/O error occurs * while accessing it. */ public static String loadText(URL url, boolean allowCache) throws IOException { return new String(load(url, allowCache)); } /** * 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; } /** * 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) { } } } /** * 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(); } /** * 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); } /** * 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; } }