Here you can find the source of readBytes(URL url)
public static byte[] readBytes(URL url) throws IOException
//package com.java2s; /*--------------------------------------------------------------------------* | Copyright (C) 2006 Christopher Kohlhaas | | | | This program is free software; you can redistribute it and/or modify | | it under the terms of the GNU General Public License as published by the | | Free Software Foundation. A copy of the license has been included with | | these distribution in the COPYING file, if not go to www.fsf.org | | | | As a special exception, you are granted the permissions to link this | | program with every library, which license fulfills the Open Source | | Definition as published by the Open Source Initiative (OSI). | *--------------------------------------------------------------------------*/ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class Main { /** reads the content form an url into a ByteArray*/ public static byte[] readBytes(URL url) throws IOException { InputStream in = null;// ww w . j a v a2 s. c o m ByteArrayOutputStream out = new ByteArrayOutputStream(); try { in = url.openStream(); byte[] buffer = new byte[1024]; int count = 0; do { out.write(buffer, 0, count); count = in.read(buffer, 0, buffer.length); } while (count != -1); return out.toByteArray(); } finally { if (in != null) { in.close(); } // end of if () } } }