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:edu.xjtu.qxcamerabridge.LiveviewImageExtractor.java

private static void dumpToDisk(String dumpFolder, LiveviewPayload payload) {
    //        System.out.println("dumping");
    String targetFileName = "liveview_" + payload.timestamp + "_" + payload.sequenceNumber + ".jpg";
    try {//w  w w  . jav a2 s.c  o m
        FileUtils.writeByteArrayToFile(new File(dumpFolder, targetFileName), payload.jpegData);
    } catch (IOException ex) {
        Logger.getLogger(LiveviewImageExtractor.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:controllers.CarPropertyController.java

@RequestMapping("/updateFromXml")
public String updateFromXml(Map<String, Object> model,
        @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras) {
    if (file == null || file.isEmpty()) {
        ras.addFlashAttribute("error", "File not found");
    } else {//from   ww  w.j  a v  a 2  s. co  m
        File newFile = new File("/usr/local/etc/CarProperty");
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            FileUtils.writeByteArrayToFile(newFile, file.getBytes());
            carPropertyService.updateFromXml(newFile);
            ras.addFlashAttribute("error", carPropertyService.getResult().getErrors());
        } catch (Exception e) {
            ras.addFlashAttribute("error", "updateFromXml" + StringAdapter.getStackTraceException(e));
        }
        if (newFile.exists()) {
            newFile.delete();
        }
    }
    return "redirect:/CarProperty/show";
}

From source file:de.undercouch.gradle.tasks.download.DownloadTaskPluginTest.java

/**
 * Runs an embedded HTTP server and creates test files to serve
 * @throws Exception if the server could not be started
 *///w w  w.  j  a  v a 2s . c  om
@Before
public void setUp() throws Exception {
    //run server on any free port
    server = new Server(0);

    //serve resources from temporary folder
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setBaseResource(Resource.newResource(folder.getRoot().getAbsolutePath()));

    //echo X-* headers back in response body
    ContextHandler echoHeadersHandler = new ContextHandler("/echo-headers") {
        @Override
        public void handle(String target, HttpServletRequest request, HttpServletResponse response,
                int dispatch) throws IOException, ServletException {
            response.setStatus(200);
            PrintWriter rw = response.getWriter();
            rw.write("headers:\n");
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                if (name.startsWith("X-")) {
                    rw.write(String.format("  %s: %s\n", name, request.getHeader(name)));
                }
            }
            rw.close();
        }
    };

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resourceHandler, echoHeadersHandler, new DefaultHandler() });
    server.setHandler(handlers);

    server.start();

    //create temporary files
    contents = new byte[4096];
    contents2 = new byte[4096];
    for (int i = 0; i < contents.length; ++i) {
        contents[i] = (byte) (Math.random() * 255);
        contents2[i] = (byte) (Math.random() * 255);
    }

    File testFile = folder.newFile(TEST_FILE_NAME);
    FileUtils.writeByteArrayToFile(testFile, contents);
    File testFile2 = folder.newFile(TEST_FILE_NAME2);
    FileUtils.writeByteArrayToFile(testFile2, contents2);
}

From source file:com.cedarsoft.crypt.HashTest.java

License:asdf

@Test
public void testIt() throws NoSuchAlgorithmException, IOException {
    URL paris = getClass().getResource("/paris.jpg");
    assertNotNull(paris);/*from w ww  .j ava  2s  .  c  om*/

    assertEquals("fbd5f9b6c0fd2035c490e46be0bc3ec3",
            HashCalculator.calculate(Algorithm.MD5, paris).getValueAsHex());//value read using md5sum cmd line tool
    assertEquals("aa5371938c4190543bddcfc1193a247717feba06",
            HashCalculator.calculate(Algorithm.SHA1, paris).getValueAsHex());//value read using sha1sum cmd line tool

    assertEquals("aa5371938c4190543bddcfc1193a247717feba06",
            HashCalculator.calculate(Algorithm.SHA1, IOUtils.toByteArray(paris.openStream())).getValueAsHex());
    assertEquals("aa5371938c4190543bddcfc1193a247717feba06",
            HashCalculator.calculate(Algorithm.SHA1, paris.openStream()).getValueAsHex());

    File file = tmp.newFile("paris.jpg");
    FileUtils.writeByteArrayToFile(file, IOUtils.toByteArray(paris.openStream()));
    assertEquals("aa5371938c4190543bddcfc1193a247717feba06",
            HashCalculator.calculate(Algorithm.SHA1, file).getValueAsHex());
}

From source file:com.validation.manager.core.server.core.AttachmentServer.java

public File getAttachedFile(String dest) {
    File result;/*from  w w w . ja  va 2  s.co m*/
    try {
        result = new File(dest + File.separator + getFileName());
        result.deleteOnExit();
        FileUtils.writeByteArrayToFile(result, getFile());
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
        result = null;
    }
    return result;
}

From source file:controllers.ColorController.java

@RequestMapping("/updateFromXml")
public String updateFromXml(Map<String, Object> model,
        @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras) {
    if (file == null || file.isEmpty()) {
        ras.addFlashAttribute("error", "File not found");
    } else {/* w w  w .j a  v  a2 s  .c  o m*/
        File newFile = new File("/usr/local/etc/Color");
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            FileUtils.writeByteArrayToFile(newFile, file.getBytes());
            colorService.updateFromXml(newFile);
            ras.addFlashAttribute("error", colorService.getResult().getErrors());
        } catch (Exception e) {
            ras.addFlashAttribute("error", "updateFromXml" + e.getMessage());
        }
        if (newFile.exists()) {
            newFile.delete();
        }
    }
    return "redirect:/Color/show";
}

From source file:controllers.ColorGroupController.java

@RequestMapping("/updateFromXml")
public String updateFromXml(Map<String, Object> model,
        @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras) {
    if (file == null || file.isEmpty()) {
        ras.addFlashAttribute("error", "File not found");
    } else {//w  ww.j av a  2s  .c om
        File newFile = new File("/usr/local/etc/ColorGroup");
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            FileUtils.writeByteArrayToFile(newFile, file.getBytes());
            colorGroupService.updateFromXml(newFile);
            ras.addFlashAttribute("error", colorGroupService.getResult().getErrors());
        } catch (Exception e) {
            ras.addFlashAttribute("error", "updateFromXml" + e.getMessage());
        }
        if (newFile.exists()) {
            newFile.delete();
        }
    }
    return "redirect:/ColorGroup/show";
}

From source file:com.sangupta.clitools.file.RandomFile.java

private void createFile(int bytes, File file) {
    final int bufferSize = 2048;
    if (bytes < bufferSize) {
        byte[] data = new byte[bytes];
        fillRandomData(data);// w  w  w  .  j  av  a2 s. co  m
        try {
            FileUtils.writeByteArrayToFile(file, data);
        } catch (IOException e) {
            System.out.println("Unable to write file to disk.");
            e.printStackTrace();
        }

        return;
    }

    final long chunks = bytes / bufferSize;
    byte[] data = new byte[bufferSize];

    FileOutputStream fileStream = null;
    BufferedOutputStream stream = null;
    try {
        fileStream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fileStream);

        for (int ch = 0; ch < chunks; ch++) {
            if (!this.fastMode) {
                fillRandomData(data);
            }

            stream.write(data);
        }

        // last min chunk
        int pending = bytes % bufferSize;
        if (pending == 0) {
            return;
        }

        data = new byte[pending];
        fillRandomData(data);
        stream.write(data);
    } catch (FileNotFoundException e) {
        // this should never happen
    } catch (IOException e) {
        System.out.println("Unable to write file to disk.");
        e.printStackTrace();
        return;
    } finally {
        IOUtils.closeQuietly(stream);
        IOUtils.closeQuietly(fileStream);
    }
}

From source file:glluch.com.ontotaxoseeker.TestsGen.java

public void testtestConceptsCountResults() throws IOException {
    TaxoPath tp = new TaxoPath();
    PathsCount pc = tp.findPaths(DOC);//from w w w .  ja v  a2  s .  c  o m
    File target = new File("resources/test/testConceptsCountResults.bin");
    byte[] vs = SerializationUtils.serialize(pc);
    FileUtils.writeByteArrayToFile(target, vs);
}

From source file:com.smash.revolance.ui.explorer.Explorer.java

private File storeApplication(byte[] application) throws IOException {
    File file = File.createTempFile(UUID.randomUUID().toString(), "jar");
    FileUtils.writeByteArrayToFile(file, application);
    return file;/*www .  j  a v a 2s .  co  m*/
}