1

My app 'encrypts' files (amateurly)... I can't open the file it processes when it goes through the following code (until i close the app) How can i free the file properly?

SecureRandom random = new SecureRandom();
// file CAN be used
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// since RandomAccessFile, file can't be used.. it's ok
FileChannel channel = raf.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
random.setSeed(key);
byte[] bytes = new byte[(int) raf.length()];
raf.read(bytes);
byte[] randomBytes = new byte[(int) raf.length()];
random.nextBytes(randomBytes);
for (int i = 0; i < bytes.length; i++)
    buffer.put((byte) (bytes[i] + randomBytes[i]));
buffer.force();
channel.close();
raf.close();
// fails to free the file...
matias
  • 105
  • 1
  • 10
  • see https://stackoverflow.com/questions/25238110/how-to-properly-close-mappedbytebuffer – Vlad May 24 '22 at 13:14

0 Answers0