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:com.oneis.appserver.StaticFileResponse.java

/**
 * Constructor//from  ww w .j av  a 2  s  . com
 *
 * @param pathname Filename of the file to read
 * @param mimeType MIME type of the response
 * @param allowCompression Whether to allow the response to be compressed.
 */
public StaticFileResponse(String pathname, String mimeType, boolean allowCompression) throws IOException {
    addHeader("Content-Type", mimeType);
    this.allowCompression = allowCompression;
    this.compressed = null;
    this.responseCode = 200;
    // Read file
    this.uncompressed = FileUtils.readFileToByteArray(new File(pathname));
}

From source file:com.truebanana.data.SecureDataFile.java

public static SecureDataFile loadFromFile(File file, String password) {
    String filePath = file.getAbsolutePath();
    if (loadedDataFiles.containsKey(filePath)) {
        Log.d("SecureDataFile", "Loading cached data file");
        return loadedDataFiles.get(filePath);
    } else {//from w ww.ja v a2s .c  o m
        Log.d("SecureDataFile", "Loading data file from disk");
        byte[] data = new byte[0];
        try {
            data = FileUtils.readFileToByteArray(file);
            Log.d("SecureDataFile", "Successfully read data file");
        } catch (IOException e) {
            Log.d("SecureDataFile", "Could not read data file");
        }

        SecureDataFile dataFile = new SecureDataFile(data, file, password);

        loadedDataFiles.put(filePath, dataFile);
        return dataFile;
    }
}

From source file:it.lic.storage.FileStorage.java

@Override
public final byte[] read(final Key key) throws LicenseToolException {
    final File source = new File(this.root, key.fullpath());
    try {/* ww w .  j  av a2 s.c  o  m*/
        return FileUtils.readFileToByteArray(source);
    } catch (final IOException e) {
        throw new LicenseToolException(String.format("Can't read key %s", key.fullpath()), e);
    }
}

From source file:nc.noumea.mairie.appock.services.impl.DownloadServiceImpl.java

@Override
public void downloadToUser(File fichier, String nomFichier) throws IOException {
    Filedownload.save(FileUtils.readFileToByteArray(fichier), null, nomFichier);
}

From source file:OwnCloud.java

public void upload(String path, File myFile) {
    try {/*from  ww  w .ja  v  a2s. co m*/
        Sardine sardine = SardineFactory.begin("admin", "qrdb.me2015");

        //System.out.println("Directory list: ");
        //List<DavResource> resources = sardine.getResources("http://www.qrdb.me/owncloud/remote.php/webdav/" + path);
        //for (DavResource res : resources)
        //{
        //     System.out.println(res.getName()); // calls the .toString() method.
        //}
        try {
            byte[] data = FileUtils.readFileToByteArray(myFile);
            sardine.put("http://www.qrdb.me/owncloud/remote.php/webdav/" + path, data);
        } catch (IOException e) {
            System.out.println("The conection to the OwnCloud failed: ");
        }
        boolean exists = sardine.exists("http://www.qrdb.me/owncloud/remote.php/webdav/" + path);
        if (exists) {
            System.out.println("The file: " + myFile + " was created in the OwnCloud server ");
        }
    } catch (SardineException e) {

    }
}

From source file:de.micromata.genome.gwiki.tools.PatchJavaHeader.java

public static void patchFile(File file) {
    try {/* w ww.  j av  a2 s. com*/
        byte[] data = FileUtils.readFileToByteArray(file);
        int ofs = skipComments(data);
        byte[] newData = new byte[data.length - ofs + headerBytes.length];
        System.arraycopy(headerBytes, 0, newData, 0, headerBytes.length);
        System.arraycopy(data, ofs, newData, headerBytes.length, data.length - ofs);
        // String nf = new String(newData);
        // System.out.println("\n\n\n" + nf);
        FileUtils.writeByteArrayToFile(file, newData);
    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }
}

From source file:com.qubit.terra.docs.core.DocumentGenerator.java

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

From source file:gov.nasa.jpl.cmac.extractors.ChecksumMetadataExtractor.java

@Override
protected Metadata getSciPgeSpecificMetadata(File sciPgeCreatedDataFile, Metadata inputMetadata,
        Object... customArgs) throws Exception {

    // empty metadata container
    Metadata met = new Metadata();

    // compute MD5 checksum
    String checksum = DigestUtils.md5Hex(FileUtils.readFileToByteArray(sciPgeCreatedDataFile));
    met.addMetadata("checksum", checksum);

    return met;//from ww w  . j  ava 2 s. c om

}

From source file:com.mirth.connect.plugins.datatypes.dicom.test.DICOMSerializerTest.java

@Test
public void testToXml3() throws Exception {
    String input = Base64//  w  ww.j a  v  a2 s .com
            .encodeBase64String(FileUtils.readFileToByteArray(new File("tests/test-dicom-input-3.dcm")));
    String output = FileUtils.readFileToString(new File("tests/test-dicom-output-3.xml"));
    DICOMSerializer serializer = new DICOMSerializer();
    Assert.assertEquals(output, TestUtil.prettyPrintXml(serializer.toXML(input)));
}

From source file:com.sysunite.weaver.nifi.FilterXMLNodesTest.java

@Test
public void testOnTrigger() {

    try {//from  w w  w  .jav a2  s  .  c o  m
        String file = "slagboom.xml";

        byte[] contents = FileUtils
                .readFileToByteArray(new File(getClass().getClassLoader().getResource(file).getFile()));

        InputStream in = new ByteArrayInputStream(contents);

        InputStream cont = new ByteArrayInputStream(IOUtils.toByteArray(in));

        // Generate a test runner to mock a processor in a flow
        TestRunner runner = TestRunners.newTestRunner(new FilterXMLNodes());

        // Add properites
        runner.setProperty(FilterXMLNodes.PROP_NODE, "FunctionalPhysicalObject");
        runner.setProperty(FilterXMLNodes.PROP_NODE_ATTRIBUTE, "name");
        runner.setProperty(FilterXMLNodes.PROP_PREGMATCH, "(.*)AB(.*)CT(.*)");

        // Add the content to the runner
        runner.enqueue(cont);

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

        // All results were processed with out failure
        //runner.assertQueueEmpty();

        // If you need to read or do aditional tests on results you can access the content
        List<MockFlowFile> results = runner.getFlowFilesForRelationship(FilterXMLNodes.MY_RELATIONSHIP);
        //assertTrue("1 match", results.size() == 1);

        System.out.println("aantal gevonden: " + results.size());

        //MockFlowFile result = results.get(0);
        //String resultValue = new String(runner.getContentAsByteArray(result));
        //System.out.println("Match: " + IOUtils.toString(runner.getContentAsByteArray(result)));
    } catch (IOException e) {
        System.out.println("FOUT!!");
        System.out.println(e.getStackTrace());
    }
}