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:be.solidx.hot.web.deprecated.ScriptExecutorController.java

public ResponseEntity<String> handleScript(WebRequest webRequest, String scriptName) {
    try {//  www. ja  v  a 2 s .c  om
        scriptName = scriptName + getScriptExtension();
        Script<COMPILED_SCRIPT> script = buildScript(IOUtils.toByteArray(loadResource(scriptName)), scriptName);
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        scriptExecutor.execute(script, webRequest, dbMap, printWriter);
        return new ResponseEntity<String>(stringWriter.toString(), httpHeaders, HttpStatus.ACCEPTED);
    } catch (Exception e) {
        StringWriter stringWriter = new StringWriter();
        printErrorPage(e, stringWriter);
        return new ResponseEntity<String>(stringWriter.toString(), httpHeaders, HttpStatus.ACCEPTED);
    }
}

From source file:de.berlin.magun.nfcmime.core.NdefMessageBuilder.java

/**
 * Stores an NDEF record from an InputStream. Will throw an IOException if the last NDEF record is
 * a checksum added with addChecksum()./*from ww  w  .  ja v a 2  s.  c o  m*/
 * @param is InputStream that holds the payload data of the NDEF record.
 * @param mimetype MIME type definition, e.g. "text/plain"
 * @throws IOException
 */
public void addMimeRecord(InputStream is, String mimetype) throws IOException {
    if (!this.hasChecksum) {
        recordlist.add(new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimetype.getBytes(Charset.forName("US-ASCII")),
                new byte[0], IOUtils.toByteArray(is)));
    } else {
        throw new IOException("Cannot add record - last record is a checksum.");
    }
}

From source file:com.createsend.models.transactional.request.Attachment.java

/**
 * Base64 encodes the input stream and stores the result as the Content.
 * @param inputStream/*w w  w  .j av  a 2  s  .c  o  m*/
 * @throws IOException
 */
public void base64EncodeContentStream(InputStream inputStream) throws IOException {
    byte[] bytes = IOUtils.toByteArray(inputStream);
    byte[] bytesBase64 = Base64.encodeBase64(bytes);
    Content = new String(bytesBase64);
}

From source file:db.migration.V2017_08_27_14_00__Import_Images_Into_Db.java

@Override
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    LOG.info("Importing images into db");
    List<ImageObject> images = jdbcTemplate.query(
            "select `" + FIELD_ID + "`, `" + FIELD_FILE_PATH + "` from `" + TABLE_NAME + "`",
            new ImageObjectRowMapper<>());
    for (ImageObject entry : images) {
        try {//from  w w w  .j av  a  2 s.c o  m
            File file = new File(entry.getFilePath());
            byte[] data = IOUtils.toByteArray(new FileInputStream(file));
            if (data != null) {
                ImageCategory category = getCategory(entry.getFilePath());
                jdbcTemplate.update(connection -> {
                    PreparedStatement ps = connection.prepareStatement(
                            String.format("update `%s` set `%s` = ?, `%s` = ?, `%s` = ? where `%s` = ?",
                                    TABLE_NAME, FIELD_CONTENT_LENGTH, FIELD_CONTENT, FIELD_CATEGORY, FIELD_ID),
                            new String[] { FIELD_CONTENT });
                    ps.setLong(1, 0L + data.length);
                    ps.setBytes(2, data);
                    ps.setString(3, category.name());
                    ps.setLong(4, entry.getId());
                    return ps;
                });
            }
        } catch (Exception e) {
            LOG.error(String.format("Unable to import file %s: %s", entry.getFilePath(), e.getMessage()));
        }
    }
    LOG.info("Done importing images into db");
}

From source file:de.hska.ld.content.persistence.domain.Attachment.java

public void setNewValues(InputStream inputStream, String name) {
    try {//  w  w w  .  j a v  a2s .  com
        this.mimeType = URLConnection.guessContentTypeFromName(name);
        this.source = IOUtils.toByteArray(inputStream);
        this.name = name;
    } catch (IOException e) {
        LOGGER.warn("Unable to set source or mime type from input stream.", e);
    }
}

From source file:com.epam.wilma.webapp.config.servlet.helper.InputStreamUtil.java

/**
 * Reads an inputStream into a byte array.
 * @param inputStream to read from/*from   w  w w. j a  va 2s.  co  m*/
 * @return contents of the inputStream as a byte array
 * @throws IOException if an IO error occurs
 */
public byte[] transformToByteArray(InputStream inputStream) throws IOException {
    return IOUtils.toByteArray(inputStream);
}

From source file:com.msopentech.odatajclient.engine.data.ODataObjectWrapper.java

/**
 * Constructor.// w w  w .  j a  v a2 s  .  co  m
 *
 * @param is source input stream.
 * @param format source format (<tt>ODataPubFormat</tt>, <tt>ODataFormat</tt>, <tt>ODataValueFormat</tt>,
 * <tt>ODataServiceDocumentFormat</tt>).
 */
public ODataObjectWrapper(final ODataReader reader, final InputStream is, final String format) {
    this.reader = reader;
    try {
        this.obj = IOUtils.toByteArray(is);
        this.format = format;
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:co.com.realtech.mariner.util.jsf.file.FileDownloader.java

/**
 * Crea el informe seleccionado en formato excel.
 *
 * @param colHeaders/*w  w  w.jav a2 s.c om*/
 * @param datos
 * @param nombreHoja
 * @param nombreArchivo
 * @param contexto
 * @param descargar
 * @return 
 * @throws java.lang.Exception
 */
public File construirExcel(List<String> colHeaders, List<Object> datos, String nombreHoja, String nombreArchivo,
        FacesContext contexto, boolean descargar) throws Exception {
    File tempFile = null;
    try {
        ResultSetToExcel rste = new ResultSetToExcel(nombreHoja);
        tempFile = File.createTempFile("reporteGeneral", ".xlsx");
        OutputStream os = new FileOutputStream(tempFile);
        rste.generate(os, datos, colHeaders);
        if (descargar) {
            InputStream is = new FileInputStream(tempFile);
            byte[] bytes;
            bytes = IOUtils.toByteArray(is);
            descargarArchivo(contexto, bytes, nombreArchivo, "xlsx");
        }
    } catch (Exception e) {
        throw e;
    }
    return tempFile;
}

From source file:com.teasoft.teavote.util.Signature.java

private PublicKey getPublicKey()
        throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
    Resource resource = res.getResource("classpath:gotaSafui");
    byte[] pubKeyBytes;
    try (InputStream pubKeyInputStream = resource.getInputStream()) {
        pubKeyBytes = IOUtils.toByteArray(pubKeyInputStream);
        pubKeyBytes = Base64.decodeBase64(pubKeyBytes);
    }/*  w  w  w  .j a v a2  s  .c  o  m*/
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
    PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
    return pubKey;
}

From source file:net.nicholaswilliams.java.licensing.samples.SampleFilePublicKeyDataProvider.java

/**
 * This method returns the data from the file containing the encrypted
 * public key from the public/private key pair. The contract for this
 * method can be fulfilled by storing the data in a byte array literal
 * in the source code itself.//from w  w  w  .j a  v  a 2 s . co  m
 *
 * @return the encrypted file contents from the public key file.
 * @throws KeyNotFoundException if the key data could not be retrieved; an acceptable message or chained cause must be provided.
 */
public byte[] getEncryptedPublicKeyData() throws KeyNotFoundException {
    try {
        return IOUtils.toByteArray(this.getClass().getResourceAsStream("sample.public.key"));
    } catch (IOException e) {
        throw new KeyNotFoundException("The public key file was not found.", e);
    }
}