Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

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

Prototype

public synchronized String toString() 

Source Link

Document

Converts the buffer's contents into a string decoding bytes using the platform's default character set.

Usage

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ??//from w w  w  .  j a  va  2  s . c  o  m
 *
 * @param filePath
 *            ?
 * @return ??
 */
public static String readFileContent(String filePath) {
    if (null == filePath) {
        return null;
    }
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        FileInputStream fis = new FileInputStream(filePath);

        byte[] buffer = new byte[XConstant.BUFFER_LEN];
        int len = 0;
        while ((len = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }

        String content = bos.toString();
        bos.close();
        fis.close();
        return content;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.v2.KubernetesV2Utils.java

static public void apply(KubernetesAccount account, String manifest) {
    manifest = prettify(manifest);// w w w. j  a va  2s  . c o  m
    List<String> command = kubectlPrefix(account);
    command.add("apply");
    command.add("-f");
    command.add("-"); // read from stdin

    JobRequest request = new JobRequest().setTokenizedCommand(command);

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();

    String jobId = DaemonTaskHandler.getJobExecutor().startJob(request, System.getenv(),
            new ByteArrayInputStream(manifest.getBytes()), stdout, stderr);

    JobStatus status;
    try {
        status = DaemonTaskHandler.getJobExecutor().backoffWait(jobId);
    } catch (InterruptedException e) {
        throw new DaemonTaskInterrupted(e);
    }

    if (status.getState() != JobStatus.State.COMPLETED) {
        throw new HalException(Problem.Severity.FATAL, String.join("\n", "Unterminated deployment of manifest:",
                manifest, stderr.toString(), stdout.toString()));
    }

    if (status.getResult() != JobStatus.Result.SUCCESS) {
        throw new HalException(Problem.Severity.FATAL, String.join("\n", "Failed to deploy manifest:", manifest,
                stderr.toString(), stdout.toString()));
    }
}

From source file:com.npower.wurfl.Wurfl.java

public static String toPrettyXML(Document doc) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    toPrettyXML(doc, out);//from  w ww . j av  a2 s . c o m
    return out.toString();
}

From source file:com.bptselenium.jenkins.BPTSeleniumJenkins.RunCommand.java

public static void runCommand(String command, TaskListener listener, Run<?, ?> build) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CommandLine cmdLine = CommandLine.parse(command);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    Executor executor = new DefaultExecutor();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    executor.setExitValue(10);/* w  w w .  j  a  v  a 2s  . co m*/
    executor.setWatchdog(watchdog);
    try {
        executor.execute(cmdLine);
    } catch (ExecuteException ee) {
        //getting a non-standard execution value, set build result to unstable
        Result result = Result.UNSTABLE;
        if (build.getResult() == null) {
            build.setResult(result);
        } else if (build.getResult().isBetterThan(result)) {
            build.setResult(result.combine(build.getResult()));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        listener.getLogger().println(outputStream.toString());
    }
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static String getManifest(JarFile jar) throws IOException {

    JarEntry manifestEntry = jar.getJarEntry(MANIFEST_LOCATION);

    InputStream jis = null;//w  w w.  ja  va  2s .  c o m
    ByteArrayOutputStream baos = null;

    try {
        jis = jar.getInputStream(manifestEntry);
        baos = new ByteArrayOutputStream();

        CopyUtil.copyClose(jis, baos);
        baos.close();

        String manifest = baos.toString();

        return manifest;
    } finally {
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(baos);
    }
}

From source file:Main.java

/**
 * read file to a string/*from w  w  w  .  j  a  va  2s  .  co m*/
 * 
 * @param context
 * @param file
 * @return
 */
public static String loadString(File file) {
    if (null == file || !file.exists()) {
        return "";
    }
    FileInputStream fis = null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        fis = new FileInputStream(file);
        int restSize = fis.available();
        int bufSize = restSize > BUF_SIZE ? BUF_SIZE : restSize;
        byte[] buf = new byte[bufSize];
        while (fis.read(buf) != -1) {
            baos.write(buf);
            restSize -= bufSize;

            if (restSize <= 0)
                break;
            if (restSize < bufSize)
                bufSize = restSize;
            buf = new byte[bufSize];
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return baos.toString();
}

From source file:com.chiralBehaviors.autoconfigure.debug.TemplateDebuggerTest.java

@Test
public void testDebugger() throws Exception {

    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    InputStream yaml = getClass().getResourceAsStream("/yaml/templateDebugger.yml");
    TemplateDebugger debugger = mapper.readValue(yaml, TemplateDebugger.class);
    assertNotNull(debugger);// ww w  .  j  av  a2  s .  c  om
    String rendered = debugger.render();
    assertNotNull(rendered);
    InputStream gold = getClass().getResourceAsStream("/configurations/templateDebugger.rendered");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Utils.copy(gold, baos);
    String expected = baos.toString();
    assertEquals(expected, rendered);
}

From source file:com.google.mr4c.message.HttpMessageHandler.java

private String toString(HttpEntity entity) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    entity.writeTo(out);//from  w ww. j  a  v a  2  s. c o m
    return out.toString();
}

From source file:com.isa.utiles.Utiles.java

public static void downloadFile(String linkDescarga, String rutaDestino)
        throws MalformedURLException, IOException {

    URL urlFile = new URL(linkDescarga);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = new BufferedInputStream(urlFile.openStream());

    byte[] buf = new byte[1024];
    int n = 0;/*from  ww w.  j  a  v  a  2  s  .c o m*/
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();

    /*
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream(rutaDestino);
    fos.write(response);
    fos.close();
    */

    File file = new File(rutaDestino);
    file.setWritable(true);
    file.setReadable(true);

    BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
    bw.write(out.toString());
    bw.close();
}

From source file:com.google.jstestdriver.server.gateway.GatewayEntityMethodTest.java

License:asdf

public void testGetNameGetUriAndGetRequestEntity() throws Exception {
    ByteArrayInputStream in = new ByteArrayInputStream("ASDF".getBytes());
    EntityEnclosingMethod method = new GatewayEntityMethod("POST", "http://www.google.com/search", in);
    assertEquals("POST", method.getName());
    assertEquals("http://www.google.com/search", method.getURI().toString());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    method.getRequestEntity().writeRequest(out);
    assertEquals("ASDF", out.toString());
}