Java BufferedReader Read Line readline(BufferedReader in, BufferedReader err)

Here you can find the source of readline(BufferedReader in, BufferedReader err)

Description

Read a line of text from the BufferedReader.

License

Open Source License

Parameter

Parameter Description
in STDOUT of the process being monitored (input to Java)
err STDERR of the process being monitored (input to Java)

Exception

Parameter Description
IOException if anything goes wrong in the reading

Return

String containing the next line from the process

Declaration

public static String readline(BufferedReader in, BufferedReader err) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
/*/*w  ww . j a  v  a 2  s. c o  m*/
 * File:  SysUtil.java   
 *
 * Copyright (C) 2002, Peter F. Peterson
 *
 * 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Peter F. Peterson <pfpeterson@anl.gov>
 *           Intense Pulsed Neutron Source Division
 *           Argonne National Laboratory
 *           9700 South Cass Avenue, Bldg 360
 *           Argonne, IL 60439-4845, USA
 *
 * This work was supported by the Intense Pulsed Neutron Source Division
 * of Argonne National Laboratory, Argonne, IL 60439-4845, USA.
 *
 * For further information, see <http://www.pns.anl.gov/ISAW/>
 *
 * Modified:
 *
 *  $Log$
 *  Revision 1.7  2003/05/28 20:51:40  pfpeterson
 *  Changed System.getProperty to SharedData.getProperty
 *
 *  Revision 1.6  2002/11/27 23:23:49  pfpeterson
 *  standardized header
 *
 *  Revision 1.5  2002/11/21 21:53:43  pfpeterson
 *  Modified readline and jumpline to also take a handle to the process's
 *  stderr and throw exceptions when stderr contains anything.
 *
 *  Revision 1.4  2002/11/01 15:46:13  pfpeterson
 *  Fixed bug where could not find an executable on windows. Also
 *  added more intelligent debug prints when mode is on.
 *
 *  Revision 1.3  2002/10/25 22:16:10  pfpeterson
 *  Added print statements for finding executables if
 *  SharedData.DEBUG is true.
 *
 *  Revision 1.2  2002/10/08 16:32:14  pfpeterson
 *  Improved the getExecInPath method to actually check the path
 *  (if available). Since how java gets the path is kinda goofy,
 *  it still supports the old method of checking a few fixed places.
 *
 *  Revision 1.1  2002/09/17 22:29:40  pfpeterson
 *  Added to CVS.
 *
 *
 *   
 */

import java.io.*;

public class Main {
    private static final int BUFF_SIZE = 100;

    /**
     * Read a line of text from the BufferedReader. In some cases this
     * can hang indefinitely (should be fixed).
     *
     * @param in STDOUT of the process being monitored (input to Java)
     * @param err STDERR of the process being monitored (input to Java)
     *
     * @return String containing the next line from the process
     *
     * @throws IOException if anything goes wrong in the reading
     */
    public static String readline(BufferedReader in, BufferedReader err) throws IOException, InterruptedException {
        // check the error stream
        if (err != null && err.ready()) {
            String msg = err.readLine();
            if (msg.toLowerCase().indexOf("interrupt") >= 0) {
                throw new InterruptedException("");
            } else {
                System.out.println("ERR:" + msg);
                throw new IOException(msg);
            }
        }

        char buff[] = new char[BUFF_SIZE];
        String result = null;
        int go_back = 0;
        int size = 0;

        while (!in.ready()) {
            // wait until it is ready
        }

        for (size = 0; size < BUFF_SIZE; size++) {
            if (!in.ready()) {
                if (size == 0)
                    return null;
            } else {
                buff[size] = (char) in.read();
                String charac = (new Character(buff[size])).toString();
                if (charac.equals("\n")) {
                    go_back++;
                    break;
                } else if (buff[size] == -1) {
                    go_back++;
                    break;
                }
            }
        }
        //in.read(buff,0,BUFF_SIZE); // default java way that doesn't work well

        if (size < go_back)
            return "";
        result = new String(buff, 0, size);
        /*if(go_back>0){
          result=result.substring(0,result.length()-go_back-1);
          }*/
        return result;
    }
}

Related

  1. readLine()
  2. readLine(BufferedReader br)
  3. readLine(BufferedReader br, int maxlen)
  4. readLine(BufferedReader in)
  5. readLine(BufferedReader in)
  6. readLine(BufferedReader input)
  7. readLine(BufferedReader reader)
  8. readLine(BufferedReader reader)
  9. readLine(BufferedReader reader)