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;
/*/*from w  ww .  j a va2 s  .  c  om*/
 * Copyright (c) 2012, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html
 * 
 * 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.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<String> list = new ArrayList<String>();
            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 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 is2, String encoding)
  6. readLines(InputStream stream)
  7. readLines(InputStream stream)
  8. readLines(InputStream stream)
  9. readLines(InputStream stream, String charset)