All Downloads are FREE. Search and download functionalities are using the official Maven repository.

smile.data.parser.IOUtils Maven / Gradle / Ivy

There is a newer version: 0.6.0-incubating
Show newest version
/*******************************************************************************
 * Copyright (c) 2010 Haifeng Li
 *   
 * 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.
 *******************************************************************************/
package smile.data.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Simple IO stream manipulation utilities.
 * 
 * @author Haifeng Li
 */
public class IOUtils {
    /**
     * Returns the lines of an InputStream as a list of Strings.
     *
     * @param input  the InputStream to read from.
     * @return the list of lines.
     */
    public static List readLines(InputStream input) throws IOException {
        final InputStreamReader reader = new InputStreamReader(input);
        return readLines(reader);
    }
    
    /**
     * Returns the lines of an InputStream as a list of Strings.
     *
     * @param input  the InputStream to read from.
     * @param charset a charset.
     * @return the list of lines.
     */
    public static List readLines(InputStream input, Charset charset) throws IOException {
        final InputStreamReader reader = new InputStreamReader(input, charset);
        return readLines(reader);
    }
    
    /**
     * Returns the lines of a Reader as a list of Strings.
     *
     * @param input  the Reader to read from.
     * @return the list of lines.
     */
    public static List readLines(Reader input) throws IOException {
        BufferedReader reader = input instanceof BufferedReader ? (BufferedReader) input : new BufferedReader(input);
        List list = new ArrayList();
        String line = reader.readLine();
        while (line != null) {
            list.add(line);
            line = reader.readLine();
        }
        reader.close();
        return list;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy