简体   繁体   中英

Getting InputStream from OMElement

I tried following sample[1] ; but since my OMElement is too large, (I'm converting a file, (800MB) as OMelement , it is coming from another process) I face following issues,

  • Process goes out of memory
  • Serialize takes much time.

Can anyone point me right solution?

[1]

 BufferedReader in = null;
 ByteArrayOutputStream baos = null;
 InputStream is = null;
 try {

    baos = new ByteArrayOutputStream();
    fileContent.serialize(baos);

    is = new ByteArrayInputStream(baos.toByteArray());

    in = new BufferedReader(new InputStreamReader(is));

Unfortunately your question doesn't provide a clear description of the actual problem you are trying to solve. Instead it describes an issue with what you believe to be the solution to your problem. Therefore I can only try to reconstruct the problem based on the comments you made in response to Ian Roberts.

If my interpretation of these comments is correctly, then the problem is as follows. You have an XML document that contains an element with a long sequence of characters, which is structured into multiple lines:

<some_element>
line 1
line 2
line 3
...
line N
</some_element>

You want to process the content of the element line by line, but N is large, so that you need to find a memory efficient way to do that, ie an approach that avoids loading the entire content into memory.

The code snippet you have provided shows that you took a wrong direction when trying to solve that problem. The code serializes the OMElement representing some_element and then creates an InputStream / Reader from the serialized output. However, that would also contain the start and end tags for some_element , which is not what you want. Instead you are only interested in the content of the element. If you look at the OMElement interface, you can see that it actually defines a method that returns that content as a Reader . It is called getTextAsStream and the Javadoc explains how to use that method in such a way that the memory usage is O(1) instead of O(N).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM