Here you can find the source of readLines(final InputStream inputStream)
Parameter | Description |
---|---|
inputStream | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static List<String> readLines(final InputStream inputStream) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 eBay Software Foundation. * * 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./*from w w w . ja v a2s . c o m*/ *******************************************************************************/ 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; } public static String readStream(final InputStream inputStream, boolean autoClose) throws IOException { try { return readStream(inputStream); } finally { if (autoClose) { try { inputStream.close(); } catch (Throwable e) { } ; } } } /** 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 { StringBuilder sb = new StringBuilder(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; } }