Java URL Load readBytes(URL url)

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

Description

reads the content form an url into a ByteArray

License

Open Source License

Declaration

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

Method Source Code

//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 ()
        }
    }
}

Related

  1. read(String url)
  2. read(URL file)
  3. read(URL url, String charsetName)
  4. readAsString(URL url)
  5. readAsStringUTF8(URL url)
  6. readBytesFromURL(String url)
  7. readBytesFromUrl(URL url)
  8. readContent(URL url)
  9. readContentFromFile(URL u, String encoding)