Java URL Load readUrlStream(String urlString)

Here you can find the source of readUrlStream(String urlString)

Description

Read, as a string, the stream at the given url string.

License

Open Source License

Declaration

public static String readUrlStream(String urlString) throws MalformedURLException, IOException 

Method Source Code

//package com.java2s;
/*//www  .  jav a  2 s  .  c om
 * Copyright (c) 2012, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html
 * 
 * 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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;

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

public class Main {
    /**
     * Read, as a string, the stream at the given url string.
     */
    public static String readUrlStream(String urlString) throws MalformedURLException, IOException {
        URL url = new URL(urlString);
        InputStream stream = url.openStream();
        return toString(stream);
    }

    private static String toString(InputStream is) throws IOException {
        final char[] buffer = new char[0x10000];
        StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(is, "UTF-8");
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                out.append(buffer, 0, read);
            }
        } while (read >= 0);
        return out.toString();
    }
}

Related

  1. readURLasString(URL url)
  2. readUrlContent(String address)
  3. readURLContents(String UrlText)
  4. readURLJSONArray(String urlString)
  5. readUrlProperties(URL url)
  6. readUrlText(final URL url, final String encoding)
  7. readURLText(String urlPath, String encoding)
  8. readUrlText(String urlString)
  9. readURLText(URL url, StringBuffer errorText)