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:fi.jumi.core.util.LocallyDefiningClassLoader.java

private byte[] getBytecode(String name) {
    String resource = name.replace('.', '/') + ".class";
    InputStream in = getResourceAsStream(resource);
    if (in == null) {
        return null;
    }//  w w w  . j  a v a2s  .  c o  m
    try {
        return IOUtils.toByteArray(in);
    } catch (IOException e) {
        throw Boilerplate.rethrow(e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.netsteadfast.greenstep.util.DynamicHqlUtils.java

public static DynamicHql loadResource(String resource) throws Exception {
    DynamicHql dynamicHql = resourceDataMap.get(resource);
    if (dynamicHql == null) {
        InputStream in = null;/*from  w  w w. j ava 2 s .  co  m*/
        try {
            in = DynamicHqlUtils.class.getResourceAsStream("/dynamichql/" + resource);
            byte[] xmlBytes = IOUtils.toByteArray(in);
            JAXBContext jaxbContext = JAXBContext.newInstance(DynamicHql.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            dynamicHql = (DynamicHql) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xmlBytes));
            resourceDataMap.put(resource, dynamicHql);
        } catch (IOException e) {
            logger.error(e.getMessage().toString());
            throw e;
        } finally {
            if (in != null) {
                IOUtils.closeQuietly(in);
            }
            in = null;
        }
    }
    return dynamicHql;
}

From source file:CiudadesApp.Modelo.Parameter.GuardarCiudades_Parameter.java

public GuardarCiudades_Parameter(HttpServletRequest request) {

    try {//from   w  ww  . j a va2 s .co m
        request.setCharacterEncoding("UTF-8");

        nombreCiudad = (String) request.getParameter("nombreCiudad");
        descripcion = (String) request.getParameter("descripcion");
        Part filePart = request.getPart("fileName");
        InputStream in = filePart.getInputStream();
        foto = IOUtils.toByteArray(in);

    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(GuardarCiudades_Parameter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GuardarCiudades_Parameter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ServletException ex) {
        Logger.getLogger(GuardarCiudades_Parameter.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:ai.nitro.bot4j.integration.alexa.receive.webhook.impl.AlexaWebhookImpl.java

@Override
public String post(final HttpServletRequest req, final HttpServletResponse res) throws IOException {
    String result = "";

    try {/* w  ww.  j  a  va2 s . c o  m*/
        final byte[] serializedSpeechletRequest = IOUtils.toByteArray(req.getInputStream());

        SpeechletRequestSignatureVerifier.checkRequestSignature(serializedSpeechletRequest,
                req.getHeader(Sdk.SIGNATURE_REQUEST_HEADER),
                req.getHeader(Sdk.SIGNATURE_CERTIFICATE_CHAIN_URL_REQUEST_HEADER));

        final byte[] outputBytes = alexaReceiveHandler.handleSpeechletRequest(serializedSpeechletRequest);
        result = new String(outputBytes);
    } catch (final IOException e) {
        //LOG.warn(e.getMessage(), e);
    } catch (final SecurityException e) {
        final int statusCode = HttpServletResponse.SC_BAD_REQUEST;
        //LOG.error("Exception occurred in doPost, returning status code {}", statusCode, e);
        res.sendError(statusCode, e.getMessage());
    }

    return result;
}

From source file:com.stgmastek.birt.report.utils.ReportUtility.java

public static IReportRunnable getReportRunnable(IReportEngine engine, Report report) {
    // Convert the Design File Identifier to relative (from classes) path
    String reportFileFQName = report.getDesignFileIdentifier();
    int extensionDotIndex = reportFileFQName.lastIndexOf('.');
    String reportFileName = reportFileFQName.substring(0, extensionDotIndex);
    String designFileRelativePath = "/" + reportFileName.replace('.', '/')
            + reportFileFQName.substring(extensionDotIndex);

    // Read Design File
    InputStream is;// w w w  .  ja  v a  2  s. c o m
    try {
        is = ResourceUtils.getResourceAsStream(ReportUtility.class, designFileRelativePath);
    } catch (IOException e) {
        throw new ReportServiceException("Could not locate Report Design file #" + designFileRelativePath, e);
    }

    // convert to Byte Array Stream
    ByteArrayInputStream bis = null;
    try {
        byte bytes[] = IOUtils.toByteArray(is);
        bis = new ByteArrayInputStream(bytes);
    } catch (IOException e) {
        String msg = "Exception while converting the read InputStream of design file : ["
                + designFileRelativePath + "] to ByteArrayStream";
        throw new ReportServiceException(msg, e);
    }

    IReportRunnable reportRunnable = null;
    try {
        reportRunnable = engine.openReportDesign(bis);
    } catch (Exception e) {
        String msg = "Exception while creating Runnable Report : [" + e.getMessage() + "] for Design File : ["
                + designFileRelativePath + "]";
        throw new ReportServiceException(msg, e);
    }

    return reportRunnable;
}

From source file:io.github.azige.bbs.web.controller.AvatarController.java

@RequestMapping("/avatar/{id}")
public ResponseEntity<byte[]> avatarResource(@PathVariable Long id) throws IOException {
    return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(IOUtils
            .toByteArray(AvatarController.class.getResourceAsStream("/io/github/azige/bbs/res/haruna.png")));
}

From source file:com.newinit.filter.XSSRequestWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    if (rawData == null) {
        rawData = IOUtils.toByteArray(this.request.getReader());
        servletStream.stream = new ByteArrayInputStream(rawData);
    }/*from   w ww.j a v  a  2 s  .  c  o  m*/
    return servletStream;
}

From source file:com.jive.myco.seyren.core.util.graphite.ByteArrayResponseHandler.java

@Override
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    HttpEntity entity = response.getEntity();
    try {/*from  w w  w  .ja  v a2  s  . c o  m*/
        InputStream stream = entity.getContent();
        return IOUtils.toByteArray(stream);
    } finally {
        EntityUtils.consume(entity);
    }
}

From source file:com.hurence.logisland.processor.hbase.util.ObjectSerDe.java

@Override
public Object deserialize(InputStream is) throws DeserializationException, IOException {
    if (is == null) {
        return null;
    }//from   w  ww  .j  a  v  a2 s.  c  om

    byte[] input = IOUtils.toByteArray(is);
    if (input == null || input.length == 0) {
        return null;
    }

    try (final ByteArrayInputStream in = new ByteArrayInputStream(input);
            final ObjectInputStream objIn = new ObjectInputStream(in)) {
        return objIn.readObject();
    } catch (ClassNotFoundException e) {
        throw new DeserializationException("Could not deserialize object due to ClassNotFoundException", e);
    }
}

From source file:com.igormaznitsa.zxpoly.utils.ROMLoader.java

static byte[] loadHTTPArchive(final String url) throws IOException {
    final HttpClient client = HttpClientBuilder.create().build();
    final HttpContext context = HttpClientContext.create();
    final HttpGet get = new HttpGet(url);
    final HttpResponse response = client.execute(get, context);

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        InputStream in = null;/*from ww  w  .java 2  s  .c o m*/
        try {
            final HttpEntity entity = response.getEntity();
            in = entity.getContent();
            final byte[] data = entity.getContentLength() < 0L ? IOUtils.toByteArray(entity.getContent())
                    : IOUtils.toByteArray(entity.getContent(), entity.getContentLength());
            return data;
        } finally {
            IOUtils.closeQuietly(in);
        }
    } else {
        throw new IOException("Can't download from http '" + url + "' code [" + url + ']');
    }
}