You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Georg Henzler (Issue Comment Edited) (JIRA)" <ji...@apache.org> on 2011/10/19 00:55:10 UTC

[jira] [Issue Comment Edited] (IO-288) Supply a ReverseFileReader

    [ https://issues.apache.org/jira/browse/IO-288?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13130101#comment-13130101 ] 

Georg Henzler edited comment on IO-288 at 10/18/11 10:54 PM:
-------------------------------------------------------------

I will provide you with Unit tests if you're interested - I tested the class with a main class and different input files but it's easy to create Unit tests from the main class. 

Regarding your questions:
- The Tailer class listens to file changes (as the unix tail does) and notifies a provided Listener passing the added line. The ReverseFileReader starts at the last line of a file and moves towards the start of the file (ignoring added lines after it has been instantiated).
- We could subclass FileReader but I'm not sure how to implement e.g. read(char[] cbuf, int off, int len)... implementing this going backward would be hard. Mixing going forward and backward is probably not really intuitive. I would suggest that if we implement FileReader, we throw a UnsupportedOperationException for most of the Reader inferface's methods. 
- I'm not sure of the Filename... is BufferedReverseFileReader a better name to emphasize on the fact that it's all about the method readLine()? Any other name suggestions? 
 


                
      was (Author: henzlerg):
    I will provide you with Unit tests if you're interested - I tested the class with a main class and different input files but it's easy to make Unit tests from the main class. 

Regarding your questions:
- The Tailer class listens to file changes (as the unix tail does) and notifies a provided Listener passing the added line. The ReverseFileReader starts at the last line of a file and moves towards the start of the file (ignoring added lines after it has instantiated).
- We could subclass FileReader but I'm not sure how to implement e.g. read(char[] cbuf, int off, int len)... implementing this going backward would be hard. Mixing going forward and backward is probably not really intuitive. I would suggest that if we implement FileReader, we throw a UnsupportedOperationException for most of the Reader inferface's methods. 
- I'm not sure of the Filename... is BufferedReverseFileReader a better name to emphasize on the fact that it's all about the method readLine()? Any other name suggestions? 
 


                  
> Supply a ReverseFileReader
> --------------------------
>
>                 Key: IO-288
>                 URL: https://issues.apache.org/jira/browse/IO-288
>             Project: Commons IO
>          Issue Type: New Feature
>          Components: Utilities
>            Reporter: Georg Henzler
>         Attachments: ReverseFileReader.java
>
>
> I needed to analyse a log file today and I was looking for a ReverseFileReader: A class that behaves exactly like BufferedReader except that it goes from bottom to top when readLine() is called. I didn't find it in IOUtils and the internet didn't help a lot either, e.g. http://www.java2s.com/Tutorial/Java/0180__File/ReversingaFile.htm is a fairly inefficient - the log files I'm analysing are huge and it is not a good idea to load the whole content in the memory. 
> So I ended up writing an implementation myself using little memory and the class RandomAccessFile - see attached file. It's used as follows:
> int blockSize = 4096; // only that much memory is needed, no matter how big the file is
> ReverseFileReader reverseFileReader = new ReverseFileReader(myFile, blockSize, "UTF-8"); // encoding is supported
> String line = null;
> while((line=reverseFileReader.readLine())!=null) {
>   ... // use the line
>   if(enoughLinesSeen) {
>      break;  
>   }
> }
> reverseFileReader.close();
> I believe this could be useful for other people as well!

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira