KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > LineNumberWriter


1 /*
2
3   Copyright (C) 2001 Laurent Martelli
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

18
19 package org.objectweb.jac.util;
20
21 import java.io.FilterWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * A writer which counts the numbers of lines that it writes.
27  *
28  * @see #getLines()
29  */

30
31 public class LineNumberWriter extends FilterWriter JavaDoc
32 {
33    // last read byte
34
int last = -1;
35    int lines = 1;
36
37    public LineNumberWriter(Writer JavaDoc out) {
38       super(out);
39    }
40
41    public void write(int b) throws IOException JavaDoc {
42       if ( (b == '\r' && last != '\n') ||
43            (b=='\n') ) {
44          lines++;
45       }
46       out.write(b);
47       last = b;
48    }
49
50    public void write(char[] b, int off, int len) throws IOException JavaDoc
51    {
52       while (len-- > 0) {
53          write(b[off++]);
54       }
55    }
56
57    public void write(String JavaDoc str, int off, int len) throws IOException JavaDoc
58    {
59       while (len-- > 0) {
60          write(str.charAt(off++));
61       }
62    }
63
64    /**
65     * Returns the number of lines written
66     */

67    public int getLines() {
68       return lines;
69    }
70 }
71
72
Popular Tags