Get String content of the editor takes a long time

Post here questions and problems related to oXygen frameworks/document types.
Johann
Posts: 199
Joined: Wed Jun 17, 2015 12:46 pm

Get String content of the editor takes a long time

Post 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
Radu
Posts: 9057
Joined: Fri Jul 09, 2004 5:18 pm

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

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Johann
Posts: 199
Joined: Wed Jun 17, 2015 12:46 pm

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

Post by Johann »

Hi Radu,

Ohh, my bad...

Thanks !

Johann
Post Reply