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:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command./*from  ww  w.  ja va 2 s.  co m*/
 * <p>
 * Equivalent to qsub [param]
 *
 * @param input job input file
 * @return job id
 */
public static String qsub(String input) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(input);

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

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, null, 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 resource overrides
 * <p>//from   w w w .j  av a2 s .co  m
 * Equivalent to qsub -t 1,2,3 -l [resource_name=value,resource_name=value] [param]
 *
 * @param input job input file
 * @param pbsArrayIDs of specified PBS indices
 * @param resourceOverrides list of resource overrides
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs, String... resourceOverrides) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    cmdLine.addArgument(listArgument);
    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:ee.ria.xroad.opmonitordaemon.QueryRequestHandlerTest.java

/**
 * Ensure that an health data response data is filtered and body is
 * constructed correctly.//from   w  w w. j a v  a  2  s  .  c  o  m
 */
@Test
public void handleHealthDataRequest() throws Exception {
    InputStream is = new FileInputStream(HEALTH_DATA_REQUEST);
    SoapParser parser = new SoapParserImpl();
    SoapMessageImpl request = (SoapMessageImpl) parser.parse(MimeTypes.TEXT_XML_UTF8, is);

    QueryRequestHandler handler = new HealthDataRequestHandler(new TestMetricsRegistry());

    OutputStream out = new ByteArrayOutputStream();

    handler.handle(request, out, ct -> testContentType = ct);

    String baseContentType = MimeUtils.getBaseContentType(testContentType);
    assertEquals(MimeTypes.TEXT_XML, baseContentType);

    SoapMessageImpl response = (SoapMessageImpl) parser.parse(MimeTypes.TEXT_XML,
            IOUtils.toInputStream(out.toString()));

    GetSecurityServerHealthDataResponseType responseData = JaxbUtils
            .createUnmarshaller(GetSecurityServerHealthDataResponseType.class)
            .unmarshal(SoapUtils.getFirstChild(response.getSoap().getSOAPBody()),
                    GetSecurityServerHealthDataResponseType.class)
            .getValue();

    assertEquals(TEST_TIMESTAMP, responseData.getMonitoringStartupTimestamp());
    assertEquals(2, responseData.getServicesEvents().getServiceEvents().size());
    assertEquals(ServiceId.create("XTEE-CI-XM", "GOV", "00000001", "System1", "xroad/GetRandom", "v2"),
            responseData.getServicesEvents().getServiceEvents().get(0).getService());
    assertEquals(5, responseData.getServicesEvents().getServiceEvents().get(0).getLastPeriodStatistics()
            .getSuccessfulRequestCount());
    assertEquals(5, responseData.getServicesEvents().getServiceEvents().get(0).getLastPeriodStatistics()
            .getUnsuccessfulRequestCount());
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyNullInputStream() throws IOException {
    OutputStream baos = new ByteArrayOutputStream(11);
    assertEquals(0, StreamUtil.copy(null, baos));
    String resultStr = baos.toString();
    baos.close();// w  w  w.  j  a  v a2 s  . c  o m
    assertEquals("", baos.toString());
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyInputStream() throws IOException {
    InputStream is = new StringInputStream("test string");
    OutputStream baos = new ByteArrayOutputStream(11);
    StreamUtil.copy(is, baos);//from w w w  . jav a2 s  .  c om
    is.close();
    String resultStr = baos.toString();
    baos.close();
    assertTrue(resultStr.equals("test string"));
}

From source file:net.floodlightcontroller.configuration.ConfigurationManager.java

@Override
public String showConfiguration(String fileName) {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode rootNode = createJsonRootNode();
    JsonFactory f = new JsonFactory();
    OutputStream out = new ByteArrayOutputStream();
    JsonGenerator g = null;/*from   www .j a  v a2 s.c o m*/
    try {
        g = f.createGenerator(out, JsonEncoding.UTF8);
        g.useDefaultPrettyPrinter();
        mapper.writeTree(g, rootNode);
    } catch (IOException e) {
        return "Error: Could not parse the JSON configuration file.";
    }
    return out.toString();
}

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

/**
 * PBS qsub command.//from   www.  ja  va2s  .co m
 * <p>
 * Equivalent to qsub [param]
 *
 * @param inputs job input file
 * @param environment environment variables
 * @return job id
 */
public static String qsub(String[] inputs, Map<String, String> environment) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    for (int i = 0; i < inputs.length; ++i) {
        cmdLine.addArgument(inputs[i]);
    }

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

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, environment, 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 with command line '" + cmdLine.toString()
                + "'. Error output: " + err.toString());
    }

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

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

/**
 * PBS qdel command./*from w w w .  j  a v  a 2s  .  c  o m*/
 * <p>
 * Equivalent to qdel [param]
 *
 * @param jobId job id
 */
public static void qdel(String jobId) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QDEL);
    cmdLine.addArgument(jobId);

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

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

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qdel exit value: " + exitValue);

    if (exitValue != 0)
        throw new PBSException("Failed to delete job " + jobId + ". Error output: " + err.toString());
}

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

/**
 * PBS tracejob command./* www  . ja v  a 2  s.co m*/
 * <p>
 * Equivalent to tracejob -n [numberOfDays] [jobId]
 *
 * @param jobId job id
 * @param numberOfDays number of days to look for the job
 * @param quiet quiet mode flag
 * @return tracejob output
 */
public static CommandOutput traceJob(String jobId, int numberOfDays, boolean quiet) {
    final CommandLine cmdLine = new CommandLine(COMMAND_TRACEJOB);
    cmdLine.addArgument(PARAMETER_NUMBER_OF_DAYS);
    cmdLine.addArgument(Integer.toString(numberOfDays));
    if (quiet) {
        cmdLine.addArgument(PARAMETER_QUIET_MODE);
    }
    cmdLine.addArgument(jobId);

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

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

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

    return new CommandOutput(out.toString(), err.toString());
}

From source file:net.sourceforge.vulcan.spring.SpringFileStoreTest.java

public void testExport() throws Exception {
    OutputStream os = new ByteArrayOutputStream();

    store.exportConfiguration(os);/*from  www.j  a va 2s .c o  m*/
    os.close();

    assertTrue(os.toString().length() > 0);
}