Java ByteArrayOutputStream Read Line readLine(InputStream in)

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

Description

Read a full line from an input stream, returning it in a string.

License

Open Source License

Parameter

Parameter Description
in The stream to read from.

Return

the line, or a zero length string if like was empty (other than end of line chars).

Declaration

public static String readLine(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
/*//w  w  w . j a v  a 2  s .  co  m
 * Oxford Brookes University Secure Email Proxy 
 * Copyright (C) 2002/3 Oxford Brookes University Secure Email Project
 * http://secemail.brookes.ac.uk
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * The Secure Email Project is:
 * 
 * Marcus Povey <mpovey@brookes.ac.uk> or <icewing@dushka.co.uk>
 * Damian Branigan <dbranigan@brookes.ac.uk>
 * George Davson <gdavson@brookes.ac.uk>
 * David Duce <daduce@brookes.ac.uk>
 * Simon Hogg <simon.hogg@brookes.ac.uk>
 * Faye Mitchell <frmitchell@brookes.ac.uk>
 * 
 * For further information visit the secure email project website.
 */

import java.io.*;

public class Main {
    /** 
     * <p>Read a full line from an input stream, returning it in a string. </p>
     * <p>Written because there are issues attached to using buffered readers in this context. </p>
     * @param in The stream to read from.
     * @return the line, or a zero length string if like was empty (other than end of line chars).
     */
    public static String readLine(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int b = in.read();
        while ((b != '\r') && (b != '\n') && (b != -1)) {
            out.write(b);
            b = in.read();
        }

        if (b == '\r')
            in.read(); // if there is a \r then next line will be a \n.. so skip it

        return out.toString();
    }
}

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, char character)
  7. readLine(InputStream input)
  8. readLine(InputStream inputStream)
  9. readLine(InputStream inputStream)