Java URL to InputStream getUrlContents(String url)

Here you can find the source of getUrlContents(String url)

Description

get Url Contents

License

Open Source License

Declaration

public static byte[] getUrlContents(String url) throws IOException 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static byte[] getUrlContents(String url) throws IOException {
        URL u = new URL(url);
        URLConnection uc = u.openConnection();
        return toByteArray(uc.getInputStream());
    }//from  w  w w .  ja v a2 s . co m

    public static byte[] toByteArray(InputStream in) throws IOException {
        int bufferSize = 1024;
        return (toByteArray(in, bufferSize));
    }

    public static byte[] toByteArray(InputStream in, int bufferSize)
            throws IOException {
        /*
         * int read = 0; byte[] data = new byte[(int)size];
         * 
         * int b = in.read(); while (b != -1) { data[read] = (byte)b; b =
         * in.read(); read++; }
         * 
         * in.close(); return (data);
         */

        ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);

        // read up to 1k at a time
        byte[] buffer = new byte[bufferSize];

        boolean done = false;
        while (!done) {
            int bytesRead = in.read(buffer);
            if (bytesRead == -1) {
                done = true; // EOF
            } else {
                baos.write(buffer, 0, bytesRead);
            }
        }
        return baos.toByteArray();
    }

    public static byte[] toByteArray(String filePath) throws IOException {
        File file = new File(filePath);
        return toByteArray(file);
    }

    public static byte[] toByteArray(File file) throws IOException {

        FileInputStream in = new FileInputStream(file);
        byte[] data = toByteArray(in);
        in.close();
        return data;

        /*
         * ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); // read
         * up to 1k at a time byte[] buffer = new byte[1024];
         * 
         * boolean done = false; while (!done) { int bytesRead =
         * in.read(buffer); if (bytesRead == -1) { done = true; // EOF } else {
         * baos.write(buffer, 0, bytesRead); } } in.close(); return
         * baos.toByteArray();
         */
    }

    public static byte[] toByteArray(URL url) throws IOException {
        InputStream in = url.openStream();
        byte[] data = toByteArray(in);
        in.close();
        return data;
    }
}

Related

  1. getInputStreamForURL(URL url)
  2. getInputStreamForURL(URL url)
  3. getInputStreamForURL(URL url)
  4. getInputStreamFromURL(String theFilePath)
  5. getInputStreamReader(URL url)
  6. getUrlContents(String urlString)
  7. getUrlContents(String urlString)
  8. getUrlContents(URL url)
  9. openFileOrURL(String fileNameOrUrl)