r1739 - in trunk/src: main/java/org/nuiton/util test/java/org/nuiton/util test/resources test/resources/org test/resources/org/nuiton test/resources/org/nuiton/util
Author: echatellier Date: 2010-01-14 17:16:12 +0100 (Thu, 14 Jan 2010) New Revision: 1739 Added: trunk/src/main/java/org/nuiton/util/ReverseFileReader.java trunk/src/test/java/org/nuiton/util/ReverseFileReaderTest.java trunk/src/test/resources/org/ trunk/src/test/resources/org/nuiton/ trunk/src/test/resources/org/nuiton/util/ trunk/src/test/resources/org/nuiton/util/reverseread.txt Log: Add new reader to read file in reverse order. Added: trunk/src/main/java/org/nuiton/util/ReverseFileReader.java =================================================================== --- trunk/src/main/java/org/nuiton/util/ReverseFileReader.java (rev 0) +++ trunk/src/main/java/org/nuiton/util/ReverseFileReader.java 2010-01-14 16:16:12 UTC (rev 1739) @@ -0,0 +1,137 @@ +/* + * *##% Nuiton utilities library + * Copyright (C) 2004 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ + +package org.nuiton.util; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * Reverse file reader. + * + * @author chatellier + * @version $Revision$ + * + * TODO add an interface on it. + * + * Last update : $Date$ + * By : $Author$ + */ +public class ReverseFileReader implements Closeable { + protected String filename; + protected RandomAccessFile randomfile; + protected long position; + + public ReverseFileReader(File file) throws IOException { + // Open up a random access file + this.randomfile = new RandomAccessFile(file, "r"); + // Set our seek position to the end of the file + this.position = this.randomfile.length(); + + // Seek to the end of the file + this.randomfile.seek(this.position); + //Move our pointer to the first valid position at the end of the file. + String thisLine = this.randomfile.readLine(); + while (thisLine == null) { + this.position--; + this.randomfile.seek(this.position); + thisLine = this.randomfile.readLine(); + this.randomfile.seek(this.position); + } + } + + public ReverseFileReader(String filename) throws IOException { + this(filename != null ? new File(filename) : null); + } + + /** + * Read one line from the current position towards the beginning. + * + * @return the next line of text from this file, or null if end of file is encountered before even one byte is read. + * @throws IOException + */ + public String readLine() throws IOException { + int thisCode; + char thisChar; + String finalLine = ""; + + // If our position is less than zero already, we are at the beginning + // with nothing to return. + if (this.position < 0) { + return null; + } + + for (;;) { + // we've reached the beginning of the file + if (this.position < 0) { + break; + } + // Seek to the current position + this.randomfile.seek(this.position); + + // Read the data at this position + thisCode = this.randomfile.readByte(); + thisChar = (char) thisCode; + + // If this is a line break or carrige return, stop looking + if (thisCode == 13 || thisCode == 10) { + // See if the previous character is also a line break character. + // this accounts for crlf combinations + this.randomfile.seek(this.position - 1); + int nextCode = this.randomfile.readByte(); + if ((thisCode == 10 && nextCode == 13) + || (thisCode == 13 && nextCode == 10)) { + // If we found another linebreak character, ignore it + this.position = this.position - 1; + } + // Move the pointer for the next readline + this.position--; + break; + } else { + // This is a valid character append to the string + finalLine = thisChar + finalLine; + } + // Move to the next char + this.position--; + } + // return the line + return finalLine; + } + + /* + * @see java.io.Closeable#close() + */ + @Override + public void close() throws IOException { + if (randomfile != null) { + randomfile.close(); + } + } + + /* + * @see java.lang.Object#finalize() + */ + @Override + protected void finalize() throws Throwable { + close(); + super.finalize(); + } +} \ No newline at end of file Property changes on: trunk/src/main/java/org/nuiton/util/ReverseFileReader.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: trunk/src/test/java/org/nuiton/util/ReverseFileReaderTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/ReverseFileReaderTest.java (rev 0) +++ trunk/src/test/java/org/nuiton/util/ReverseFileReaderTest.java 2010-01-14 16:16:12 UTC (rev 1739) @@ -0,0 +1,62 @@ +/* + * *##% Nuiton utilities library + * Copyright (C) 2004 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* + */ + +package org.nuiton.util; + +import java.io.IOException; +import java.net.URL; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * Test for reverse reader utility. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class ReverseFileReaderTest { + + /** + * Test to read test file in reverse order. + * + * @throws IOException + */ + @Test + public void testReverseRead() throws IOException { + URL url = ReverseFileReaderTest.class.getResource("reverseread.txt"); + String reverseFile = url.getFile(); + ReverseFileReader reader = new ReverseFileReader(reverseFile); + + String lastRead = reader.readLine(); + Assert.assertEquals("Line 4", lastRead); + + lastRead = reader.readLine(); + Assert.assertEquals("Line 3", lastRead); + + lastRead = reader.readLine(); + Assert.assertEquals("Line 2", lastRead); + + reader.close(); + } +} Property changes on: trunk/src/test/java/org/nuiton/util/ReverseFileReaderTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: trunk/src/test/resources/org/nuiton/util/reverseread.txt =================================================================== --- trunk/src/test/resources/org/nuiton/util/reverseread.txt (rev 0) +++ trunk/src/test/resources/org/nuiton/util/reverseread.txt 2010-01-14 16:16:12 UTC (rev 1739) @@ -0,0 +1,4 @@ +Line 1 +Line 2 +Line 3 +Line 4 \ No newline at end of file
participants (1)
-
echatellier@users.nuiton.org