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:de.catma.document.source.contenthandler.DefaultProtocolHandler.java

private void handle(URI sourceDocURI) throws IOException {

    final String sourceDocURL = sourceDocURI.toURL().toString();
    final String sourceURIPath = sourceDocURI.getPath();

    SourceDocumentHandler sourceDocumentHandler = new SourceDocumentHandler();

    URLConnection urlConnection = new URL(sourceDocURL).openConnection();

    InputStream is = urlConnection.getInputStream();
    try {/* www .j av a2s . c o m*/
        this.byteContent = IOUtils.toByteArray(is);
        if (this.mimeType == null) {
            this.mimeType = sourceDocumentHandler.getMimeType(sourceURIPath, urlConnection,
                    FileType.TEXT.getMimeType());
        }

        this.encoding = Charset.defaultCharset().name();

        if (this.mimeType.equals(FileType.TEXT.getMimeType())
                || (this.mimeType.equals(FileType.HTML.getMimeType()))) {
            this.encoding = sourceDocumentHandler.getEncoding(urlConnection, byteContent,
                    Charset.defaultCharset().name());
        }
    } finally {
        is.close();
    }

}

From source file:com.devnexus.ting.core.dao.DocumentDaoTest.java

/**
 * Test to verify that the seed data is correctly populated. Typically there
 * should be 10 industries in the system:
 *
 *//*from w  w w  . j  a  va  2  s  .c  o  m*/
@Test
public void testInsertDocument() throws IOException {

    FileData document = new FileData();
    document.setFileModified(new Date());
    document.setName("cartman.jpg");
    document.setType("image/jpeg");

    InputStream is = DocumentDaoTest.class.getResourceAsStream("/images/speakers/hans_dockter.jpg");

    byte[] imageData = IOUtils.toByteArray(is);
    document.setFileSize(Long.valueOf(imageData.length));
    document.setFileData(imageData);

    FileData savedDocument = documentDao.save(document);
    super.entityManager.flush();

    FileData docFromDb = documentDao.getOne(savedDocument.getId());

    Assert.assertEquals("cartman.jpg", docFromDb.getName());

}

From source file:com.cognifide.aet.job.common.ArtifactDAOMock.java

@Override
public String saveArtifact(DBKey dbKey, InputStream data) {
    try {//  w w w  .j a  v a 2 s .c o  m
        savedArtifactDatas.add(IOUtils.toByteArray(data));
    } catch (IOException e) {
        return null;
    }
    return "";
}

From source file:com.siemens.sw360.fossology.config.FossologySettings.java

private static byte[] loadKeyFile(String keyFilePath) {
    byte[] fossologyPrivateKey = null;
    try {/*from w ww  .  jav a 2 s  . c  om*/
        try (InputStream keyFileStream = FossologySettings.class.getResourceAsStream(keyFilePath)) {
            if (keyFileStream == null)
                throw new IOException("cannot open " + keyFilePath);
            fossologyPrivateKey = IOUtils.toByteArray(keyFileStream);
        }
    } catch (IOException e) {
        log.error("Cannot load private key", e);
    }
    return fossologyPrivateKey;
}

From source file:de.tudarmstadt.ukp.dkpro.c4corpus.language.CharsetDetectorTest.java

@Test
public void testDetectCharset() throws Exception {
    // resources/charset-detector contains *.txt files
    URL url = getClass().getClassLoader().getResource("charset-detector");
    assertNotNull(url);//from   w  w  w . j a va  2s.  c  o m

    File dir = new File(url.getFile());

    File[] files = dir.listFiles();
    assertNotNull(files);

    for (File file : files) {
        // file names are: charset_lang.txt (_lang is optional!)
        String charsetName = file.getName().replaceAll("\\.txt$", "").split("_")[0];

        Charset goldCharset = Charset.forName(charsetName);

        byte[] bytes = IOUtils.toByteArray(new FileInputStream(file));

        // distracting the method by wrong declared charset
        String distract = "utf-16LE";
        Charset detectedCharset = detector.detectCharset(bytes, distract);

        // System.out.println(detectedCharset);

        assertEquals(goldCharset, detectedCharset);
    }
}

From source file:adams.data.imagemagick.PPMOutputConsumer.java

/**
 * Reads the ppm input stream into a {@link BufferedImage}.
 * // w w  w  .j av a2s  .  c  om
 * @param stream      the stream to read from
 * @throws IOException   if reading from stream fails
 */
@Override
public void consumeOutput(InputStream stream) throws IOException {
    byte[] bytes;
    ByteArraySeekableStream bstream;
    ParameterBlock pb;
    RenderedOp op;

    m_Image = null;
    bytes = IOUtils.toByteArray(stream);
    bstream = new ByteArraySeekableStream(bytes);
    pb = new ParameterBlock();
    pb.add(bstream);
    op = JAI.create("PNM", pb);
    m_Image = op.getAsBufferedImage();
}

From source file:be.solidx.hot.test.data.jdbc.TestPyCollectionApi.java

@Test
public void testSync() throws Exception {
    Map<String, Object> context = new HashMap<>();
    context.put("db", db);
    Script<CompiledScript> script = new Script<>(
            IOUtils.toByteArray(getClass().getResourceAsStream("/be/solidx/hot/test/data/jdbc/scripts/db.py")),
            "py-db");
    PythonScriptExecutor executor = new PythonScriptExecutor();
    executor.execute(script, context);// w ww .  j a va  2  s. c o m
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

/**
 * Decompresses the given byte array without transforming it into a String
 *
 * @param compressed byte array input/*  ww  w. j ava 2s. c o  m*/
 * @return decompressed data in a byte array
 * @throws IOException
 */
public static byte[] decompressBytesNonBase64(byte[] compressed) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(compressed);
    InputStream gis = new GZIPInputStream(is);
    return IOUtils.toByteArray(gis);
}

From source file:is.hax.spring.web.multipart.StreamingMultipartFile.java

public byte[] getBytes() throws IOException {
    if (bytes == null) {
        bytes = IOUtils.toByteArray(item.openStream());
    }//from   w w w  .  j  a  va 2  s  .c o m

    return bytes;

}

From source file:com.dvidder.service.AccountService.java

@Transactional
public byte[] getProfilePictureForUsername(String username) throws IOException {
    Account account = accountRepository.findByUsername(username);
    if (account.getProfilePicture().isDefaultPic()) {

        byte[] imgData = new byte[5000];
        Resource resource = new ClassPathResource("static/img/default-profile-pic.png");
        if (!resource.exists()) {
            System.out.println("RESOURCE DOESN'T EXIST");
        } else {/*from w  w w.  ja  v  a 2  s. c  om*/
            System.out.println("YES, RESOURCE EXISTS");

            System.out.println("resource content length: " + resource.contentLength());
        }

        imgData = IOUtils.toByteArray(resource.getInputStream());
        System.out.println("byte array length: " + imgData.length);
        return imgData;
    }
    return accountRepository.findByUsername(username).getProfilePicture().getImageData();
}