Java URL to InputStream getUrlContents(String urlString)

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

Description

get Url Contents

License

Apache License

Declaration

public static String getUrlContents(String urlString) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************* 
 *  Copyright 2008 Amazon Technologies, Inc.
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  // www .  j  a  v a  2  s  .com
 *  You may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 *  This file 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.
 * ***************************************************************************** 
 *    __  _    _  ___ 
 *   (  )( \/\/ )/ __)
 *   /__\ \    / \__ \
 *  (_)(_) \/\/  (___/
 * 
 *  Amazon FPS Java Library
 *  API Version: 2008-09-17
 *  Generated: Tue Sep 29 03:25:00 PDT 2009 
 * 
 */

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

import java.net.URL;

public class Main {
    private static final int READ_THRESHOLD = 1024 * 1024;
    private static final String NewLine = "\n";

    public static String getUrlContents(String urlString) throws IOException {
        URL url = new URL(urlString);
        if (url == null)
            return null;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(url.openStream()));

            StringBuilder urlContents = new StringBuilder();
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                urlContents.append(inputLine);
                urlContents.append(NewLine);
                if (urlContents.length() >= READ_THRESHOLD) {
                    throw new IOException("Size of the contents at the given url [" + url
                            + "] exceeds threshold of [" + READ_THRESHOLD + "]");
                }
            }
            return urlContents.toString();
        } finally {
            if (in != null)
                in.close();
        }
    }
}

Related

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