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:net.rptools.lib.FileUtil.java

/**
 * Reads the entire content of the given file into a byte array.
 * /*from w  w  w.j a v  a 2  s .co m*/
 * @deprecated use {@link FileUtils#readFileToByteArray(File)} instead.
 * @param file
 * @return byte contents of the file
 * @throws IOException
 */
@Deprecated
public static byte[] loadFile(File file) throws IOException {
    return FileUtils.readFileToByteArray(file);
}

From source file:com.qubit.terra.docs.util.helpers.OpenofficeInProcessConverter.java

public static synchronized byte[] convert(final byte[] odtContent, final String tempDirFullPath,
        final String mimeTypeAbbreviation) {

    try {//from  w w w  . j  a  va 2 s  .  c  o m
        long currentTimeMillis = System.currentTimeMillis();
        if (System.getProperty("os.name").startsWith("Windows")) {
            final String odtFilename = tempDirFullPath + "openofficeConversion-" + currentTimeMillis + ".odt";

            FileUtils.writeByteArrayToFile(new File(odtFilename), odtContent);

            final Process process = Runtime.getRuntime()
                    .exec(String.format("soffice --headless --convert-to %s --outdir %s %s",
                            mimeTypeAbbreviation, tempDirFullPath.subSequence(0, tempDirFullPath.length() - 1),
                            odtFilename));

            try {
                process.waitFor();
            } catch (InterruptedException e) {
            }

            process.destroy();

            final String outputFilename = tempDirFullPath + "openofficeConversion-" + currentTimeMillis + "."
                    + mimeTypeAbbreviation;
            final byte[] output = FileUtils.readFileToByteArray(new File(outputFilename));

            FileUtils.deleteQuietly(new File(odtFilename));
            FileUtils.deleteQuietly(new File(outputFilename));
            return output;

        } else {
            final String odtFilename = tempDirFullPath + "/openofficeConversion-" + currentTimeMillis + ".odt";

            FileUtils.writeByteArrayToFile(new File(odtFilename), odtContent);

            final Process process = Runtime.getRuntime()
                    .exec(String.format(
                            "soffice --headless --convert-to %s -env:UserInstallation=file://%s --outdir %s %s",
                            mimeTypeAbbreviation, tempDirFullPath, tempDirFullPath, odtFilename));

            try {
                process.waitFor();
            } catch (InterruptedException e) {
            }

            process.destroy();

            final String outputFilename = tempDirFullPath + "/openofficeConversion-" + currentTimeMillis + "."
                    + mimeTypeAbbreviation;
            final byte[] output = FileUtils.readFileToByteArray(new File(outputFilename));
            FileUtils.deleteQuietly(new File(odtFilename));
            FileUtils.deleteQuietly(new File(outputFilename));
            return output;

        }
    } catch (final Throwable e) {
        throw new OpenofficeInProcessConversionException(e);
    }

}

From source file:com.sangupta.andruil.commands.checksum.Crc32.java

/**
 * @see com.sangupta.andruil.commands.AbstractAndruilCommand#execute(java.lang.String[])
 *///from w w w.  ja  va 2 s.c  o m
@Override
protected boolean processFile(File file) throws IOException {
    if (file.isDirectory()) {
        return true;
    }

    byte[] bytes = FileUtils.readFileToByteArray(file);
    CRC32 crc32 = new CRC32();
    crc32.update(bytes);
    System.out.println(Long.toHexString(crc32.getValue()) + " *" + file.getName());

    return true;
}

From source file:com.galenframework.storage.repository.LocalFileStorage.java

@Override
public byte[] readFile(String imagePath) {
    try {//from  w ww. ja  v  a  2  s .co m
        File file = new File(root.getPath() + File.separator + imagePath);
        if (file.exists()) {
            return FileUtils.readFileToByteArray(file);
        } else {
            throw new FileNotFoundException(file.getPath());
        }
    } catch (Exception ex) {
        throw new RuntimeException("Can't read file: " + imagePath, ex);
    }
}

From source file:com.igormaznitsa.zxpspritecorrector.files.SCRPlugin.java

@Override
public ReadResult readFrom(final File file, final int index) throws IOException {
    final byte[] wholeFile = FileUtils.readFileToByteArray(file);
    return new ReadResult(
            new ZXPolyData(new Info(file.getName(), '$', 16384, wholeFile.length, 0), this, wholeFile), null);
}

From source file:com.sangupta.andruil.commands.file.Combine.java

@Override
public void execute(String[] args) {
    if (args.length < 3) {
        System.out.println("Usage: combine <out-file> <file1> <file2> ...");
        return;/*from  w  w  w  .  j  a v  a  2  s  .  c om*/
    }

    File outFile = resolveFile(args[0]);
    if (outFile.exists() && outFile.isFile()) {
        System.out.println("Output file exists... exiting!");
        return;
    }

    try {
        for (int index = 1; index < args.length; index++) {
            File inFile = resolveFile(args[index]);
            if (!(inFile.exists() && inFile.isFile())) {
                continue;
            }

            byte[] bytes = FileUtils.readFileToByteArray(inFile);
            FileUtils.writeByteArrayToFile(outFile, bytes, true);
        }
    } catch (IOException e) {
        System.out.println("Unable to copy files: " + e.getMessage());
    }
}

From source file:edu.usc.pgroup.floe.filesys.FileInfo.java

/**
 * Read the entire contents of the file.
 */// w ww  . ja  v a2  s  . c om
public final void readFileContents() {
    try {
        contents = FileUtils.readFileToByteArray(new File(fileName));
    } catch (IOException e) {
        LOGGER.warn("Could not open file for read. Exception: {}", e);
    }
}

From source file:br.ufrn.fonoweb.service.ArquivoService.java

public byte[] openFile(String fileName) {
    byte[] result = null;
    try {/*from   w  ww  .  ja  va 2 s. com*/
        result = FileUtils.readFileToByteArray(new File(this.getDataStore().concat(fileName)));
    } catch (IOException ex) {
        Logger.getLogger(ArquivoService.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}

From source file:com.ehammer.jobs.DaMailMan.java

private void sendEmailAndUpdateState(List<EmailStatus> newRecordsForEmailing) {
    for (EmailStatus emailStatus : newRecordsForEmailing) {

        Flyer flyer = emailStatus.getFlyerId();
        Customer customer = emailStatus.getCustomerId();

        byte[] attachment = null;

        if (flyer.getFlyerAttachement() != null) {
            File file = new File(FrameworkUtil.getAttachementStoragePath() + "" + flyer.getFlyerAttachement());
            try {
                attachment = FileUtils.readFileToByteArray(file);
            } catch (IOException ex) {
                Logger.getLogger(DaMailMan.class.getName()).log(Level.SEVERE, null, ex);
            }//from  w  ww. j av  a 2  s .c  om

        }

        String result = Emailer.init().SendEmail(FrameworkUtil.loadSettings(),
                replaceAttributes(flyer.getHtmlContent(), customer), flyer.getName(), customer.getEmail(),
                attachment, flyer.getFlyerAttachement());

        if (result.startsWith("Error")) {
            emailStatus.setEmailStatus(EmailState.FAI.toString());

        } else {
            emailStatus.setEmailStatus(EmailState.SUC.toString());

        }

        emailStatus.setDescription(result);

    }
    EmailStatusDAO.instance().updateAll(newRecordsForEmailing);
}

From source file:de.unisb.cs.st.javalanche.mutation.testutil.TestUtil.java

public static List<Mutation> getMutations(File f, String className) throws IOException {
    byte[] bytes = FileUtils.readFileToByteArray(f);
    return getMutations(bytes, className);
}