File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Getting OutOfMemory Error while reading EXE file content and placing in JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting OutOfMemory Error while reading EXE file content and placing in JTextArea" Watch "Getting OutOfMemory Error while reading EXE file content and placing in JTextArea" New topic
Author

Getting OutOfMemory Error while reading EXE file content and placing in JTextArea

manasa manogna
Greenhorn

Joined: Mar 29, 2011
Posts: 22
Hi

I am trying to read the content of EXE file and convert that into HEX Representation. After getting the hex representation as string, I am trying to place that text into JTextArea. But I am getting OutOfMemory Error while attempting to do this. below is the code I am trying



In the above code, I am using readBytesFromFile method to read the content of the exe file and getHex to convert this byte[] into hex string representation. I am trying to place this string in JTextArea using JTextArea.setText() method. But I am getting the OutOfMemory Error. Below is the error I am receiving

File length: 4518720 Maximum Length: 2147483647

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at javax.swing.text.WrappedPlainView$WrappedLine.breakLines(WrappedPlainView.java:782)
at javax.swing.text.WrappedPlainView$WrappedLine.update(WrappedPlainView.java:717)
at javax.swing.text.WrappedPlainView$WrappedLine.insertUpdate(WrappedPlainView.java:708)
at javax.swing.text.WrappedPlainView.insertUpdate(WrappedPlainView.java:455)
at javax.swing.plaf.basic.BasicTextUI$RootView.insertUpdate(BasicTextUI.java:1590)
at javax.swing.plaf.basic.BasicTextUI$UpdateHandler.insertUpdate(BasicTextUI.java:1849)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:185)
at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:734)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:693)
at javax.swing.text.PlainDocument.insertString(PlainDocument.java:114)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:655)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1693)
at SupportFiles.EXEFile.createAndDisplayGUI(EXEFile.java:58)
at SupportFiles.EXEFile$1.run(EXEFile.java:33)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Can you please help me to solve this issue.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

at SupportFiles.EXEFile.createAndDisplayGUI(EXEFile.java:58)

So what do you have on line 58 of EXEFile.java?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
manasa manogna
Greenhorn

Joined: Mar 29, 2011
Posts: 22
Hi maneesh

At line 58 of EXEFile, I am calling the statement textArea.setText(getHex(getBytesFromFile(fileName))). Here textArea is an object of JTextArea and I have set the following properties for textArea.



Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Why don't you try doing the processing in chunks instead of reading the entire file all at once. That is read 4K from the file, convert the data into the StringBuilder, then append the builder to the textArea. Repeat until the entire file has been processed. This way you will need smaller amounts of work spaces. That is the buffer for the file will only be 4K and the StringBuilder will only be 8K. The Document of the textare will grow as more data is appended to it.
manasa manogna
Greenhorn

Joined: Mar 29, 2011
Posts: 22
Hi Rob

Thank you for the suggestion. Can you please help me how to achieve this with a piece of code please. I am trying to google the same but not getting satisfactory solution to achieve.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Here is an example that simply reads a file in 4K blocks and writes the data to another file. You would need to change the write() logic to convert the data to your StringBuilder and then update the text area.

manasa manogna
Greenhorn

Joined: Mar 29, 2011
Posts: 22
Hi Rob

Thank you very much for the example. It solved my problem. Thanks once again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting OutOfMemory Error while reading EXE file content and placing in JTextArea