I am developing a web application where I need to encrypt a big file approx 500mb to an image. First time the code works fine but after that my server gives an error java.lang.OutOfMemoryError: Java heap space. I am using netbeans and glassfish server. I have also increased the heap size.
byte j[] = key.getBytes();
SecretKeySpec kye = new SecretKeySpec(j, "AES");
Cipher enc = Cipher.getInstance("AES");
enc.init(Cipher.ENCRYPT_MODE, kye);
FileOutputStream output = new FileOutputStream("xyz.mkv");
CipherOutputStream cos = new CipherOutputStream(output, enc);
byte[] buf = new byte[104857600];
int read;
while ((read = file.read(buf)) != -1) {
cos.write(buf, 0, read);
}
output.flush();
buf = null;
file.close();
cos.close();
I don't know what's going wrong.Please help. here is the stack trace..
Warning: StandardWrapperValve[DocEncrypt]: Servlet.service() for
servlet DocEncrypt threw exception
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236)
at com.sun.crypto.provider.CipherCore.update(CipherCore.java:666)
at com.sun.crypto.provider.AESCipher.engineUpdate(AESCipher.java:371)
at javax.crypto.Cipher.update(Cipher.java:1832)
at javax.crypto.CipherOutputStream.write(CipherOutputStream.java:158)