Java ByteArrayOutputStream Read Line readLine(InputStream in)

Here you can find the source of readLine(InputStream in)

Description

read one line.

License

MIT License

Declaration

public static String readLine(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright(c) 2013 M Hata
   This software is released under the MIT License.
   http://opensource.org/licenses/mit-license.php */

import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Reader;

public class Main {
    /**/*from   w  w  w  . j a  v a 2s.c om*/
     * read one line.
     */
    public static String readLine(InputStream in) throws IOException {
        ByteArrayOutputStream sb = new ByteArrayOutputStream();
        while (true) {
            int c = in.read();
            if (c < 0) {
                if (sb.size() == 0) {
                    return null; //end
                }
                break;
            } else if (c == '\r') {
            } else if (c == '\n') {
                break;
            } else {
                sb.write(c);
            }
        }
        return sb.toString();
    }

    /**
     * read one line.
     */
    public static String readLine(Reader in) throws IOException {
        StringBuilder sb = new StringBuilder();
        while (true) {
            int c = in.read();
            if (c < 0) {
                if (sb.toString().equals("")) {
                    return null; //end
                }
                break;
            } else if (c == '\r') {
            } else if (c == '\n') {
                break;
            } else {
                sb.append((char) c);
            }
        }
        return sb.toString();
    }

    public static void write(OutputStream out, String str) throws IOException {
        out.write(str.getBytes());
    }
}

Related

  1. readLine()
  2. readLine(final InputStream in)
  3. readLine(InputStream in)
  4. readLine(InputStream in)
  5. readLine(InputStream in)
  6. readLine(InputStream in)