This discussion is archived
1 2 Previous Next 19 Replies Latest reply: Aug 12, 2010 12:59 AM by 843790 RSS

java.lang.StackOverflowError reading to a variable

843790 Newbie
Currently Being Moderated
Hello,

Below is the piece of the code used to replace all matched pattern to empty. But when executed

1. FileInputStream fis = new FileInputStream(filePath);
2. FileChannel fc = fis.getChannel();
3. MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,(int) fc.size());
4. Charset cs = Charset.forName("UTF-8");
5. CharsetDecoder cd = cs.newDecoder();
6. CharBuffer cb = cd.decode(bb);
7. Matcher m = p.matcher(cb);
8. str = m.replaceAll("");

at line 8, there is a java.lang.StackOverflowError. In some cases only this error arises.
I have looked into http://forums.sun.com/thread.jspa?threadID=5268472
but I could not understand solution for my problem.

Any help is highly appreciated.

Thanks,
Mallikarjun.
  • 1. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    Please enter your code in the CODE tag above, like this
    System.out.print("example")
    A stack overflow is thrown when an application recurses too deeply. (Which normally means its in an infinite recursion).
    Link

    Can you post more of your code, so we can diagonise the problem. Or even better an SSCCE.
  • 2. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    Hi,
    I am trying to extract the un-commented part of the code available in the .js file through pattern matching regular expression. The sample code of the JavaScript is shown below. In many scenarios it is working fine, but for this file, it throws StackOverFlowError.

    sample .js file:
    /**
    * Loads an instance of a device service interface. This is a synchronous
    * function.
    *
    * @alias nokia.device.load
    *
    * @param {string} interfaceName A fully qualified name of the interface name.
    *                      E.g. A fully qualified name: nokia.device.contact
    *
    * @param {string} [version] A string representing the interface version number.
    *                Matches if the supported version is equal to or larger than
    *                the supplied version.e.g. 1.0
    *                If not specified the framework will load the default version
    * @example
    * // Loading the service defined by the Contact API:
    * try {
    *   var so = nokia.device.load("contacts");
    *   // use Contact API functions: so.addContact(), or so.getContact(), etc.
    * } catch (e) {
    *   // An error occurred. If e.name is "DeviceException", then the error orginates
    *   // in load(), the properties e.message and e.code contain more information.
    * }
    * @throws {DeviceException} May throw non-recoverable errors with error codes 1 through 3.
    * @return {ServiceInterface} An object that implements ServiceInterface and in addition contains the
    *                               functions defined in the API identified by +interfaceName+.
    */
    var so = abc.device.load("contacts");
    var so = abc.device.load("contacts");
    My Java code to get uncommented portion of file:
    String str = null;
    Pattern p = null;
    String regex;
    if ((filePath.toString().endsWith(".htm")) || (filePath.toString().endsWith(".html"))) {
    p = Pattern.compile("\\<!--[ \\r\\n\\t]-->",Pattern.MULTILINE);
    } else if (filePath.toString().endsWith(".js")) {
    p =Pattern.compile("(?:/\(?:|(?:\/))\/)|(?://.)", Pattern.MULTILINE);
    }
    if (p != null) {
    try {
    FileInputStream fis = new FileInputStream(filePath);
    FileChannel fc = fis.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,(int) fc.size());
    Charset cs = Charset.forName("UTF-8");
    CharsetDecoder cd = cs.newDecoder();
    CharBuffer cb = cd.decode(bb);
    Matcher m = p.matcher(cb);
    str = m.replaceAll(""); // at this point of time, I am getting the exception
    } catch (IOException e) {
    Activator.log(e);
    }
    }
    return str;
    Edited by: malli2010 on Aug 10, 2010 11:53 PM
  • 3. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    Please produce an SSCCE of the problem, so i can complie and test the program.
  • 4. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    jamesybaby wrote:
    Please produce an SSCCE of the problem, so i can complie and test the program.
    I don't really think an SSCCE is necessary since I think the problem is in the regular expression. Now I thought I understood regex but the one being proposed for using on the javascript file makes no sense at all to me. It is possible that the over use of non-capturing groups is obscuring the desired functionality but ....

    Edited by: sabre150 on Aug 11, 2010 10:12 AM

    Even accounting for a failure to escape any '\' chars I can't see how the regex can possibly achieve the desired objective.
  • 5. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    Any suggestions to solve the issue?

    by the way what is SSCCE?
  • 6. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    malli2010 wrote:
    Any suggestions to solve the issue?
    Instead of just guessing, spend some time learning about regular expressions. Take a long detailed look at [http://download.oracle.com/javase/tutorial/essential/regex/|http://download.oracle.com/javase/tutorial/essential/regex/] and [http://www.regular-expressions.info/|http://www.regular-expressions.info/].

    >
    by the way what is SSCCE?
    [http://pscode.org/sscce.html|http://pscode.org/sscce.html]
  • 7. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    I have learned and found the regex for the JS and HTML from the below link. pls have look
    http://ostermiller.org/findcomment.html
    http://ostermiller.org/findhtmlcomment.html
    -
    any inputs?
  • 8. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    malli2010 wrote:
    I have learned and found the regex for the JS and HTML from the below link. pls have look
    http://ostermiller.org/findcomment.html
    http://ostermiller.org/findhtmlcomment.html
    -
    any inputs?
    The first of those is used to find the comments in javascript but I don't see that your regular expression in any way resembles the ostermiller one.
  • 9. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    sabre150 wrote:
    jamesybaby wrote:
    Please produce an SSCCE of the problem, so i can complie and test the program.
    I don't really think an SSCCE is necessary since I think the problem is in the regular expression. Now I thought I understood regex but the one being proposed for using on the javascript file makes no sense at all to me. It is possible that the over use of non-capturing groups is obscuring the desired functionality but ....

    Edited by: sabre150 on Aug 11, 2010 10:12 AM

    Even accounting for a failure to escape any '\' chars I can't see how the regex can possibly achieve the desired objective.
    I thought that as well, but I wasn't sure because i've never seen any Javascript used like this before. Hence why I suggested an SSCCE. I also think that maybe if (p!=null), and that it was never reaching null that maybe it was eating up the memory, hence causing the stackoverflow at that line. But without testing it, I weren't that sure.
  • 10. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    Do you mean to say that "(?:/\(?:|(?:\/))\/)|(?://.)" is not a properly regular expression to find out commented lines in a file as suggested by ostermiller?

    Also, why 'p' has to eatup the memory since it is not in loop at all?
  • 11. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    Have a look, if p never equaled null then it would be in a loop forever. I can't tell you if that is the problem because I can't test it. I am most likely wrong, however that one of the things I would test.

    And read the links sabre give you, he is very knowledgeable.
  • 12. Re: java.lang.StackOverflowError reading to a variable
    EJP Guru
    Currently Being Moderated
    I would have expected the '*' character to feature in it somewhere. Without that it can't possibly be correct.
  • 13. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    here is the SSCCE,

    Test.java
    import java.io.FileInputStream;
    
    import java.io.IOException;
    
    import java.io.InputStream;
    
    import java.io.OutputStream;
    
    import java.nio.ByteBuffer;
    
    import java.nio.MappedByteBuffer;
    
    import java.nio.CharBuffer;
    
    import java.nio.channels.FileChannel;
    
    import java.nio.charset.Charset;
    
    import java.nio.charset.CharsetDecoder;
    
    import java.util.regex.Matcher;
    
    import java.util.regex.Pattern;
    
    
    
    class Test {
    
    public static void main(String args[]) {
    
    String str = null;
    
    Pattern p = null;
    
    String regex;
    
    String filePath = args[0];
    
    //System.out.println(filePath);
    
    if ((filePath.toString().endsWith(".htm")) || (filePath.toString().endsWith(".html"))) {
    
    p = Pattern.compile("\\<!--[ \\r\\n\\t]-->",Pattern.MULTILINE);
    
    } else if (filePath.toString().endsWith(".js")) {
    
    //p = Pattern.compile("(?:/\\(?:|(?:\\/))\\/)|(?://.)", Pattern.MULTILINE);
    
    p = Pattern.compile("(?:/\\(?:|(?:\\/))\\/)|(?://.*)", Pattern.MULTILINE);
    
    }
    
    if (p != null) {
    
    try {
    
    FileInputStream fis = new FileInputStream(filePath);
    
    FileChannel fc = fis.getChannel();
    
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,(int) fc.size());
    
    Charset cs = Charset.forName("UTF-8");
    
    CharsetDecoder cd = cs.newDecoder();
    
    CharBuffer cb = cd.decode(bb);
    
    Matcher m = p.matcher(cb); str = m.replaceAll(""); // at this point of time, I am getting the exception
    
    System.out.println(str); } catch (IOException e) {
    
    e.printStackTrace(); }
    
    }
    
    }
    
    }
    Also please tell me how to attach files here in this forum?
  • 14. Re: java.lang.StackOverflowError reading to a variable
    843790 Newbie
    Currently Being Moderated
    malli2010 wrote:
    Do you mean to say that "(?:/\(?:|(?:\/))\/)|(?://.)" is not a properly regular expression to find out commented lines in a file as suggested by ostermiller?
    I don't see that regex or anything resembling it posted by ostermiller on the sites you quote.

    >
    Also, why 'p' has to eatup the memory since it is not in loop at all?
    If is very very common for bad regex to get close to infinite recursion. I don't know why that one is bad but since it does not make sense I'm not going to analyse it.
1 2 Previous Next