KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > utils > NumberedLineReader


1 package SnowMailClient.utils;
2
3 import java.util.*;
4 import java.io.*;
5
6
7 /** the number is usually used a human-readable debug information
8     this also stores a cache of the last line.
9 */

10 public class NumberedLineReader extends BufferedReader
11 {
12   private int lineNumber = 0;
13   private String JavaDoc lastLine = null;
14   /** only for one line !
15   */

16   private boolean undo = false;
17
18
19   public NumberedLineReader(String JavaDoc cont)
20   {
21      super( new StringReader( cont ) );
22   } // Constructor
23

24
25   public final String JavaDoc readLine() throws IOException
26   {
27      if(undo)
28      {
29        undo = false;
30      }
31      else
32      {
33        lineNumber++;
34        lastLine = super.readLine();
35      }
36      return lastLine;
37   }
38
39   /** this undoes the read of the last line.
40    => the next readLine will return exactely the same line
41   */

42   public final void undoRead()
43   {
44     if(undo==true) throw new RuntimeException JavaDoc("Only one undo allowed !");
45     if(lastLine==null) throw new RuntimeException JavaDoc("No line to undo !");
46     this.undo = true;
47   }
48
49   public final String JavaDoc getLastLineCached() { return lastLine; }
50
51   public final int getLineNumber() { return lineNumber; }
52
53 } // NumberedLineReader
Popular Tags