Java URL Connection getLines(final URL url, final int linesToRead)

Here you can find the source of getLines(final URL url, final int linesToRead)

Description

Reads the first n lines from the resource pointed to by the passed URL

License

Apache License

Parameter

Parameter Description
url The URL to read the lines from
linesToRead The number of lines to read

Return

an array of the read lines which will be at most linesToRead lines long

Declaration

public static String[] getLines(final URL url, final int linesToRead) 

Method Source Code


//package com.java2s;
/*/*w w  w  .  j a  v  a 2s.  c om*/
 * Copyright 2015 the original author or authors.
 *
 * 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.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Reads the first <b><code>n</code></b> lines from the resource pointed to by the passed URL
     * @param url The URL to read the lines from
     * @param linesToRead The number of lines to read
     * @return an array of the read lines which will be at most <b><code>linesToRead</code></b> lines long
     */
    public static String[] getLines(final URL url, final int linesToRead) {
        if (url == null)
            throw new IllegalArgumentException("The passed URL was null");
        if (linesToRead < 1)
            throw new IllegalArgumentException("Invalid number of lines [" + linesToRead + "]. Must be >= 1");
        InputStream is = null;
        BufferedReader br = null;
        InputStreamReader isr = null;
        URLConnection connection = null;
        try {
            connection = url.openConnection();
            connection.setConnectTimeout(1000);
            connection.setReadTimeout(1000);
            connection.connect();
            is = connection.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line = null;
            final List<String> strLines = new ArrayList<String>(linesToRead);
            int linesRead = 0;
            while ((line = br.readLine()) != null && linesRead < linesToRead) {
                strLines.add(line);
                linesRead++;
            }
            return strLines.toArray(new String[strLines.size()]);
        } catch (Exception ex) {
            throw new RuntimeException("Failed to read " + linesToRead + " from [" + url + "]");
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (Exception x) {
                    /* No Op */}
            if (isr != null)
                try {
                    isr.close();
                } catch (Exception x) {
                    /* No Op */}
            if (is != null)
                try {
                    is.close();
                } catch (Exception x) {
                    /* No Op */}
        }

    }
}

Related

  1. getJar(String jarUrl)
  2. getJarUrlForPackage(String packageName)
  3. getLastModified(URL url)
  4. getLastModified(URLConnection connection)
  5. getLength(String htmlUrl)
  6. getOutputStream(final URL outputURL)
  7. getOutputStream(URL url)
  8. getPage(String url)
  9. getPageContent(String inputUrlString)