r505 - in trunk/jrst/src: main/java/org/nuiton/jrst test/java/org/nuiton/jrst
Author: vbriand Date: 2010-11-12 17:24:27 +0100 (Fri, 12 Nov 2010) New Revision: 505 Url: http://nuiton.org/repositories/revision/jrst/505 Log: The files ending with \r\n and \r are now managed correctly. The temporary directory used by createTempFile in AdvancedReaderTest.java is now created before the tests so they don't fail anymore. Modified: trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java trunk/jrst/src/test/java/org/nuiton/jrst/AdvancedReaderTest.java Modified: trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java =================================================================== --- trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java 2010-11-05 10:36:34 UTC (rev 504) +++ trunk/jrst/src/main/java/org/nuiton/jrst/AdvancedReader.java 2010-11-12 16:24:27 UTC (rev 505) @@ -93,6 +93,7 @@ protected int readInMark; + protected boolean nlTwoCtrlChars = false; protected boolean noNlAtEOF = false; /** @@ -198,11 +199,11 @@ */ public int unread(int number) { int result = Math.min(number, nextChar); + nextChar -= result; - charNumber -= result; for (int i = nextChar; i < nextChar + result; i++) { - if (buffer.get(i) == '\n') { + if (buffer.get(i) == '\n' || (buffer.get(i) == '\r' && i + 1 < nextChar + result && buffer.get(i + 1) != '\n')) { lineNumber--; } } @@ -220,7 +221,7 @@ * @return number of unread char */ public int unread(String line, boolean addNewLine) { - int result = unread(line.length() + (addNewLine ? 1 : 0)); + int result = unread(line.length() + (addNewLine ? (nlTwoCtrlChars ? 2 : 1) : 0)); return result; } @@ -254,7 +255,7 @@ if (nextChar < buffer.size()) { result = buffer.get(nextChar++); charNumber++; - if (result == '\n') { + if ((char)result == '\n' || ((char)result == '\r' && nextChar < buffer.size() && buffer.get(nextChar) != '\n')) { lineNumber++; } } @@ -270,10 +271,19 @@ public String readLine() throws IOException { StringBuffer result = new StringBuffer(READ_AHEAD); int c = read(); - while (c != -1 && c != '\n') { + while (c != -1 && c != '\n' && c != '\r') { result.append((char) c); c = read(); } + nlTwoCtrlChars = false; + if (c == '\r') { + c = read(); + if (c != '\n' && c != -1) { + unread(1); + } else { + nlTwoCtrlChars = true; + } + } noNlAtEOF = false; if (c == -1 && result.length() >= 0) { if (c == -1 && result.length() == 0) { @@ -356,7 +366,7 @@ tmp = readLine(); } if (tmp != null) { - unread(tmp.length() + (!noNlAtEOF ? 1 : 0)); // +1 for '\n' not in line + unread(tmp.length() + (!noNlAtEOF ? (nlTwoCtrlChars ? 2 : 1) : 0)); // +1 for '\n' not in line } return result.toArray(new String[result.size()]); } @@ -376,7 +386,7 @@ tmp = readLine(); } if (tmp != null) { - unread(tmp.length() + (!noNlAtEOF ? 1 : 0)); // +1 for '\n' not in line + unread(tmp.length() + (!noNlAtEOF ? (nlTwoCtrlChars ? 2 : 1) : 0)); // +1 for '\n' not in line } return result.toArray(new String[result.size()]); } Modified: trunk/jrst/src/test/java/org/nuiton/jrst/AdvancedReaderTest.java =================================================================== --- trunk/jrst/src/test/java/org/nuiton/jrst/AdvancedReaderTest.java 2010-11-05 10:36:34 UTC (rev 504) +++ trunk/jrst/src/test/java/org/nuiton/jrst/AdvancedReaderTest.java 2010-11-12 16:24:27 UTC (rev 505) @@ -35,6 +35,7 @@ import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; /** @@ -57,6 +58,15 @@ + "9 toto tata tutu\n"; protected File file; + + /** + * Creates the temporary directory used by createTempFile + */ + @BeforeClass + static public void createTmpDir() { + File tmp = new File(System.getProperty("java.io.tmpdir")); + tmp.mkdirs(); + } /** * setUp - create test file. @@ -64,12 +74,12 @@ */ @Before public void setUp() throws IOException { - file = File.createTempFile("test-AdvancedReader", ".txt"); - file.deleteOnExit(); + file = File.createTempFile("test-AdvancedReader", ".txt"); + file.deleteOnExit(); - Writer out = new BufferedWriter(new FileWriter(file)); - out.write(text); - out.close(); + Writer out = new BufferedWriter(new FileWriter(file)); + out.write(text); + out.close(); } /** @@ -297,5 +307,19 @@ line = in.readLine(); Assert.assertNull(line); } - + + @Test + public void testEol() throws IOException { + String tmp = text; + AdvancedReader in = new AdvancedReader(new StringReader(tmp)); + String[] linesN = in.readAll(); + tmp = tmp.replace('\n', '\r'); + in = new AdvancedReader(new StringReader(tmp)); + String[] linesR = in.readAll(); + tmp = tmp.replaceAll("\r", "\r\n"); + in = new AdvancedReader(new StringReader(tmp)); + String[] linesRN = in.readAll(); + Assert.assertArrayEquals(linesN, linesR); + Assert.assertArrayEquals(linesN, linesRN); + } }
participants (1)
-
vbriand@users.nuiton.org