Java URL Load readURLThrowException(final String url)

Here you can find the source of readURLThrowException(final String url)

Description

read URL Throw Exception

License

Apache License

Declaration

public static byte[] readURLThrowException(final String url) throws MalformedURLException, IOException 

Method Source Code

//package com.java2s;
/**//from  w  ww . ja  v a2s.co m
 * Sahi - Web Automation and Test Tool
 *
 * Copyright  2006  V Narayan Raman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;

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

import java.io.UnsupportedEncodingException;

import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;

public class Main {
    public static int BUFFER_SIZE = 8192;

    public static byte[] readURLThrowException(final String url) throws MalformedURLException, IOException {
        byte[] data = null;
        InputStream inputStream = null;
        try {
            inputStream = new URL(url).openStream();
            data = getBytes(inputStream, -1);
            inputStream.close();
        } finally {
            inputStream.close();
        }
        return data;
    }

    public static byte[] getBytes(InputStream in) throws IOException {
        return getBytes(in, -1);
    }

    public static byte[] getBytes(final InputStream in, final int contentLength) throws IOException {
        BufferedInputStream bin = new BufferedInputStream(in, BUFFER_SIZE);

        if (contentLength != -1) {
            int totalBytesRead = 0;
            byte[] buffer = new byte[contentLength];
            while (totalBytesRead < contentLength) {
                int bytesRead = -1;
                try {
                    bytesRead = bin.read(buffer, totalBytesRead, contentLength - totalBytesRead);
                } catch (EOFException e) {
                }
                if (bytesRead == -1) {
                    break;
                }
                totalBytesRead += bytesRead;
            }
            return buffer;
        } else {
            ByteArrayOutputStream byteArOut = new ByteArrayOutputStream();
            BufferedOutputStream bout = new BufferedOutputStream(byteArOut);
            try {
                int totalBytesRead = 0;
                byte[] buffer = new byte[BUFFER_SIZE];

                while (true) {
                    int bytesRead = -1;
                    try {
                        bytesRead = bin.read(buffer);
                    } catch (EOFException e) {
                    }
                    if (bytesRead == -1) {
                        break;
                    }
                    bout.write(buffer, 0, bytesRead);
                    totalBytesRead += bytesRead;
                }
            } catch (SocketTimeoutException ste) {
                ste.printStackTrace();
            }
            bout.flush();
            bout.close();
            return byteArOut.toByteArray();
        }
    }

    public static byte[] getBytes(String dataStr) {
        try {
            return dataStr.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            return dataStr.getBytes();
        }
    }
}

Related

  1. readUrlStream(String urlString)
  2. readUrlText(final URL url, final String encoding)
  3. readURLText(String urlPath, String encoding)
  4. readUrlText(String urlString)
  5. readURLText(URL url, StringBuffer errorText)
  6. readURLToByteArray(URL url)
  7. readURLToString(URL u, String encoding)
  8. saveFile(final URL url, final File file)
  9. saveFileFromURL(URL url, File destinationFile)