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:com.wandisco.s3hdfs.rewrite.filter.S3HdfsTestUtil.java

File getFile(int size) throws IOException {
    byte[] data = new byte[size];
    for (int i = 0; i < size; i++) {
        data[i] = (byte) (i % 256);
    }/*from  w w  w  .j av a 2  s  .  c  o  m*/

    File file = new File("src/test/java/resources/test" + size + "File");
    if (file.exists()) {
        assertEquals(true, file.delete());
    }
    assertEquals(true, file.createNewFile());
    FileUtils.writeByteArrayToFile(file, data);
    return file;
}

From source file:com.camnter.patch.utils.classref.ClassReferenceListBuilder.java

private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName) || !refcname.equals(classBinaryName + CLASS_EXTENSION)) {
        return;/* w w w . j  av  a  2 s.c o  m*/
    }

    try {
        DirectClassFile classFile = path.getClass(currentName);
        classNames.add(currentName);
        File entryFile = new File(patchDir + "/" + currentName);
        entryFile.getParentFile().mkdirs();

        if (!entryFile.exists()) {
            entryFile.createNewFile();
            //                Iterable<ClassPathElement> elements = path.getElements();
            //                for (ClassPathElement element : elements) {
            //                    InputStream in = element.open(currentName);
            byte[] bytes = NuwaProcessor.referHackWhenInit(classFile.getBytes().makeDataInputStream());
            //                    System.out.println(classFile.getFilePath() + ",size:" + bytes.length);
            FileUtils.writeByteArrayToFile(entryFile, bytes);
            //                }
        }
        //            NuwaProcessor.referHackWhenInit();

        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }

        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.xirp.mail.MailManager.java

/**
 * Transports the given {@link de.xirp.mail.Mail} via
 * a SMTP server.//w  ww.  jav  a2  s .c om
 * 
 * @param mail
 *            The mail to transport.
 * @throws EmailException
 *             if something is wrong with the given mail or the
 *             mail settings.
 * @see de.xirp.mail.Mail
 * @see de.xirp.mail.Contact
 * @see de.xirp.mail.Attachment
 */
private static void transport(Mail mail) throws EmailException {
    // Create the email message
    MultiPartEmail email = new MultiPartEmail();
    email.setHostName(PropertiesManager.getSmtpHost());
    email.setSmtpPort(PropertiesManager.getSmtpPort());

    if (PropertiesManager.isNeedsAuthentication()) {
        email.setAuthentication(PropertiesManager.getSmtpUser(),
                Util.decrypt(PropertiesManager.getSmtpPassword()));
    }

    for (Contact c : mail.getTo()) {
        email.addTo(c.getMail(), c.getFirstName() + " " + c.getLastName()); //$NON-NLS-1$
    }
    for (Contact c : mail.getCc()) {
        email.addCc(c.getMail(), c.getFirstName() + " " + c.getLastName()); //$NON-NLS-1$
    }

    email.setFrom(PropertiesManager.getNoReplyAddress(), Constants.APP_NAME);
    email.setSubject(mail.getSubject());
    email.setMsg(mail.getText() + I18n.getString(I18n.getString("MailManager.mail.footer", //$NON-NLS-1$
            Constants.LINE_SEPARATOR, Constants.BASE_NAME_MAJOR_VERSION)));

    // Create attachment
    for (Attachment a : mail.getAttachments()) {
        File file = a.getFile();
        EmailAttachment attachment = new EmailAttachment();

        if (file != null) {
            attachment.setPath(file.getPath());
        } else {
            File tmpFile = null;
            try {
                tmpFile = new File(Constants.TMP_DIR, a.getFileName());
                FileUtils.writeByteArrayToFile(tmpFile, a.getAttachmentFileContent());
                attachment.setPath(tmpFile.getPath());
            } catch (IOException e) {
                tmpFile = null;
            }
        }
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription(a.getFileName());
        attachment.setName(a.getFileName());
        email.attach(attachment);
    }

    try {
        email.send();
    } catch (EmailException e) {
        logClass.error("Error: " + e.getMessage() + Constants.LINE_SEPARATOR, e); //$NON-NLS-1$
        throw e;
    }
}

From source file:algorithm.ImageImageFrameExpanding.java

/**
 * Append restoration metadata. Size of carrier and payload files is added.
 * /*from  w  ww  .  jav  a  2 s. co  m*/
 * @param carrier
 * @param payload
 * @return metadata image
 * @throws IOException
 */
protected BufferedImage getMetadataImage(File carrier, File payload) throws IOException {
    BufferedImage carrierBuffered = ImageIO.read(carrier);
    BufferedImage payloadBuffered = ImageIO.read(payload);
    BufferedImage metadataImage = new BufferedImage(
            Math.max(carrierBuffered.getWidth(), payloadBuffered.getWidth()), METADATA_HEIGHT,
            carrierBuffered.getType());
    colorizeImage(metadataImage, Color.blue.getRGB());
    File metadataImageFile = new File(OUTPUT_DIRECTORY + "metadataImage.png");
    writeImage(metadataImageFile, metadataImage);
    File metadataFile = new File(OUTPUT_DIRECTORY + "tmpMetadataText.txt");
    tmpFiles.add(metadataFile);
    PayloadSegment payloadSegment = new PayloadSegment(carrier, payload, this);
    // add height and width of carrier and payload:
    payloadSegment.addOptionalProperty("carrierWidth", "" + carrierBuffered.getWidth());
    payloadSegment.addOptionalProperty("carrierHeight", "" + carrierBuffered.getHeight());
    payloadSegment.addOptionalProperty("payloadWidth", "" + payloadBuffered.getWidth());
    payloadSegment.addOptionalProperty("payloadHeight", "" + payloadBuffered.getHeight());
    byte[] metadata = payloadSegment.getRestorationMetadataBytes();
    FileUtils.writeByteArrayToFile(metadataFile, metadata);
    OpenStegoRandomLSBSteganography lsbAlgorithm = new OpenStegoRandomLSBSteganography();
    File tmpOutputFile = lsbAlgorithm.encapsulate(metadataImageFile, metadataFile);
    tmpFiles.add(tmpOutputFile);
    metadataImage = ImageIO.read(tmpOutputFile);
    return metadataImage;
}

From source file:io.wcm.devops.conga.tooling.maven.plugin.DefinitionPackageMojo.java

private void copyDefinitions(ResourceCollection sourceDir, File rootOutputDir, File parentTargetDir,
        String dirName) throws IOException {
    if (!sourceDir.exists()) {
        return;//from  w w  w. j ava2s  .  c om
    }
    SortedSet<Resource> files = sourceDir.getResources();
    SortedSet<ResourceCollection> dirs = sourceDir.getResourceCollections();
    if (files.isEmpty() && dirs.isEmpty()) {
        return;
    }

    File targetDir = new File(parentTargetDir, dirName);
    if (!targetDir.exists()) {
        targetDir.mkdirs();
    }

    for (Resource file : files) {
        File targetFile = new File(targetDir, file.getName());

        getLog().info("Include " + getPathForLog(rootOutputDir, targetFile));

        if (targetFile.exists()) {
            targetFile.delete();
        }
        try (InputStream is = file.getInputStream()) {
            byte[] data = IOUtils.toByteArray(is);
            FileUtils.writeByteArrayToFile(targetFile, data);
        }
    }

    for (ResourceCollection dir : dirs) {
        copyDefinitions(dir, rootOutputDir, targetDir, dir.getName());
    }
}

From source file:info.mikaelsvensson.devtools.sitesearch.SiteSearchPlugin.java

private void saveResourceAsFile(final String resourceName, final File file) {
    try {//from  w ww. ja  v  a2 s .c om
        InputStream stream = getClass().getClassLoader().getResourceAsStream(resourceName);
        byte[] bytes = IOUtils.toByteArray(stream);
        FileUtils.writeByteArrayToFile(file, bytes);
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
}

From source file:controllers.SequenceController.java

@RequestMapping("/updateFromXml")
public String updateFromXml(Map<String, Object> model,
        @RequestParam(value = "xlsFile", required = false) MultipartFile file,
        @RequestParam(value = "sequenceId", required = true) Long sequenceId, RedirectAttributes ras) {
    if (file == null || file.isEmpty()) {
        ras.addFlashAttribute("error", "File not found");
    } else {/*from   w  ww . ja v  a2  s .  c  om*/
        File newFile = new File("/usr/local/etc/sceneParamsList");
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            FileUtils.writeByteArrayToFile(newFile, file.getBytes());
            sequenceService.updateFromXml(newFile, sequenceId);
            ras.addFlashAttribute("error", sequenceService.getResult().getErrors());
        } catch (Exception e) {
            ras.addFlashAttribute("error",
                    "updateFromXml" + StringAdapter.getStackTraceException(e)/*e.getMessage()*/);
        }
        if (newFile.exists()) {
            newFile.delete();
        }
    }
    ras.addAttribute("sequenceId", sequenceId);
    return "redirect:/Sequence/showOne";
}

From source file:com.thruzero.domain.dsc.fs.FileDataStoreContainer.java

/**
 * Update an existing data file with the given fileData.
 * /*from w w  w. j a  v a  2 s .co  m*/
 * @throws DAOException
 *           if nonexistent.
 */
@Override
public void updateEntity(String fileName, DataStoreEntity fileData) {
    File fileToWrite = getFileFor(fileName);

    if (fileToWrite.exists()) {
        byte[] dataAsBytes;
        try {
            dataAsBytes = IOUtils.toByteArray(fileData.getData());
        } catch (IOException e1) {
            throw new DAOException("ERROR: Can't read fileData (to byte array).");
        }

        try {
            FileUtils.writeByteArrayToFile(fileToWrite, dataAsBytes);
        } catch (IOException e) {
            throw new DAOException("Error writing to file: " + fileToWrite.getAbsolutePath(), e);
        }
    } else {
        throw new DAOException(
                "Error - Can't write to file that doesn't exist: " + fileToWrite.getAbsolutePath());
    }
}

From source file:be.fedict.eid.pkira.xkmsws.util.XMLMarshallingUtil.java

public void writeDocumentToFile(byte[] responseMessage, String prefix, String suffix) {
    String fileName = createFileName(prefix, suffix);
    try {//from  ww  w.  j  av a2s .co m
        FileUtils.writeByteArrayToFile(new File(fileName), responseMessage);
    } catch (IOException e) {
        System.err.println("Error writing XML document.");
        e.printStackTrace();
    }
}

From source file:algorithm.OpenStegoRandomLSBSteganography.java

private String getPayload(File carrier, File payload) throws IOException {
    PayloadSegment payloadSegment = new PayloadSegment(carrier, payload, this);
    File payloadSemgentFile = new File("tmp");
    FileUtils.writeByteArrayToFile(payloadSemgentFile, payloadSegment.getPayloadSegmentBytes());
    return "" + payloadSemgentFile.toPath();
}