Java ByteArrayOutputStream Read Line readLine(InputStream in)

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

Description

Reads from the input stream until a linefeed is encountered.

License

Open Source License

Declaration

public static String readLine(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*from  w  w  w .  j a va2  s  .  c  o m*/
     * Reads from the input stream until a linefeed is encountered. All data up until that point is returned as a
     * string. If the byte preceding the linefeed is a carriage return, that is also removed from the returned value.
     * The stream is positioned immediately after the linefeed.
     */
    public static String readLine(InputStream in) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int c = in.read();
        if (c == -1 || c == '\n')
            return "";
        int c2 = in.read();

        while (c2 != -1 && (char) c2 != '\n') {
            baos.write(c);
            c = c2;
            c2 = in.read();
        }

        if ((char) c != '\r')
            baos.write(c);

        return new String(baos.toByteArray(), "UTF-8");
    }
}

Related

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