I have a ByteArrayOutputStream
which holds a byte representation of an XML with 750MB size.
I need to convert it to String.
I wrote:
ByteArrayOutputStream xmlArchive = ...
String xmlAsString = xmlArchive.toString(UTF8);
However although I am using 4GB of heap size I get java.lang.OutOfMemoryError: Java heap space
What is wrong? How can I know which heap size to use? I am using JDK64 bit
UPDATE
I need it as String in order to remove all the characters before "<?xml"
Currently my code is:
String xmlAsString = xmlArchive.toString(UTF8);
int xmlBegin = xmlAsString.indexOf("<?xml");
if (xmlBegin >0){
return xmlAsString.substring(xmlBegin);
}
return xmlAsString;
I then convert it again to byte array.
UPDATED 2 The ByteArrayOutputStream is written like this:
HttpMethod method ..
InputStream response = method.getResponseBodyAsStream();
byte[] buf = new byte[5000];
while ( (len=response.read(buf)) != -1) {
output.write(buf, 0, len);
}
len
is from the header of the response Content-Length