Java InputStream Read Line readLines(final InputStream inputStream)

Here you can find the source of readLines(final InputStream inputStream)

Description

This is a convenience method thread reads an input stream into a List

License

Open Source License

Parameter

Parameter Description
inputStream a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static List<String> readLines(final InputStream inputStream) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 eBay Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w ww. j a  v a2  s  .c  o m*/
 *     eBay Inc. - initial API and implementation
 *******************************************************************************/

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

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

public class Main {
    /** This is a convenience method thread reads an input stream into a
     * List<String>
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static List<String> readLines(final InputStream inputStream) throws IOException {
        final String text = readStream(inputStream);
        StringTokenizer tokenizer = new StringTokenizer(text, "\n\r");

        List<String> list = new ArrayList<String>();

        while (tokenizer.hasMoreElements()) {
            final String line = tokenizer.nextToken();
            list.add(line);
        }
        return list;
    }

    /** This is a convienence method to read text from an input stream into a
     * string.  It will use the default encoding of the OS.
     * It call the underlying readString(InputStreamReader)
     *
     * @param inputStream - InputStream
     * @return String - the text that was read in
     * @throws IOException
     */
    public static String readStream(final InputStream inputStream) throws IOException {
        final InputStreamReader isr = new InputStreamReader(inputStream);
        try {
            return readStream(isr);
        } finally {
            isr.close();
        }
    }

    /** This is a convienence method to read text from a stream into a string.
     * The transfer buffer is 4k and the initial string buffer is 75k.  If
     * this causes a problem, write your own routine.
     *
     * @param isr - InputStreamReader
     * @return String - the text that was read in
     * @throws IOException
     */
    public static String readStream(final InputStreamReader isr) throws IOException {
        StringBuffer sb = new StringBuffer(75000);
        char[] buf = new char[4096];
        int numRead;
        do {
            numRead = isr.read(buf, 0, buf.length);
            if (numRead > 0) {
                sb.append(buf, 0, numRead);
            }
        } while (numRead >= 0);
        final String result = sb.toString();
        return result;
    }
}

Related

  1. readLines(ByteArrayInputStream outputSource)
  2. readLines(final InputStream in)
  3. readLines(final InputStream input, final String encoding)
  4. readLines(final InputStream inputStream)
  5. readLines(final InputStream is)
  6. readlines(final InputStream stream)
  7. readLines(InputStream in)
  8. readLines(InputStream in)