Page 1 of 1

Get String content of the editor takes a long time

Posted: Fri Sep 09, 2016 6:44 pm
by Johann
Hi everyone,

I'm using the last oxygen SDK (18.0.0.3). I try to get the string content of my current page. My current page is a large XML file (about 700 k) and I want to get the string content during the edition of the document (before saving).

In order to do so, I used this code :

Code: Select all


File tempFile = File.createTempFile("tempfile", ".XML");
tempFile.deleteOnExit();
Reader reader = wsEditor.createContentReader();
String xmlContent = "";
int data = reader.read();
while (data != -1) {
char dataChar = (char) data;
xmlContent += dataChar;
data = reader.read();
}

FileUtils.writeStringToFile(tempFile, xmlContent);
The string is available (just before "writeStringToFile" method) after more than 30 seconds... Why is the reader so long to get this string content ?

Thanks for your help,

Johann

Re: Get String content of the editor takes a long time

Posted: Mon Sep 12, 2016 9:27 am
by Radu
Hi Johann,

As general rules:

1) When you read from a reader you should first wrap a buffered reader around it so that you can read larger chunks of chars.
2) You never add strings together, a string contains inside an array of characters like:

['h', 'e', 'l', 'l', 'o']

and it is immutable. So when to this string you add a space for example ' ', the virtual machine needs to create another array of characters which is one character larger:

['h', 'e', 'l', 'l', 'o', ' '].

So whenever you want to add characters you should use string builders.
The optimized code would need to look like this:

Code: Select all

    BufferedReader reader = new BufferedReader(wsEditor.createContentReader());
StringBuilder xmlContent = new StringBuilder();
int len = -1;
char[] buffer = new char[1024];
while ((len = reader.read(buffer)) != -1) {
xmlContent.append(buffer, 0, len);
}
String finalContent = xmlContent.toString();
If it still takes too long, the problem might be elsewhere.

Regards,
Radu

Re: Get String content of the editor takes a long time

Posted: Mon Sep 12, 2016 11:43 am
by Johann
Hi Radu,

Ohh, my bad...

Thanks !

Johann