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:co.com.realtech.mariner.util.jsf.file.FileDownloader.java

/**
 * Descarga un archivo del excel como PDF
 * @param archivoExcel//from w w  w  . ja v a  2 s .  c  om
 * @param contexto
 * @param columnas
 * @return
 * @throws Exception 
 */
public File descargarPdfDeExcel(File archivoExcel, FacesContext contexto, int columnas) throws Exception {
    File tempFile = null;
    try {
        tempFile = File.createTempFile("reporteGeneral", ".pdf");
        ExcelToPdf.convert(archivoExcel, tempFile, columnas);
        InputStream is = new FileInputStream(tempFile);
        byte[] bytes;
        bytes = IOUtils.toByteArray(is);
        descargarArchivo(contexto, bytes, "reporteGeneral", "pdf");
    } catch (Exception e) {
        throw e;
    }
    return tempFile;
}

From source file:com.telefonica.euro_iaas.sdc.client.services.impl.ProductReleaseServiceImpl.java

/**
 * {@inheritDoc}//w  ww  .j a va 2 s  .c  o  m
 */
@Override
public ProductRelease add(ProductReleaseDto releaseDto, InputStream cookbook, InputStream files) {
    try {

        MultiPart payload = new MultiPart().bodyPart(new BodyPart(releaseDto, MediaType.valueOf(getType())))
                .bodyPart(new BodyPart(IOUtils.toByteArray(cookbook), MediaType.APPLICATION_OCTET_STREAM_TYPE))
                .bodyPart(new BodyPart(IOUtils.toByteArray(files), MediaType.APPLICATION_OCTET_STREAM_TYPE));

        String url = getBaseHost()
                + MessageFormat.format(ClientConstants.BASE_PRODUCT_RELEASE_PATH, releaseDto.getProductName());
        WebResource wr = getClient().resource(url);
        return wr.accept(getType()).type(MultiPartMediaTypes.MULTIPART_MIXED_TYPE).post(ProductRelease.class,
                payload);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cedarsoft.serialization.neo4j.test.utils.AbstractNeo4jSerializerTest2.java

@Nonnull
public static <T> Entry<? extends T> create(@Nonnull T object, @Nullable URL expected) {
    if (expected == null) {
        throw new IllegalStateException("No resource found for <" + object + ">");
    }//from ww  w .  j  a  va2 s  . c  om

    try {
        return new Entry<T>(object, IOUtils.toByteArray(expected.openStream()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.googlecode.dex2jar.test.TestUtils.java

public static void checkZipFile(File zip) throws ZipException, Exception {
    ZipFile zipFile = new ZipFile(zip);
    for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) {
        ZipEntry entry = e.nextElement();
        if (entry.getName().endsWith(".class")) {
            StringWriter sw = new StringWriter();
            // PrintWriter pw = new PrintWriter(sw);
            InputStream is = zipFile.getInputStream(entry);
            try {
                verify(new ClassReader(IOUtils.toByteArray(is)));
            } finally {
                IOUtils.closeQuietly(is);
            }/*from   w ww. ja  v a 2  s  .c om*/
            Assert.assertTrue(sw.toString(), sw.toString().length() == 0);
        }
    }
}

From source file:br.eb.ime.pfc.controllers.WMSStyleImageServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  ww  w  .  ja  va 2 s.  com
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    InputStream in = null;
    try {
        URL url = new URL(
                "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Download_font_awesome.svg/512px-Download_font_awesome.svg.png");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();
        in = conn.getInputStream();
        byte[] data = IOUtils.toByteArray(in);
        response.getOutputStream().write(data);
    } catch (Exception e) {

    } finally {
        in.close();
    }
}

From source file:com.cloudera.csd.components.JsonMdlParserTest.java

private byte[] getMdl(String name) throws IOException {
    InputStream stream = null;/*from  www  . j  a  va2  s. c o m*/
    try {
        stream = JsonSdlParserTest.class.getResourceAsStream(SdlTestUtils.SDL_PARSER_RESOURCE_PATH + name);
        return IOUtils.toByteArray(stream);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:jBittorrentAPI.utils.IOManager.java

/**
 * Read all bytes available in the stream
 * @param is InputStream/*from  ww  w.  ja va2  s  . com*/
 * @return byte[]
  */
public static byte[] readBytesFromStream(InputStream is) {
    byte[] result = new byte[0];
    try {
        result = IOUtils.toByteArray(is);
    } catch (IOException e) {
        lm.writeLog(e.getMessage());
        System.err.println("Problem when reading from stream...");
        e.printStackTrace();
    }
    return result;
}

From source file:com.crushpaper.CachingResource.java

private void cache() {
    // In this case the input was already cached.
    if (cachedInput != null) {
        return;/*from ww  w .  j  a va  2 s. c  o  m*/
    }

    try {
        cachedInput = IOUtils.toByteArray(resource.getInputStream());
    } catch (IOException e) {
    }
}

From source file:cpcc.core.utils.PngImageStreamResponseTest.java

@Test
public void shouldStreamGivenImageData() throws IOException {
    byte[] imageData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    PngImageStreamResponse response = new PngImageStreamResponse(imageData);

    assertThat(response.getStream()).isNotNull();
    byte[] actual = IOUtils.toByteArray(response.getStream());

    assertThat(actual).isEqualTo(imageData);
    assertThat(response.getStream().read()).isEqualTo(-1);
    response.getStream().close();/*  w w  w. j  a va  2s.c  om*/
}

From source file:com.jaspersoft.android.jaspermobile.data.repository.report.page.RawPageCreator.java

@NonNull
@Override/*from  www.  j av a  2  s .  com*/
public ReportPage create() throws Exception {
    String range = mPageRequest.getRange();
    PageRange pageRange = PageRange.parse(range);

    ReportExportOptions options = ReportExportOptions.builder()
            .withFormat(ReportFormat.valueOf(mPageRequest.getFormat())).withPageRange(pageRange).build();

    ReportExport export = mExecution.export(options);
    ReportExportOutput output = export.download();

    InputStream reportExport = output.getStream();

    try {
        byte[] content = IOUtils.toByteArray(reportExport);
        return new ReportPage(content, output.isFinal());
    } finally {
        IOUtils.closeQuietly(reportExport);
    }
}