Q:

java.util.Scanner NoSuchElementException with LineFeed terminated file 1025 chars long

I can't figure out why I'm getting a NoSuchElementException. The file I'm reading is a LF terminated, and this code has been running in production for a year now. In the process of debugging my colleague discovered that you can reproduce the error when the file ends with a LF and is exactly 1025 chars long (though the original file is 10132 chars long). This error is reproducible on both Linux and Windows and I tried JDK 1.6.0_30, JRE 1.7 and JDK 1.7.0_07. If you change the Pattern to "~$?" or "~\r?\n?" the error is gone.
public static void main(String[] args) throws FileNotFoundException {
    String filepath = "file.txt";               
    Scanner s = null;
    try {
        s = new Scanner(new File(filepath));
        s.useDelimiter(Pattern.compile("~\n?", Pattern.UNIX_LINES));
        while (s.hasNext()) {
            System.out.println(s.next());
        }

    } finally {
        if (s != null)
            s.close();
    }
}

Any suggestions appreciated.
Thank you!
EDIT 1
I just tried the GNU Classpath implementation of Scanner and that version of Scanner works just fine. 

A:

So just to put a closure on this... If your scanner pattern ends with an optional new line \n? you might get NoSuchElementException. The problem is with the implementation of Scanner but there are easy workarounds: 

Scan for lines and apply your regex without the \n?
End your scan with (?:\r?\n?); This works because it forces the Scanner to pull more chars in the buffer skipping the fringe case. 
End your scan $?; This detects end of line but doesn't pick the caret return/new line.

