Example usage for java.io OutputStream toString

List of usage examples for java.io OutputStream toString

Introduction

In this page you can find the example usage for java.io OutputStream toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:uk.org.lidalia.sysoutslf4j.integration.TestSysOutOverSlf4J.java

@Test
public void stopSendingSystemOutAndErrToSLF4JSendsOutputToOldSystemOut() {
    OutputStream sysOutMock = setUpMockSystemOutput(SystemOutput.OUT);
    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
    SysOutOverSLF4J.stopSendingSystemOutAndErrToSLF4J();

    System.out.println("Hello");

    assertThat(sysOutMock.toString(), containsString("Hello" + LINE_SEPARATOR));
}

From source file:uk.org.lidalia.sysoutslf4j.integration.TestSysOutOverSlf4J.java

@Test
public void registeredLoggingSystemCanStillGetToConsole() {
    OutputStream sysOutMock = setUpMockSystemOutput(SystemOutput.OUT);
    SysOutOverSLF4J.registerLoggingSystem(PACKAGE_NAME);
    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();

    System.out.println("Should reach console");

    assertThat(sysOutMock.toString(), containsString("Should reach console" + LINE_SEPARATOR));
}

From source file:uk.org.lidalia.sysoutslf4j.integration.TestSysOutOverSlf4J.java

@Test
public void log4JConsoleAppenderStillLogsToConsole() throws Exception {
    OutputStream sysOutMock = setUpMockSystemOutput(SystemOutput.OUT);
    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();

    org.apache.log4j.Logger log = configureLog4jLoggerToUseConsoleAppender();
    log.info("Should reach the old sysout");

    assertThat(sysOutMock.toString(), containsString("INFO - Should reach the old sysout"));
}

From source file:info.rmapproject.api.responsemgr.EventResponseManager.java

/**
 * Retrieves RMap Event in requested RDF format and forms an HTTP response.
 *
 * @param strEventUri the Event URI//  w  w w .  j  ava  2 s  . c o m
 * @param returnType the RDF return type
 * @return HTTP Response
 * @throws RMapApiException the RMap API exception
 */
public Response getRMapEvent(String strEventUri, RdfMediaType returnType) throws RMapApiException {
    boolean reqSuccessful = false;
    Response response = null;
    try {
        if (strEventUri == null || strEventUri.length() == 0) {
            throw new RMapApiException(ErrorCode.ER_NO_OBJECT_URI_PROVIDED);
        }
        if (returnType == null) {
            returnType = Constants.DEFAULT_RDF_TYPE;
        }

        URI uriEventUri = null;
        try {
            strEventUri = URLDecoder.decode(strEventUri, "UTF-8");
            uriEventUri = new URI(strEventUri);
        } catch (Exception ex) {
            throw RMapApiException.wrap(ex, ErrorCode.ER_PARAM_WONT_CONVERT_TO_URI);
        }

        RMapEvent rmapEvent = rmapService.readEvent(uriEventUri);
        if (rmapEvent == null) {
            throw new RMapApiException(ErrorCode.ER_CORE_READ_EVENT_RETURNED_NULL);
        }

        OutputStream eventOutput = rdfHandler.event2Rdf(rmapEvent, returnType.getRdfType());
        if (eventOutput == null) {
            throw new RMapApiException(ErrorCode.ER_CORE_RDFHANDLER_OUTPUT_ISNULL);
        }

        response = Response.status(Response.Status.OK).entity(eventOutput.toString())
                .location(new URI(Utils.makeEventUrl(strEventUri)))
                .type(HttpTypeMediator.getResponseRMapMediaType("event", returnType.getRdfType())) //TODO move version number to constants
                .build();

        reqSuccessful = true;

    } catch (RMapApiException ex) {
        throw RMapApiException.wrap(ex);
    } catch (RMapEventNotFoundException ex) {
        throw RMapApiException.wrap(ex, ErrorCode.ER_EVENT_OBJECT_NOT_FOUND);
    } catch (RMapDefectiveArgumentException ex) {
        throw RMapApiException.wrap(ex, ErrorCode.ER_GET_STMT_BAD_ARGUMENT);
    } catch (RMapException ex) {
        throw RMapApiException.wrap(ex, ErrorCode.ER_CORE_GENERIC_RMAP_EXCEPTION);
    } catch (Exception ex) {
        throw RMapApiException.wrap(ex, ErrorCode.ER_UNKNOWN_SYSTEM_ERROR);
    } finally {
        if (rmapService != null)
            rmapService.closeConnection();
        if (!reqSuccessful && response != null)
            response.close();
    }
    return response;
}

From source file:org.corpus_tools.salt.util.tests.VisJsVisualizerTest.java

@Test
public void testWriterOutput()
        throws SaltParameterException, SaltResourceException, SaltException, IOException, XMLStreamException {
    SDocument doc = SaltFactory.createSDocument();
    SampleGenerator.createMorphologyAnnotations(doc);

    // SDocument doc = visInput.getDocument();
    VisJsVisualizer VisJsVisualizer = new VisJsVisualizer(doc);

    OutputStream osNodes = new ByteArrayOutputStream();
    OutputStream osEdges = new ByteArrayOutputStream();

    VisJsVisualizer.setNodeWriter(osNodes);
    VisJsVisualizer.setEdgeWriter(osEdges);
    VisJsVisualizer.buildJSON();//from w  w  w.  java2 s .co m

    String strNodes = osNodes.toString();
    String strEdges = osEdges.toString();

    System.out.println(strNodes);
    System.out.println(strEdges);

    JSONArray jsonNodes = new JSONArray(strNodes);
    JSONArray jsonEdges = new JSONArray(strEdges);

    System.out.println(jsonNodes.toString());
    System.out.println(jsonEdges.toString());

}

From source file:com.tupilabs.pbs.PBS.java

/**
 * <p>//from  w w  w .ja v  a 2 s. co  m
 * PBS qsub command with arguments resource overrides
 * </p>
 *
 * <p>
 * Equivalent to qsub [param] -l [resource_name=value,resource_name=value]]
 * </p>
 *
 * @param input job input file
 * @param resourceOverrides variable number of resources to override
 * @return job id
 */
public static String qsub(String input, String... resourceOverrides) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_RESOURCE_OVERRIDE_STATUS);
    String resourceOverrideArgument = StringUtils.join(resourceOverrides, ",");
    cmdLine.addArgument(resourceOverrideArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>/*from w w  w  . j  av  a  2  s  . c om*/
 * Equivalent to qsub -t 1,2,3 [param]
 *
 * @param input job input file
 * @param pbsArrayIDs list of specified PBS indices
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    cmdLine.addArgument(listArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>/*from w  w  w.  j av a 2  s.com*/
 * Equivalent to qsub -t 5-20 [param]
 *
 * @param input job input file
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    cmdLine.addArgument(rangeArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit AND a range to submit
 * <p>/*from  www  .  j  av  a2 s.c  o m*/
 * Equivalent to qsub -t 1,2,3,5-20 [param]
 *
 * @param input job input file
 * @param pbsArrayIDs list of specified indices
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    String combinedArgument = listArgument + "," + rangeArgument;
    cmdLine.addArgument(combinedArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}