Example usage for java.io Reader ready

List of usage examples for java.io Reader ready

Introduction

In this page you can find the example usage for java.io Reader ready.

Prototype

public boolean ready() throws IOException 

Source Link

Document

Tells whether this stream is ready to be read.

Usage

From source file:ExecHelper.java

/**
 * Take a process, record its standard error and standard out streams, wait for it to finish
 *
 * @param process process to watch//from w  w  w.  ja  v a 2  s . c  o  m
 * @throws SecurityException if a security manager exists and its checkExec method doesn't allow creation of a subprocess.
 * @throws IOException - if an I/O error occurs
 * @throws NullPointerException - if cmdarray is null
 * @throws IndexOutOfBoundsException - if cmdarray is an empty array (has length 0).
 *
 * @since ostermillerutils 1.06.00
 */
private ExecHelper(Process process, String charset) throws IOException {
    StringBuffer output = new StringBuffer();
    StringBuffer error = new StringBuffer();

    Reader stdout;
    Reader stderr;

    if (charset == null) {
        // This is one time that the system charset is appropriate,
        // don't specify a character set.
        stdout = new InputStreamReader(process.getInputStream());
        stderr = new InputStreamReader(process.getErrorStream());
    } else {
        stdout = new InputStreamReader(process.getInputStream(), charset);
        stderr = new InputStreamReader(process.getErrorStream(), charset);
    }
    char[] buffer = new char[1024];

    boolean done = false;
    boolean stdoutclosed = false;
    boolean stderrclosed = false;
    while (!done) {
        boolean readSomething = false;
        // read from the process's standard output
        if (!stdoutclosed && stdout.ready()) {
            readSomething = true;
            int read = stdout.read(buffer, 0, buffer.length);
            if (read < 0) {
                readSomething = true;
                stdoutclosed = true;
            } else if (read > 0) {
                readSomething = true;
                output.append(buffer, 0, read);
            }
        }
        // read from the process's standard error
        if (!stderrclosed && stderr.ready()) {
            int read = stderr.read(buffer, 0, buffer.length);
            if (read < 0) {
                readSomething = true;
                stderrclosed = true;
            } else if (read > 0) {
                readSomething = true;
                error.append(buffer, 0, read);
            }
        }
        // Check the exit status only we haven't read anything,
        // if something has been read, the process is obviously not dead yet.
        if (!readSomething) {
            try {
                this.status = process.exitValue();
                done = true;
            } catch (IllegalThreadStateException itx) {
                // Exit status not ready yet.
                // Give the process a little breathing room.
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ix) {
                    process.destroy();
                    throw new IOException("Interrupted - processes killed");
                }
            }
        }
    }

    this.output = output.toString();
    this.error = error.toString();
}

From source file:com.p5solutions.core.json.JsonDeserializer.java

/**
 * Throw input stream reader exception.//from w  w w  . j  av a  2 s  .  com
 */
protected void throwInputStreamReaderException(Reader reader) {
    if (reader == null) {
        throw new NullPointerException(getClass() + " cannot deserialize a null input stream. "
                + "You must instantiate with a vaild JSON input stream.");
    }

    try {
        if (!reader.ready()) {
            logger.error("Input stream reader is not ready to be read. Check instance of " + getClass());
        }
    } catch (Exception e) {
        logger.error(
                e + " : " + "Input stream reader is not ready to be read. Check instance of " + getClass());
    }

}

From source file:nl.toolforge.karma.core.module.template.BaseModuleLayoutTemplate.java

public final void createLayout(File baseDir) throws IOException {

    FileTemplate[] fileTemplates = getFileElements();
    String[] templateFiles = new String[fileTemplates.length];

    for (int i = 0; i < fileTemplates.length; i++) {

        FileTemplate fileTemplate = fileTemplates[i];
        logger.debug(/*from  w  w  w . ja  v a 2 s  .  com*/
                "Write template '" + fileTemplate.getSource() + "' to '" + fileTemplate.getTarget() + "'.");
        Reader input = new BufferedReader(new InputStreamReader(SourceModule.class
                .getResourceAsStream(fileTemplate.getSource().toString().replace('\\', '/'))));

        File outputFile = new File(baseDir + File.separator + fileTemplate.getTarget());
        outputFile.getParentFile().mkdirs();
        outputFile.createNewFile();

        FileOutputStream output = new FileOutputStream(outputFile);

        while (input.ready()) {
            output.write(input.read());
        }
        templateFiles[i] = fileTemplate.getTarget().getPath();
        logger.debug("Wrote template.");
    }

    // Create directories.
    //
    String[] dirs = getDirectoryElements();

    for (int i = 0; i < dirs.length; i++) {

        File dirToAdd = new File(baseDir, dirs[i]);

        // Try to create the dirs
        if (!dirToAdd.mkdirs() && !dirToAdd.exists()) {
            // Failed to create the dirs and they do not exist yet.
            //
            throw new KarmaRuntimeException("Error while creating module layout.");
        }
    }
}

From source file:org.efaps.wikiutil.export.latex.MakePDF.java

/**
 * Executes &quot;<code>pdflatex</code>&quot; from the Latex packages and
 * converts all Latex files to related PDF file <code>book.pdf</code>.
 *
 * @return <i>true</i> if the Latex to PDF convert was successfully;
 *         otherwise <i>false</i>
 * @throws IOException if execute failed
 * @see #tempDir/*ww  w  .  j a v a2 s  . c  o  m*/
 */
protected boolean executePDFLatex() throws IOException {
    final ProcessBuilder processBuilder = new ProcessBuilder(this.executablePdfLaTeX, "book.tex");
    processBuilder.directory(this.tempDir);
    final Process process = processBuilder.start();

    final Reader in = new InputStreamReader(process.getInputStream());
    final Reader err = new InputStreamReader(process.getErrorStream());
    //    PrintStream out = new PrintStream(process.getOutputStream());

    Integer exitCode = null;
    for (;;) {
        if (err.ready()) {
            System.err.print((char) err.read());
        } else if (in.ready()) {
            System.out.print((char) in.read());
        } else {
            try {
                exitCode = process.exitValue();
                break;
            } catch (final IllegalThreadStateException e) {
                try {
                    Thread.sleep(1000);
                } catch (final InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }
    return (exitCode != null) && (exitCode == 0);
}