Created
May 24, 2018 06:07
-
-
Save kolipass/92a4d0f335fb41650fe90cb7ed5a8527 to your computer and use it in GitHub Desktop.
Binary File Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public byte[] generateFile(File file, long size) throws IOException, NoSuchAlgorithmException { | |
Random random = new Random(); | |
byte[] data = new byte[1024]; | |
if (!file.exists()) { | |
file.createNewFile(); | |
} | |
try (FileOutputStream outStream = new FileOutputStream(file)) { | |
for (int j = 0; j < size / 1024; j++) { | |
random.nextBytes(data); | |
outStream.write(data); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return getMd5(file); | |
} | |
private byte[] getMd5(File file) throws NoSuchAlgorithmException, IOException { | |
InputStream fis = new FileInputStream(file); | |
byte[] buffer = new byte[1024]; | |
MessageDigest complete = MessageDigest.getInstance("MD5"); | |
int numRead; | |
do { | |
numRead = fis.read(buffer); | |
if (numRead > 0) { | |
complete.update(buffer, 0, numRead); | |
} | |
} while (numRead != -1); | |
fis.close(); | |
return complete.digest(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment