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

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

Introduction

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

Prototype

public static void writeByteArrayToFile(File file, byte[] data) throws IOException 

Source Link

Document

Writes a byte array to a file creating the file if it does not exist.

Usage

From source file:fr.paris.lutece.plugins.plu.web.PluJspBean.java

/**
 * @param nIdAtome nIdAtome//from   w ww .  j  a va2s  .c om
 * @param atome atome
 * @param version version
 * @param fileTitle fileTitle
 * @param i i
 * @param order order
 * @param strNumVersion strNumVersion
 * @param file file
 * @throws IOException IOException
 */
private void updateFile(int nIdAtome, Atome atome, Version version, String[] fileTitle, int i, int order,
        String strNumVersion, File file, int nIdVersion) throws IOException {

    List<File> oldFileList = new ArrayList<File>();
    oldFileList = _fileServices.findByVersion(nIdVersion);

    if (!file.getTitle().equals(fileTitle[i])) {
        file.setTitle(fileTitle[i]);
    }

    file.setAtome(nIdAtome);
    file.setOrder(order);
    file.setVersion(version.getId());

    // Test if file name contains extension, if yes remove extension to file name
    int a = file.getName().lastIndexOf(".");
    String nameWithoutExt = file.getName();
    String nameExt = "";
    if (a > 0) {
        nameWithoutExt = file.getName().substring(0, a);
        nameExt = file.getName().substring(a);
    }

    a = nameWithoutExt.lastIndexOf("-V");
    if (a > 0) {
        if (!oldFileList.contains(file)) {
            nameWithoutExt = nameWithoutExt.substring(0, a) + strNumVersion;
        }

    } else {
        nameWithoutExt = nameWithoutExt + strNumVersion;
    }

    file.setName(nameWithoutExt + nameExt);

    if (file.getId() != 0) {
        _fileServices.update(file);
    } else {
        _fileServices.create(file);
        FileFilter fileFilter = new FileFilter();
        fileFilter.setName(file.getName());
        fileFilter.setTitle(file.getTitle());
        AtomeFilter atomeFilter = new AtomeFilter();
        atomeFilter.setId(atome.getId());

        List<File> fileCreate = _fileServices.findByFilter(fileFilter, atomeFilter);
        file.setId(fileCreate.get(fileCreate.size() - 1).getId());
    }

    java.io.File fileDest = new java.io.File(
            new java.io.File(AppPropertiesService.getProperty("plu.docs.path")),
            file.getId() + "_" + file.getName());

    if (!fileDest.exists()) {
        FileUtils.writeByteArrayToFile(fileDest, file.getFile());
    }
}

From source file:net.sourceforge.subsonic.controller.MediaFolderSettingsController.java

private void createKey(byte[] data, Map<String, Object> map) {

    try {/*from  w w  w  .j  a v a 2  s  .co m*/
        String keyFile = settingsService.getSpotifyKey();
        FileUtils.writeByteArrayToFile(new File(keyFile), data);

    } catch (Exception x) {
        LOG.warn("Failed to upload spotify key: " + x, x);
        map.put("error", x);
    }
}

From source file:net.sourceforge.subsonic.controller.SpotifyKeyUploadController.java

private void createKey(byte[] data, Map<String, Object> map) throws IOException {

    try {/* www. ja  v a2s  .  c o m*/
        String keyFile = settingsService.getSpotifyKey();
        FileUtils.writeByteArrayToFile(new File(keyFile), data);

    } catch (Exception x) {
        LOG.warn("Failed to upload spotify key: " + x, x);
        map.put("error", x);
    }
}

From source file:oracle.kv.sample.fileloader.FileLoader.java

/**
 * this method will be called if there is a bulk get call
 *//*from w ww.  j ava 2 s.  co m*/
private void getData() {

    System.out.println("Get File");
    final int maxConcurrentRequests = 9;
    final int batchResultsSize = 0;
    // Direction - UNORDERED - Iterate in no particular key order.
    // CONSISTENCY - NONE_REQUIRED - A consistency policy that lets a
    // transaction on a replica using this policy proceed regardless of the
    // state of the Replica relative to the Master.
    final TableIteratorOptions tio = new TableIteratorOptions(Direction.UNORDERED, Consistency.NONE_REQUIRED, 0,
            null, maxConcurrentRequests, batchResultsSize);

    // Create Primary key with the value passed as argument
    PrimaryKey myKey = table.createPrimaryKey();

    myKey.put("id", fileId);

    // Create the table iterator and pass te primary Key, Muti Row Options
    // and Table Iterator options.
    final TableIterator<Row> iterator = tableh.tableIterator(myKey, null, tio);

    // Now retrieve the records.
    try {
        while (iterator.hasNext()) {
            Row row = (Row) iterator.next();
            File newFile = new File(dir + "/" + fileId + ".pdf");
            try {
                FileUtils.writeByteArrayToFile(newFile, row.get("file").asBinary().get());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } finally {
        // iterator close
        if (iterator != null) {
            iterator.close();
        }
    }
}

From source file:org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.java

public void persistCompiledClasses(CompilationUnit compilationUnit) {
    JavaFileObjectRegistry registry = compilationUnit.getRegistry();
    File classOutputDirectory = compilationUnit.getOutputClassDirectory();
    if (!classOutputDirectory.exists()) {
        if (!classOutputDirectory.mkdirs())
            throw new IllegalStateException(
                    "Failed to create directory " + classOutputDirectory.getAbsolutePath());
    }/*from ww w .  ja v a 2s.c o  m*/
    for (JavaFileObject javaFileObject : registry.get(JavaFileObject.Kind.CLASS)) {
        String internalName = javaFileObject.getName().substring(1);
        File compiledClassFile = new File(classOutputDirectory, internalName);
        if (!compiledClassFile.getParentFile().exists()) {
            if (!compiledClassFile.getParentFile().mkdirs()) {
                throw new IllegalStateException(
                        "Failed to create directories " + compiledClassFile.getParent());
            }
        }
        try {
            FileUtils.writeByteArrayToFile(compiledClassFile,
                    JavaCodeFileObject.class.cast(javaFileObject).getByteCode());
        } catch (IOException e) {
            throw new IllegalStateException("Failed to write to file " + compiledClassFile, e);
        }
    }
}

From source file:org.ala.repository.RepositoryImpl.java

/**
 * Store this metadata in a file in the saem directory as the document
 * with the supplied document id./*from   w w  w . j a  v a 2s.  com*/
 * 
 * @param documentId
 * @param content
 * @param metadataFileName
 * @throws IOException
 */
public void storeMetadata(int documentId, byte[] content, String metadataFileName) throws IOException {

    Document doc = documentDao.getById(documentId);
    String directory = doc.getFilePath();

    String fullFilePath = directory + File.separator + metadataFileName;
    File file = new File(fullFilePath);
    if (file.exists()) {
        FileUtils.forceDelete(file);
    }

    FileUtils.writeByteArrayToFile(file, content);
}

From source file:org.ala.repository.RepositoryImpl.java

/**
 * Store the raw byte content for this document.
 * /*from w  w w .  j a  v  a  2 s. c o  m*/
 * @param directory
 * @param content
 * @return
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
private File saveContent(File directory, byte[] content, FileType fileType, String contentType)
        throws UnsupportedEncodingException, IOException {

    File file = getOutputFile(directory, fileType, contentType);
    FileUtils.writeByteArrayToFile(file, content);

    try {
        //generate a thumbnail if the mime type indicate an image
        if (MimeType.getImageMimeTypes().contains(contentType)) {
            GenerateThumbnails.generateThumbnail(file, fileType, contentType, true, false, false);
        }
    } catch (Exception e) {
        log.error("Problem generating a thumbail for " + file.getAbsolutePath() + " " + e.getMessage(), e);
    }
    return file;
}

From source file:org.alex73.osm.monitors.export.ExportOutput.java

/**
 *       ???./*  www  . j  a v  a2  s . c  o  m*/
 */
void save(GitClient git, MemoryStorage osm) {
    try {
        if (queue.isEmpty()) {
            if (file1.exists()) {
                //  
                file1.delete();
                git.remove(path1);
            }
            if (file2.exists()) {
                //  
                file2.delete();
                git.remove(path2);
            }
            oldchecksum = null;
        } else {
            byte[] data = export(osm);
            String ck = calcChecksum(data);
            // 
            if (!ck.equals(oldchecksum)) {
                FileUtils.writeByteArrayToFile(file1, data);
                git.add(path1);
                FileUtils.writeByteArrayToFile(file2, data);
                git.add(path2);
                oldchecksum = ck;
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException();
    }
}

From source file:org.alex73.osm.monitors.export.ReadChangesets.java

/**
 *  changeset  ID.// ww w .j av  a 2  s .c o m
 */
public static byte[] download(Changeset ch) throws Exception {
    File cache = new File(Env.readProperty("data.cache") + "/changesets/" + ch.getId() + ".xml");
    byte[] xml;
    if (cache.exists()) {
        xml = FileUtils.readFileToByteArray(cache);
    } else {
        xml = get("http://www.openstreetmap.org/api/0.6/changeset/" + ch.getId() + "/download");
        cache.getParentFile().mkdirs();
        FileUtils.writeByteArrayToFile(cache, xml);
    }
    return xml;
}

From source file:org.alex73.osm.monitors.export.ReadChangesets.java

/**
 *  osm  ?? changeset'.//from   w  w w .j  a va2 s.  co  m
 */
static List<Changeset> readSeq(long index) throws Exception {
    String s = new DecimalFormat("000000000").format(index);
    File cache = new File(Env.readProperty("data.cache") + "/changesets/" + s + ".osm.gz");
    byte[] xml;
    if (cache.exists()) {
        System.out.println("Read from cache " + cache);
        xml = FileUtils.readFileToByteArray(cache);
    } else {
        xml = get("http://planet.openstreetmap.org/replication/changesets/" + s.substring(0, 3) + "/"
                + s.substring(3, 6) + "/" + s.substring(6) + ".osm.gz");
        cache.getParentFile().mkdirs();
        FileUtils.writeByteArrayToFile(cache, xml);
    }
    if (xml.length == 0) {
        return new ArrayList<>();
    }
    Osm result = (Osm) CONTEXT.createUnmarshaller()
            .unmarshal(new GZIPInputStream(new ByteArrayInputStream(xml)));
    return result.getChangeset();
}