Java URL Load loadFile(URL resource)

Here you can find the source of loadFile(URL resource)

Description

Load the content from the URL into a byte array.

License

Open Source License

Parameter

Parameter Description
resource a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static byte[] loadFile(URL resource) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Main {
    /**/*from w  ww .  j  ava  2 s.c  om*/
     * Load the content from the URL into a byte array.
     * 
     * @param resource
     * @return
     * @throws IOException
     */
    public static byte[] loadFile(URL resource) throws IOException {
        InputStream is = resource.openStream();
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int n;
            while ((n = is.read(buffer)) > 0) {
                bos.write(buffer, 0, n);
            }
            return bos.toByteArray();
        } finally {
            is.close();
        }
    }
}

Related

  1. load(URL url)
  2. load(URL url)
  3. load(URL url, boolean allowCache)
  4. loadByteArray(URL url)
  5. loadFile(String url)
  6. loadFile(URL url)
  7. loadFileNoArray(URL f)
  8. loadFileOrUrlToList(String iFileOrUrl)
  9. loadFromUrl(String url)