Here you can find the source of readURL(final URL url)
Parameter | Description |
---|---|
url | to read |
Parameter | Description |
---|---|
IOException | if an error occurs reading data |
public static byte[] readURL(final URL url) throws IOException
//package com.java2s; /* See LICENSE for licensing and NOTICE for copyright. */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class Main { /** Size of buffer in bytes to use when reading files. */ private static final int READ_BUFFER_SIZE = 128; /**// w ww .j a v a2 s . com * Reads the data at the supplied URL and returns it as a byte array. * * @param url to read * * @return bytes read from the URL * * @throws IOException if an error occurs reading data */ public static byte[] readURL(final URL url) throws IOException { return readInputStream(url.openStream()); } /** * Reads the data in the supplied stream and returns it as a byte array. * * @param is stream to read * * @return bytes read from the stream * * @throws IOException if an error occurs reading data */ public static byte[] readInputStream(final InputStream is) throws IOException { final ByteArrayOutputStream data = new ByteArrayOutputStream(); try { final byte[] buffer = new byte[READ_BUFFER_SIZE]; int length; while ((length = is.read(buffer)) != -1) { data.write(buffer, 0, length); } } finally { is.close(); data.close(); } return data.toByteArray(); } }