outputtostring.java

来自「MoMEUnit是一个单元测试的J2ME的应用程序xUnit架构实例。这是来自J」· Java 代码 · 共 93 行

JAVA
93
字号
package org.momeunit.ant.core;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;/** * Class that cumulates input read from {@link InputStream}. Provides methods * {@link #getOutputString()} for accessing cumulated input as string. Sends * input to pipe if requested. *  * @author Sergio Morozov * @version 1.1.2 */public class OutputToString extends OutputPipe{  private StringBuffer sb = null;  private boolean endOfOutput = false;  /**   * Instantiates OutputToString object with given InputStream.   *    * @param pipeIn   *          the InputStream to read from.   * @since 1.1   */  public OutputToString(InputStream pipeIn)  {    super(pipeIn);  }  /**   * Reads input from {@link InputStream} and cumulates it until input is   * closed. Sends input to pipe if requested.   *    * @see OutputPipe#run()   */  public void run()  {    BufferedReader reader = new BufferedReader(new InputStreamReader(this        .getInputStream()));    this.sb = new StringBuffer();    OutputStream out = this.getOutpuStream();    try    {      for (String line; (line = reader.readLine()) != null;)      {        sb.append(line).append('\n');        if (out != null) out.write((line + '\n').getBytes());      }    } catch (IOException e)    {} finally    {      setEndOfOutput();      try      {        if (out != null && out != System.out && out != System.err) out.close();        if (reader != null) reader.close();      } catch (IOException e)      {}    }  }  private synchronized void setEndOfOutput()  {    this.endOfOutput = true;    notifyAll();  }  /**   * Returns gathered input as {@link String}.   *    * @return the cumulated input as {@link String}.   * @since 1.1   */  public synchronized String getOutputString()  {    while (!this.endOfOutput)    {      try      {        wait();      } catch (InterruptedException e)      {}    }    return this.sb.toString();  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?