Java InputStream Read Line readLines(InputStream is2, String encoding)

Here you can find the source of readLines(InputStream is2, String encoding)

Description

Breaks the contents of the given input stream into an array of strings.

License

Open Source License

Declaration

static String[] readLines(InputStream is2, String encoding) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation 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:/*from ww  w .  j  a  v a2 s  .c  o  m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

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

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

public class Main {
    /**
     * Breaks the contents of the given input stream into an array of strings.
     * The function assumes that the input stream uses the platform's default encoding
     * (<code>ResourcesPlugin.getEncoding()</code>).
     * Returns null if an error occurred.
     */
    static String[] readLines(InputStream is2, String encoding) {

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(is2, encoding));
            StringBuffer sb = new StringBuffer();
            List list = new ArrayList();
            while (true) {
                int c = reader.read();
                if (c == -1)
                    break;
                sb.append((char) c);
                if (c == '\r') { // single CR or a CR followed by LF
                    c = reader.read();
                    if (c == -1)
                        break;
                    sb.append((char) c);
                    if (c == '\n') {
                        list.add(sb.toString());
                        sb = new StringBuffer();
                    }
                } else if (c == '\n') { // a single LF
                    list.add(sb.toString());
                    sb = new StringBuffer();
                }
            }
            if (sb.length() > 0)
                list.add(sb.toString());
            return (String[]) list.toArray(new String[list.size()]);

        } catch (IOException ex) {
            return null;

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    // silently ignored
                }
            }
        }
    }
}

Related

  1. readLines(InputStream is)
  2. readLines(InputStream is)
  3. readLines(InputStream is)
  4. readLines(InputStream is)
  5. readLines(InputStream is)
  6. readLines(InputStream is2, String encoding)
  7. readLines(InputStream stream)
  8. readLines(InputStream stream)
  9. readLines(InputStream stream)