Example usage for org.apache.commons.io FileUtils readFileToByteArray

List of usage examples for org.apache.commons.io FileUtils readFileToByteArray

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils readFileToByteArray.

Prototype

public static byte[] readFileToByteArray(File file) throws IOException 

Source Link

Document

Reads the contents of a file into a byte array.

Usage

From source file:de.micromata.genome.gwiki.plugin.rogmp3_1_0.RogMp3ActionBean.java

private void serveFile(File f, String contentType) {
    GLog.note(GWikiLogCategory.Wiki, "RogMp3; serveFile: " + f.getAbsolutePath());
    try {/*from   w w w  . j  av a 2  s.  com*/
        byte[] data = FileUtils.readFileToByteArray(f);
        serveFile(data, f.getName(), contentType);
    } catch (IOException e) {
        GLog.note(GWikiLogCategory.Wiki, "Cannot read  file: " + f.getAbsolutePath() + ": " + e.toString());
    }
}

From source file:com.ruesga.rview.misc.CacheHelper.java

public static byte[] readFileCache(Context context, Account account, String name) throws IOException {
    File file = new File(getAccountCacheDir(context, account), name);
    if (file.exists()) {
        return FileUtils.readFileToByteArray(file);
    }/*  w  w w  .  j a v  a 2s .  co  m*/
    return null;
}

From source file:de.undercouch.gradle.tasks.download.FunctionalDownloadTest.java

/**
 * Test if the download task is triggered if another tasks depends on its
 * output files//from   w w  w .j  a  va 2  s  .  com
 * @throws Exception if anything went wrong
 */
@Test
public void fileDependenciesWithMultipleSourcesTriggersDownloadTask() throws Exception {
    assertTaskSuccess(runTask(":processTask", new Parameters(multipleSrc, dest, true, false)));
    assertTrue(destFile.isDirectory());
    assertArrayEquals(contents, FileUtils.readFileToByteArray(new File(destFile, TEST_FILE_NAME)));
    assertArrayEquals(contents2, FileUtils.readFileToByteArray(new File(destFile, TEST_FILE_NAME2)));
}

From source file:com.bittorrent.mpetazzoni.client.SharedTorrent.java

/**
 * Create a new shared torrent from the given torrent file.
 *
 * @param source The <code>.torrent</code> file to read the torrent
 * meta-info from.//from ww  w. j  a  va 2 s. c  o m
 * @param parent The parent directory or location of the torrent files.
 * @throws IOException When the torrent file cannot be read or decoded.
 */
public static SharedTorrent fromFile(File source, File parent) throws IOException {
    byte[] data = FileUtils.readFileToByteArray(source);
    return new SharedTorrent(data, parent);
}

From source file:algorithm.PDFFileAttacher.java

/**
 * PDFBox adds a new line with ^M at the end of the restored payload file.
 * This method removes the buggy line./* w  w  w  .  ja va 2s  .c o  m*/
 * 
 * @param restoredPayload
 */
private void removeBuggyLineEnding(File restoredPayload) throws IOException {
    byte[] data = FileUtils.readFileToByteArray(restoredPayload);
    FileUtils.writeByteArrayToFile(restoredPayload, Arrays.copyOfRange(data, 0, data.length - 2), false);
}

From source file:com.falkonry.TestAddDataStream.java

/**
 * Should add narrow data to datastream in stream CSV format
 * @throws Exception/*from w ww  . j a  v  a2  s. c om*/
 */
@Test
public void addDataCsvStream() throws Exception {

    Datastream ds = new Datastream();
    ds.setName("Test-DS3-" + Math.random());

    TimeObject time = new TimeObject();
    time.setIdentifier("time");
    time.setFormat("iso_8601");
    time.setZone("GMT");

    Signal signal = new Signal();
    signal.setTagIdentifier("tag");
    signal.setValueIdentifier("value");
    signal.setDelimiter("_");
    signal.setIsSignalPrefix(false);

    Datasource dataSource = new Datasource();
    dataSource.setType("STANDALONE");

    Field field = new Field();
    field.setSiganl(signal);
    field.setTime(time);

    ds.setDatasource(dataSource);
    ds.setField(field);

    Datastream datastream = falkonry.createDatastream(ds);
    datastreams.add(datastream);

    Map<String, String> options = new HashMap<String, String>();

    File file = new File("res/data.csv");
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));

    InputStatus inputStatus = falkonry.addInputStream(datastream.getId(), byteArrayInputStream, options);
    Assert.assertEquals(inputStatus.getAction(), "ADD_DATA_DATASTREAM");
    Assert.assertEquals(inputStatus.getStatus(), "PENDING");

    Datastream datastream1 = falkonry.getDatastream(datastream.getId());
    Assert.assertEquals(datastream1.getId(), datastream.getId());
    Assert.assertEquals(datastream1.getName(), datastream.getName());

    Field field1 = datastream1.getField();

    Signal signal1 = field1.getSignal();
    Assert.assertEquals(signal1.getDelimiter(), signal.getDelimiter());
    Assert.assertEquals(signal1.getTagIdentifier(), signal.getTagIdentifier());
    Assert.assertEquals(signal1.getValueIdentifier(), signal.getValueIdentifier());
    Assert.assertEquals(signal1.getIsSignalPrefix(), signal.getIsSignalPrefix());
}

From source file:ee.pri.rl.blog.service.BlogServiceImpl.java

/**
 * @see BlogService#getUploadedFileTag(String)
 */// ww  w  .ja v a2 s  . co m
@Override
public String getUploadedFileTag(String path) throws NoSuchFileException {
    synchronized (fileChecksums) {
        if (!fileChecksums.containsKey(path)) {
            File directory = new File(getSetting(SettingName.UPLOAD_PATH).getValue());
            File file = new File(directory, path);
            try {
                log.info("Calculating checksum for " + path);
                fileChecksums.put(path, DigestUtils.md5Hex(FileUtils.readFileToByteArray(file)));
            } catch (IOException e) {
                log.warn("Cannot find checksum for " + path + ": " + e.getMessage());
                throw new NoSuchFileException(path, e);
            }
        }

        return fileChecksums.get(path);
    }
}

From source file:com.weaverplatform.nifi.CreateIndividualTest.java

@Test
public void testOnTrigger() {

    try {//from w ww . j av  a 2  s.c om

        // Random info and simulate flowfile (with attributes) passed through to this processor in early state
        String file = "line.txt";
        byte[] contents = FileUtils
                .readFileToByteArray(new File(getClass().getClassLoader().getResource(file).getFile()));
        InputStream in = new ByteArrayInputStream(contents);
        InputStream cont = new ByteArrayInputStream(IOUtils.toByteArray(in));
        ProcessSession session = testRunner.getProcessSessionFactory().createSession();
        FlowFile f = session.create();
        f = session.importFrom(cont, f);
        f = session.putAttribute(f, "id", "cio5u54ts00023j6ku6j3j1jg"); //"816ee370-4274-e211-a3a8-b8ac6f902f00");

        String connectionUrl = "http://weaver.test.ib.weaverplatform.com";

        //from nifi-envi the user specifies this dynamic attribute, which to look for on the flowfile later
        // Add properties (required)
        testRunner.setProperty(CreateIndividual.WEAVER, connectionUrl);//"http://localhost:9487");
        testRunner.setProperty(CreateIndividual.INDIVIDUAL_ATTRIBUTE, "id"); // RDF_TYPE_STATIC: ib:Afsluitboom

        // Add the flowfile to the runner
        testRunner.enqueue(f);

        // Run the enqueued content, it also takes an int = number of contents queued
        testRunner.run();

        Weaver weaver = new Weaver();
        weaver.connect(new WeaverSocket(new URI(connectionUrl)));

        Entity e = weaver.get("cio5u54ts00023j6ku6j3j1jg");
        System.out.println(e.getId());

        //            //get original flowfile contents
        //            List<MockFlowFile> results = testRunner.getFlowFilesForRelationship("original");
        //            MockFlowFile result = results.get(0);
        //            String resultValue = new String(testRunner.getContentAsByteArray(result));
        //            System.out.println(resultValue);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.qubit.terra.docs.util.ReportGenerator.java

public static ReportGenerator create(final String template, final String fontsPath, final String mimeType) {
    try {/* w w  w.j ava  2  s.c  o  m*/
        return new ReportGenerator(FileUtils.readFileToByteArray(new File(template)), fontsPath, mimeType);
    } catch (FileNotFoundException e) {
        throw new ReportGenerationException("Error finding template", e);
    } catch (IOException e) {
        throw new ReportGenerationException("Error finding template", e);
    }
}

From source file:com.googlecode.gwtphonegap.server.file.FileRemoteServiceServlet.java

@Override
public String readAsDataUrl(String relativePath) throws FileErrorException {
    File basePath = new File(path);
    File file = new File(basePath, relativePath);
    ensureLocalRoot(basePath, file);/*from  w w  w.j a  v  a 2 s.  co m*/

    try {
        byte[] binaryData = FileUtils.readFileToByteArray(file);
        byte[] base64 = Base64.encodeBase64(binaryData);

        String base64String = new String(base64, "UTF-8");
        String mimeType = guessMimeType(file);
        return "data:" + mimeType + ";base64," + base64String;

    } catch (IOException e) {
        logger.log(Level.WARNING, "error while reading file", e);
        throw new FileErrorException(FileError.NOT_READABLE_ERR);
    }
}