r504 - in trunk/jrst/src: main/java/org/nuiton/jrst test/java/org/nuiton/jrst/bugs
Author: vbriand Date: 2010-11-05 11:36:34 +0100 (Fri, 05 Nov 2010) New Revision: 504 Url: http://nuiton.org/repositories/revision/jrst/504 Log: There is no infinite loop anymore when the file doesn't end with a \n (backslash n) Modified: trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java trunk/jrst/src/test/java/org/nuiton/jrst/bugs/InfiniteLoopTest.java Modified: trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java =================================================================== --- trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java 2010-11-04 15:55:25 UTC (rev 503) +++ trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java 2010-11-05 10:36:34 UTC (rev 504) @@ -92,6 +92,8 @@ protected int markChar; protected int readInMark; + + protected boolean noNlAtEOF = false; /** * @@ -154,7 +156,7 @@ } /** - * ensure that have number char available and not allready read + * ensure that have number char available and not already read * * @param number ? * @throws IOException @@ -164,8 +166,10 @@ if (needed > 0) { char[] cbuf = new char[needed + READ_AHEAD]; int read = in.read(cbuf); - for (int i = 0; i < read; i++) { - buffer.add(cbuf[i]); + if (read != -1) { + for (int i = 0; i < read; i++) { + buffer.add(cbuf[i]); + } } } } @@ -241,7 +245,7 @@ /** * read one char in buffer * - * @return the next car + * @return the next char * @throws IOException pour tout pb de lecture */ public int read() throws IOException { @@ -270,8 +274,14 @@ result.append((char) c); c = read(); } - if (c == -1 && result.length() == 0) { - return null; + noNlAtEOF = false; + if (c == -1 && result.length() >= 0) { + if (c == -1 && result.length() == 0) { + return null; + } else { + noNlAtEOF = true; + return result.toString(); + } } else { return result.toString(); } @@ -346,7 +356,7 @@ tmp = readLine(); } if (tmp != null) { - unread(tmp.length() + 1); // +1 for '\n' not in line + unread(tmp.length() + (!noNlAtEOF ? 1 : 0)); // +1 for '\n' not in line } return result.toArray(new String[result.size()]); } @@ -366,7 +376,7 @@ tmp = readLine(); } if (tmp != null) { - unread(tmp.length() + 1); // +1 for '\n' not in line + unread(tmp.length() + (!noNlAtEOF ? 1 : 0)); // +1 for '\n' not in line } return result.toArray(new String[result.size()]); } Modified: trunk/jrst/src/test/java/org/nuiton/jrst/bugs/InfiniteLoopTest.java =================================================================== --- trunk/jrst/src/test/java/org/nuiton/jrst/bugs/InfiniteLoopTest.java 2010-11-04 15:55:25 UTC (rev 503) +++ trunk/jrst/src/test/java/org/nuiton/jrst/bugs/InfiniteLoopTest.java 2010-11-05 10:36:34 UTC (rev 504) @@ -52,9 +52,8 @@ * plus de 5s. (bug #697). * * @throws Exception if any - * //FIXME tchemit 2010-10-06 Remove Ignore when workaround will be proposed */ - @Ignore + @Test(timeout = 5000) public void testInfiniteLoop() throws Exception { URL url = JRSTReaderTest.class.getResource("/bugs/infiniteLoop.rst");
participants (1)
-
vbriand@users.nuiton.org