Java OutputStream Write Text readLine(java.io.InputStream input)

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

Description

read Line

License

Open Source License

Declaration

public static String readLine(java.io.InputStream input)
            throws IOException 

Method Source Code

//package com.java2s;
/*/*from w w  w. jav  a  2s.c  o  m*/
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2016 Serge Rieder (serge@jkiss.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (version 2)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.*;

public class Main {
    public static final int DEFAULT_BUFFER_SIZE = 16384;

    public static String readLine(java.io.InputStream input)
            throws IOException {
        StringBuilder linebuf = new StringBuilder();
        for (int b = input.read(); b != '\n'; b = input.read()) {
            if (b == -1) {
                if (linebuf.length() == 0) {
                    return null;
                } else {
                    break;
                }
            }
            if (b != '\r') {
                linebuf.append((char) b);
            }
        }
        return linebuf.toString();
    }

    public static String toString(File file) throws IOException {
        try (Reader reader = new FileReader(file)) {
            StringWriter writer = new StringWriter();
            copyText(reader, writer, DEFAULT_BUFFER_SIZE);
            return writer.toString();
        }
    }

    /**
       Read entire reader content and writes it to writer
       then closes reader and flushed output.
     */
    public static void copyText(java.io.Reader reader,
            java.io.Writer writer, int bufferSize) throws IOException {
        char[] writeBuffer = new char[bufferSize];
        for (int br = reader.read(writeBuffer); br != -1; br = reader
                .read(writeBuffer)) {
            writer.write(writeBuffer, 0, br);
        }
        writer.flush();
    }

    public static void copyText(java.io.Reader reader, java.io.Writer writer)
            throws IOException {
        copyText(reader, writer, DEFAULT_BUFFER_SIZE);
    }
}

Related

  1. readLine(InputStream in, OutputStream out)
  2. readLine(InputStream in, OutputStream out)