Java ByteArrayOutputStream Read Line readLine(InputStream input)

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

Description

read a line terminated by a new line '\n' character

License

Open Source License

Parameter

Parameter Description
input - the input string to read from

Exception

Parameter Description
IOException an exception

Return

the line read or null if end of input is reached

Declaration

public static String readLine(InputStream input) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*from ww w  .jav  a2  s .c om*/
     * read a line terminated by a new line '\n' character
     * @param input - the input string to read from 
     * @return the line read or <code>null</code> if end of input is reached
     * @throws IOException
     */
    public static String readLine(InputStream input) throws IOException {
        return readLine(input, '\n');
    }

    /**
     * read a line from the given input stream
     * @param input - the input stream
     * @param terminatorChar - the character used to terminate lines
     * @return the line read or <code>null</code> if end of input is reached
     * @throws IOException
     */
    public static String readLine(InputStream input, char terminatorChar) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while (true) {
            int c = input.read();
            if (c < 0 && bos.size() == 0)
                return null;
            if (c < 0 || c == terminatorChar)
                break;
            else
                bos.write(c);
        }
        return bos.size() > 0 ? bos.toString() : null;
    }
}

Related

  1. readLine(InputStream in)
  2. readLine(InputStream in)
  3. readLine(InputStream in)
  4. readLine(InputStream in)
  5. readLine(InputStream in, char character)
  6. readLine(InputStream inputStream)
  7. readLine(InputStream inputStream)
  8. readLine(InputStream inputstream)
  9. readLine(InputStream inputStream)