1 package org.paneris.util;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.FilterReader;
8 import java.io.IOException;
9
10 public class CRLFFtellReader extends FilterReader {
11
12 private long ftell = 0L;
13 private long markedFtell = 0L;
14
15
16
17
18
19 public CRLFFtellReader(BufferedReader in) {
20 super(in);
21 }
22
23
24
25
26
27 public CRLFFtellReader(File f) throws FileNotFoundException {
28 this(new BufferedReader(new FileReader(f)));
29 }
30
31
32
33
34
35 public long getFtell() {
36 return ftell;
37 }
38
39
40
41
42
43
44
45
46
47
48 public int read() throws IOException {
49 synchronized (lock) {
50 int c = super.read();
51 if (c != -1) ++ftell;
52 return c;
53 }
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public int read(char cbuf[], int off, int len) throws IOException {
70 synchronized (lock) {
71 if (len <= 0) return 0;
72
73 int n = super.read(cbuf, off, len);
74 if (n > 0) ftell += n;
75 return n;
76 }
77 }
78
79
80
81
82
83
84
85
86
87
88
89 public long skip(long n) throws IOException {
90 synchronized (lock) {
91 long skipped = super.skip(n);
92 ftell += skipped;
93 return skipped;
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public void mark(int readAheadLimit) throws IOException {
111 synchronized (lock) {
112 super.mark(readAheadLimit);
113 markedFtell = ftell;
114 }
115 }
116
117
118
119
120
121
122
123
124 public void reset() throws IOException {
125 synchronized (lock) {
126 super.reset();
127 ftell = markedFtell;
128 }
129 }
130
131
132
133
134
135
136
137
138
139
140 public String readLine() throws IOException {
141 synchronized (lock) {
142 String l = ((BufferedReader)in).readLine();
143 if (l != null) ftell += l.length() + 2;
144 return l;
145 }
146 }
147 }