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:br.com.topsys.cd.util.AssinarArquivo.java

public static void main(String args[]) throws Exception {

    InputStream assinatura = AssinarArquivo.assinarDocumento();

    byte[] assinatura_binario = IOUtils.toByteArray(assinatura);
    FileUtils.writeByteArrayToFile(
            new File(caminho + "assinados//" + "assinatura_detached_bytes_prontuario.p7s"), assinatura_binario);

}

From source file:edu.kit.dama.transfer.client.util.TestDataBuilder.java

/**
 * The entry point./*from   ww  w .ja v a 2s  .c  o m*/
 *
 * @param args Command line args.
 */
public static void main(String[] args) {
    try {
        FileUtils.forceMkdir(new File(targetDir));

        byte[] oneMegaByte = buildRandomData((int) FileUtils.ONE_KB);

        for (int i = 0; i < BASE_FILES; i++) {
            File f = new File(targetDir + i + ".bin");
            LOGGER.debug(" - Writing file {}", f);
            FileUtils.writeByteArrayToFile(f, oneMegaByte);
        }
        for (int j = 0; j < SUB_DIRS; j++) {
            String subdir = targetDir + "subDir" + j;
            for (int k = 0; k < SUB_SUB_DIRS; k++) {
                String subsubdir = subdir + "/subsubdir" + k;
                oneMegaByte = buildRandomData((int) FileUtils.ONE_KB);
                for (int i = 0; i < FILES_IN_SUB_SUB_DIRS; i++) {
                    File f = new File(subsubdir + File.separator + i + "-" + j + "-" + k + ".bin");
                    LOGGER.debug(" - Writing file {}", f);
                    FileUtils.writeByteArrayToFile(f, oneMegaByte);
                }
            }
        }
    } catch (IOException ioe) {
        LOGGER.error("Failed to build data", ioe);
    }
}

From source file:com.e2info.helloasm.HelloAsmApp.java

public static void main(String[] args) throws IOException {
    String name = "HelloAsm";
    int flag = ClassWriter.COMPUTE_MAXS;
    ClassWriter cw = new ClassWriter(flag);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, name, null, "java/lang/Object", null);

    cw.visitSource(name + ".java", null);

    {/*from   w w w. ja v  a2s.c  o  m*/
        MethodVisitor mv;
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(Opcodes.RETURN);
        // we need this call to take effect ClassWriter.COMPUTE_MAXS flag.
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    {
        MethodVisitor mv;
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null,
                null);
        mv.visitCode();
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("hello ASM");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
        mv.visitInsn(Opcodes.RETURN);
        // we need this call to take effect ClassWriter.COMPUTE_MAXS flag.
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    cw.visitEnd();

    // build binary
    byte[] bin = cw.toByteArray();

    // save asm trace for human readable
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        new ClassReader(bin).accept(new TraceClassVisitor(pw), 0);
        File f = new File(name + ".txt");
        FileUtils.writeStringToFile(f, sw.toString());
    }

    // save as calss file
    {
        File f = new File(name + ".class");
        FileUtils.writeByteArrayToFile(f, bin);
    }

}

From source file:com.jwm123.loggly.reporter.AppLauncher.java

public static void main(String args[]) throws Exception {
    try {/*w w w  .  java  2 s. com*/
        CommandLine cl = parseCLI(args);
        try {
            config = new Configuration();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("ERROR: Failed to read in persisted configuration.");
        }
        if (cl.hasOption("h")) {

            HelpFormatter help = new HelpFormatter();
            String jarName = AppLauncher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
            if (jarName.contains("/")) {
                jarName = jarName.substring(jarName.lastIndexOf("/") + 1);
            }
            help.printHelp("java -jar " + jarName + " [options]", opts);
        }
        if (cl.hasOption("c")) {
            config.update();
        }
        if (cl.hasOption("q")) {
            Client client = new Client(config);
            client.setQuery(cl.getOptionValue("q"));
            if (cl.hasOption("from")) {
                client.setFrom(cl.getOptionValue("from"));
            }
            if (cl.hasOption("to")) {
                client.setTo(cl.getOptionValue("to"));
            }
            List<Map<String, Object>> report = client.getReport();

            if (report != null) {
                List<Map<String, String>> reportContent = new ArrayList<Map<String, String>>();
                ReportGenerator generator = null;
                if (cl.hasOption("file")) {
                    generator = new ReportGenerator(new File(cl.getOptionValue("file")));
                }
                byte reportFile[] = null;

                if (cl.hasOption("g")) {
                    System.out.println("Search results: " + report.size());
                    Set<Object> values = new TreeSet<Object>();
                    Map<Object, Integer> counts = new HashMap<Object, Integer>();
                    for (String groupBy : cl.getOptionValues("g")) {
                        for (Map<String, Object> result : report) {
                            if (mapContains(result, groupBy)) {
                                Object value = mapGet(result, groupBy);
                                values.add(value);
                                if (counts.containsKey(value)) {
                                    counts.put(value, counts.get(value) + 1);
                                } else {
                                    counts.put(value, 1);
                                }
                            }
                        }
                        System.out.println("For key: " + groupBy);
                        for (Object value : values) {
                            System.out.println("  " + value + ": " + counts.get(value));
                        }
                    }
                    if (cl.hasOption("file")) {
                        Map<String, String> reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Month", MONTH_FORMAT.format(new Date()));
                        reportContent.add(reportAddition);
                        for (Object value : values) {
                            reportAddition = new LinkedHashMap<String, String>();
                            reportAddition.put(value.toString(), "" + counts.get(value));
                            reportContent.add(reportAddition);
                        }
                        reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Total", "" + report.size());
                        reportContent.add(reportAddition);
                    }
                } else {
                    System.out.println("The Search [" + cl.getOptionValue("q") + "] yielded " + report.size()
                            + " results.");
                    if (cl.hasOption("file")) {
                        Map<String, String> reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Month", MONTH_FORMAT.format(new Date()));
                        reportContent.add(reportAddition);
                        reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Count", "" + report.size());
                        reportContent.add(reportAddition);
                    }
                }
                if (cl.hasOption("file")) {
                    reportFile = generator.build(reportContent);
                    File reportFileObj = new File(cl.getOptionValue("file"));
                    FileUtils.writeByteArrayToFile(reportFileObj, reportFile);
                    if (cl.hasOption("e")) {
                        ReportMailer mailer = new ReportMailer(config, cl.getOptionValues("e"),
                                cl.getOptionValue("s"), reportFileObj.getName(), reportFile);
                        mailer.send();
                    }
                }
            }
        }

    } catch (IllegalArgumentException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}

From source file:com.miyue.util.Cryptos.java

public static void main(String[] args) throws Exception {
    String helloWorld = "skymobi-android-browser-statiscs-key";
    byte key[] = generateAesKey();
    File file = new File("/Users/feize/key");
    //      byte key[]=FileUtils.readFileToByteArray(file);
    FileUtils.writeByteArrayToFile(file, key);
    //[B@4139eeda
    System.out.println("key is:" + key);
    //      FileOutputStream fos = new FileOutputStream("/Users/feize/key"); 
    //      fos.write(key);
    byte encodeString[] = aesEncrypt(helloWorld.getBytes(), key);
    System.out.println("encodeString is:" + encodeString);
    String decodeString = aesDecrypt(encodeString, key);
    System.out.println("decodeString is:" + decodeString);
}

From source file:com.wfd.util.FileUtil.java

public static void writeToFile(InputStream in, String filePath) {
    try {/*from  w  w w  .ja  va 2  s. com*/
        byte[] data = IOUtils.toByteArray(in);
        FileUtils.writeByteArrayToFile(new File(filePath), data);
        IOUtils.closeQuietly(in);
    } catch (Exception e) {
        System.out.println("Failed to conver inputstream to an image in server.");
        e.printStackTrace();
    }
}

From source file:com.mirth.connect.util.AttachmentUtil.java

public static void writeToFile(String filePath, Attachment attachment, boolean binary) throws IOException {
    File file = new File(filePath);
    if (!file.canWrite()) {
        String dirName = file.getPath();
        int i = dirName.lastIndexOf(File.separator);
        if (i > -1) {
            dirName = dirName.substring(0, i);
            File dir = new File(dirName);
            dir.mkdirs();/*from  w  ww.ja  v  a  2 s . c  o m*/
        }
        file.createNewFile();
    }

    if (attachment != null && StringUtils.isNotEmpty(filePath)) {
        FileUtils.writeByteArrayToFile(file,
                binary ? Base64Util.decodeBase64(attachment.getContent()) : attachment.getContent());
    }
}

From source file:com.alu.e3.common.tools.BundleTools.java

public static void byteArray2File(byte[] inputByteArray, File outputFile) throws Exception {
    FileUtils.writeByteArrayToFile(outputFile, inputByteArray);
}

From source file:net.darkmist.alib.io.Spew.java

/** @deprecated Use {@link org.apache.commons.io.FileUtils#writeByteArrayToFile(java.io.File, byte[])} instead. */
@Deprecated/*from  ww w .ja  v  a 2 s  . c  o  m*/
public static void spew(File file, byte[] data) throws IOException {
    FileUtils.writeByteArrayToFile(file, data);
    /*
    OutputStream stream = new FileOutputStream(file);
            
    spew(stream, data);
    stream.close();
    */
}

From source file:com.googlecode.fightinglayoutbugs.helpers.FileHelper.java

public static void bytesToFile(@Nonnull byte[] bytes, @Nonnull File file) {
    checkNotNull(bytes, "Method parameter bytes must not be null.");
    FileHelper.createParentDirectoryIfNeeded(checkNotNull(file, "Method parameter file must not be null."));
    try {//from w w  w. j a  va 2s.  c  o  m
        FileUtils.writeByteArrayToFile(file, bytes);
    } catch (IOException e) {
        throw new RuntimeException(
                "Failed to write " + StringHelper.amountString(bytes.length, "byte") + " to file: " + file, e);
    }

}