Example usage for org.apache.commons.io IOUtils toByteArray

List of usage examples for org.apache.commons.io IOUtils toByteArray

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toByteArray.

Prototype

public static byte[] toByteArray(String input) throws IOException 

Source Link

Document

Get the contents of a String as a byte[] using the default character encoding of the platform.

Usage

From source file:com.ms.commons.standalone.utils.Shell.java

public static String exec(String cmd) {
    Process process = null;// w ww. j  a v a 2 s. co  m
    String[] cmds = { "/bin/bash", "-c", cmd, };
    try {
        process = new ProcessBuilder(cmds).redirectErrorStream(true).start();
        byte[] buffer = IOUtils.toByteArray(process.getInputStream());
        process.waitFor();
        return new String(buffer, "utf-8");
    } catch (Exception e) {
        logger.error("runtime.exec cmd: " + cmd + " failed", e);
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
    return "";
}

From source file:es.caib.sgtsic.util.DataHandlers.java

public static byte[] dataHandlerToByteArray(DataHandler dataHandler) throws IOException {

    InputStream is = dataHandler.getInputStream();
    return IOUtils.toByteArray(is);

}

From source file:llc.rockford.webcast.StringUtils.java

public static String encodeBase64BinaryFile(InputStream is) {
    String base64EncodedFile = "";
    try {//from w w  w  .  ja  v  a2s.c o  m
        byte[] bytesFromFile = IOUtils.toByteArray(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(baos);
        stream.write(bytesFromFile);
        stream.flush();
        stream.close();
        base64EncodedFile = new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return base64EncodedFile;
}

From source file:cz.vutbr.fit.xhriba01.bc.lib.Utils.java

public static byte[] inputStreamToBytes(InputStream stream) {

    try {//from www .  ja  v  a  2  s  .c o  m

        return IOUtils.toByteArray(stream);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:com.hurence.logisland.util.file.FileUtil.java

/**
 * load logs from resources as a string or return null if an error occurs
 *//*from www.j a  v a 2s. c o m*/
public static String loadFileContentAsString(String path, String encoding) {
    try {
        final InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(path);
        assert is != null;
        byte[] encoded = IOUtils.toByteArray(is);
        is.close();
        return new String(encoded, encoding);
    } catch (Exception e) {
        logger.error(String.format("Could not load json file %s and convert to string", path), e);
        return null;
    }
}

From source file:com.formkiq.core.util.Resources.java

/**
 * Gets a file from classpath as bytes./*from ww  w  .ja va 2s. c o  m*/
 * @param file String
 * @return byte[]
 * @throws IOException IOException
 */
public static byte[] getResourceAsBytes(final String file) throws IOException {
    InputStream is = getResourceAsInputStream(file);
    try {
        byte[] bytes = IOUtils.toByteArray(is);
        return bytes;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:de.dennishoersch.web.css.images.resolver.HttpPathResolver.java

private static Path downloadToTmp(String url) throws IOException {
    try {/*from ww w.j  ava 2s  . com*/
        URL weburl = new URL(url);
        byte[] bytes = IOUtils.toByteArray(weburl.openStream());

        Path tempFile = Files.createTempFile("__" + HttpPathResolver.class.getSimpleName() + "_", "");
        Files.write(tempFile, bytes);

        return tempFile;
    } catch (IllegalArgumentException e) {
        logger.log(Level.WARNING, "url of wrong format: '" + url + "'!", e);
        throw e;
    }
}

From source file:com.wisemapping.util.ZipUtils.java

public static byte[] zipToBytes(byte[] zip) throws IOException {

    byte[] result = null;
    if (zip != null) {
        final ByteArrayInputStream in = new ByteArrayInputStream(zip);
        final ZipInputStream zipIn = new ZipInputStream(in);
        zipIn.getNextEntry();//from   w w w.j ava2s  .  c o  m
        result = IOUtils.toByteArray(zipIn);

        zipIn.closeEntry();
        zipIn.close();
    }

    return result;
}

From source file:net.jadler.matchers.RawBodyRequestMatcher.java

@Override
protected byte[] retrieveValue(final Request req) throws Exception {
    return IOUtils.toByteArray(req.getBody());
}

From source file:com.linkedin.flashback.factory.RecordedHttpBodyFactory.java

/**
 * Given content type, charset, construct concrete {@link com.linkedin.flashback.serializable.RecordedByteHttpBody}
 * @param contentType http body content type
 * @param contentEncoding http body content encoding
 * @param inputStream input stream from http request/response
 * @param charset charset/*from www.  ja  v  a 2 s  .  c om*/
 * @return concrete {@link com.linkedin.flashback.serializable.RecordedHttpBody}
 *
 * */
public static RecordedHttpBody create(String contentType, String contentEncoding, final InputStream inputStream,
        final String charset) throws IOException {

    if (HttpUtilities.isCompressedContentEncoding(contentEncoding)) {
        return new RecordedEncodedHttpBody(IOUtils.toByteArray(inputStream), contentEncoding, charset,
                contentType);
    } else if (HttpUtilities.isTextContentType(contentType)) {
        return new RecordedStringHttpBody(IOUtils.toString(inputStream, charset));
    } else {
        return new RecordedByteHttpBody(IOUtils.toByteArray(inputStream));
    }
}