Java URL Read getBytes(URL url)

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

Description

get Bytes

License

Open Source License

Declaration

private static byte[] getBytes(URL url) throws IOException 

Method Source Code


//package com.java2s;
/*/* w  ww  .ja  va 2 s.  c o  m*/
 * @(#)MethodUtil.java
 *
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.io.BufferedInputStream;
import java.io.IOException;

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

public class Main {
    private static byte[] getBytes(URL url) throws IOException {
        URLConnection urlconnection = url.openConnection();
        if (urlconnection instanceof HttpURLConnection) {
            if (((HttpURLConnection) urlconnection).getResponseCode() >= 400)
                throw new IOException("open HTTP connection failed.");
        }
        int i = urlconnection.getContentLength();
        BufferedInputStream bufferedinputstream = new BufferedInputStream(urlconnection.getInputStream());
        byte abyte0[];
        try {
            if (i != -1) {
                abyte0 = new byte[i];
                int l;
                for (; i > 0; i -= l) {
                    l = bufferedinputstream.read(abyte0, abyte0.length - i, i);
                    if (l == -1)
                        throw new IOException("unexpected EOF");
                }
            } else {
                abyte0 = new byte[8192];
                int i1 = 0;
                do {
                    int j;
                    if ((j = bufferedinputstream.read(abyte0, i1, abyte0.length - i1)) == -1)
                        break;
                    i1 += j;
                    if (i1 >= abyte0.length) {
                        byte abyte1[] = new byte[i1 * 2];
                        System.arraycopy(abyte0, 0, abyte1, 0, i1);
                        abyte0 = abyte1;
                    }
                } while (true);
                if (i1 != abyte0.length) {
                    byte abyte2[] = new byte[i1];
                    System.arraycopy(abyte0, 0, abyte2, 0, i1);
                    abyte0 = abyte2;
                }
            }
        } finally {
            bufferedinputstream.close();
        }
        return abyte0;
    }
}

Related

  1. getBodyResponse(HttpURLConnection con)
  2. getBytes(URL url)
  3. getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination)
  4. getFileSize(String sURL)
  5. getFileSize(URL url)