Java URL Load readBytesFromUrl(URL url)

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

Description

read Bytes From Url

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/**/*from w  w  w .j  ava 2 s . co m*/
 * Copyright (C) 2000-2010 Atomikos <info@atomikos.com>
 *
 * This code ("Atomikos TransactionsEssentials"), by itself,
 * is being distributed under the
 * Apache License, Version 2.0 ("License"), a copy of which may be found at
 * http://www.atomikos.com/licenses/apache-license-2.0.txt .
 * You may not use this file except in compliance with the License.
 *
 * While the License grants certain patent license rights,
 * those patent license rights only extend to the use of
 * Atomikos TransactionsEssentials by itself.
 *
 * This code (Atomikos TransactionsEssentials) contains certain interfaces
 * in package (namespace) com.atomikos.icatch
 * (including com.atomikos.icatch.Participant) which, if implemented, may
 * infringe one or more patents held by Atomikos.
 * It should be appreciated that you may NOT implement such interfaces;
 * licensing to implement these interfaces must be obtained separately from Atomikos.
 *
 * 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.
 */

import java.io.ByteArrayOutputStream;

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

import java.net.URL;

public class Main {
    public static byte[] readBytesFromUrl(URL url) throws IOException {
        InputStream in = url.openStream();
        byte[] ret = readAllBytesFromInputStream(in);
        in.close();
        return ret;
    }

    public static byte[] readAllBytesFromInputStream(InputStream in) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        int b = in.read();
        while (b >= 0) {
            bout.write(b);
            b = in.read();
        }
        return bout.toByteArray();
    }
}

Related

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