I'm running into an issue that I hope someone can help with.
Any file that is greater than 2G before encryption (a S/MIME file (.p7m)) causes an out of memory error when trying to decrypt. I have tried increasing the JVM and still the same result. Interesting enough when I do set the heap space, the process seems to be grabbing more that what is set.
When tracing my code, it always appears to fail at the mimeMessage method.
Here is a snippet of my code:
// Create the decoder object
PKCS7DecodeStream decoder = null;
decoder = new PKCS7DecodeStream(loggedCredentialEntry.loggedInUser,
MimeMessage mimeMessage = new MimeMessage(null, decoder);
Below is the stack trace:
xception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.io.ByteArrayOutputStream.write(Unknown Source)
Edited by: user13502256 on Jan 20, 2011 7:29 AM
Since 2 gigs is a array limit size the limit would seem apparent to me.
Solutions would be break it into pieces, find a different decoder or figure out if the decoder you are using will deal with files (streams).
Thanks for your response jcshell.
What/How exactly do you mean break it into pieces? I came across another reading that stated the below but not clear on how to implement it.
".....major problem with using the MimeMessage(Session, InputStream) constructor. When you use this over 2x the size of the actual message worth of memory will be allocated which can be a major problem if you have large attachment sizes. The reason this is happening is down in the ASCIIUtility.getBytes(InputStream) method. What happens is it reads the inputstream fully into a ByteArrayOutputStream, which is fine. The problem then comes is that when you call .getBytes() on that output stream it creates a copy of the byte, instead of just returning the internal byte, thus it is allocating 2x the memory. To fix this you have two options, create your own OutputStream and give access to its internal byte, or you could subclass ByteArrayOutputStream and add an accessor to the buf byte array since its protected, and return that..."