Hi there,
I came upon the following situation when I used this method, I have a
OutOfMemory error.
When I do remove the usage of this method, everything is oke.
TextNavigation or TextSelection doesn't implement the Closeable interface so I
normally don't have to close them.
private void replace(String searchField, String data, OdfTextDocument document)
throws InvalidNavigationException {
TextNavigation search = new TextNavigation(searchField, document);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.getCurrentItem();
item.replaceWith(data);
}
}
To give a situation, I'm building PDF's(approx. 30k) from an ODT template
(already byte[] in code)
We need to inject some data into the template and then we save it to the DB.
Can you confirm this is a memory leak in your part or the issue is with me?
The usage is in this part :
/**
* Creates all the bulletins from the given templates, with data stored in
the extractor.
*
* @throws Exception => createPdf(Bulletin bulletin, String[] data) throws
Exception so can't be more specified here.
*/
public void createBulletins() throws Exception {
Map<Bulletin, byte[]> bulletins = Maps.newHashMap();
for (Entry<String, String[]> entry : extractor.getCsv().entrySet()) {
readedSize++;
Bulletin bulletin = createBulletin(entry.getValue());
bulletins.put(bulletin, createPdf(bulletin, entry.getValue()));
if (bulletins.size()>=SAVE_SIZE) {
LOGGER.info("Saved " + SAVE_SIZE + " to the DB");
bulletinService.saveBulletins(bulletins);
this.report.addReaded(SAVE_SIZE);
bulletins = Maps.newHashMap();
}
}
this.report.addReaded(bulletins.size());
bulletinService.saveBulletins(bulletins);
}
/**
*
* @param bulletin bulletin info what needs to be filled in
* @param data the data what must be injected into the template.
* @return the created bulletin.
* @throws Exception => OdfTextDocument.loadDocument throws raw exception
so can't be more specified here.
*/
private byte[] createPdf(Bulletin bulletin, String[] data) throws Exception
{
InputStream in = null;
OdfTextDocument document = null;
ByteArrayOutputStream output = null;
try {
if ("NL".equalsIgnoreCase(data[LANGUAGE])) {
Objects.requireNonNull(templateNL, "Nederlandstalige template
is niet aanwezig.");
Objects.requireNonNull(extractor.getBulletinTitle(),
"Nederlandstalige titel is niet aanwezig.");
in = new ByteArrayInputStream(templateNL);
bulletin.setTitle(extractor.getBulletinTitle());
} else if ("FR".equalsIgnoreCase(data[LANGUAGE])) {
Objects.requireNonNull(templateFR, "Franstalige template is
niet aanwezig.");
Objects.requireNonNull(extractor.getBulletinTitleFR(),
"Franstalige titel is niet aanwezig.");
in = new ByteArrayInputStream(templateFR);
bulletin.setTitle(extractor.getBulletinTitleFR());
} else {
throw new IOException("Kan niet vinden of het NL of FR moet
zijn. Is deze op de 3de entry in de csv?");
}
document = OdfTextDocument.loadDocument(in);
//TODO : FIX OutOfMEM issue
// replace("%name%", data[NAME], document);
// replace("%firstName%", data[FIRST_NAME], document);
// replace("%street%", data[STREET], document);
// replace("%city%", data[ZIP] + " " + data[CITY], document);
// replace("%birthDate%", data[BIRTH_DATE], document);
// replace("%formNr%", data[NUMBER], document);
// replace("%sendDate%", "//TODO : TO BE CONFIRMED", document);
// replace("%employedBy%", data[EMPLOYD_BY], document);
output = new ByteArrayOutputStream();
document.save(output);
PdfConverter.getInstance().convert(document, output, options);
output.flush();
return output.toByteArray();
} finally {
close(in);
close(document);
close(output);
}
}
Thx in advance.
Filip
The best way to learn anything on the Internet
isn't to post a question but instead to post the wrong answer.
No one wants to help the noob that can't figure it out,
but everyone wants to correct the guy who is wrong.
[cid:[email protected]]
Filip Cossaer
1SM
CC V&C
9-6321-10435
Intranet<http://intranet.mil.intra/sites/Portal/Pages/Default.aspx>
This e-mail and any attachments may contain sensitive and
privileged information. If you are not the intended recipient,
please notify the sender immediately by return e-mail,
delete this e-mail and destroy any copies.
Any dissemination or use of this information by a person other
than the intended recipient is unauthorized and may be illegal.