Get String content of the editor takes a long time

Post here questions and problems related to oXygen frameworks/document types.
Johann
Posts: 60

Get String content of the editor takes a long time

Fri Sep 09, 2016 6:44 pm

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: 5131

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

Mon Sep 12, 2016 9:27 am

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: 60

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

Mon Sep 12, 2016 11:43 am

Hi Radu,

Ohh, my bad...

Thanks !

Johann

Return to “SDK-API, Frameworks - Document Types”

Who is online

Users browsing this forum: No registered users and 1 guest