/** writes safely (to temp file, then rename) */ public static byte[] saveBinaryFile(String fileName, byte[] contents) ctex { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; try (FileOutputStream fileOutputStream = newFileOutputStream(tempFileName)) { fileOutputStream.write(contents); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); ifndef LeanMode vmBus_send('wroteFile, file); endifndef ret contents; } static byte[] saveBinaryFile(File fileName, byte[] contents) { ret saveBinaryFile(fileName.getPath(), contents); }