1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.api;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.Reader;
29 import java.io.StringReader;
30 import java.nio.charset.Charset;
31 import java.nio.charset.CharsetDecoder;
32 import java.nio.charset.CodingErrorAction;
33 import java.nio.charset.UnsupportedCharsetException;
34 import java.nio.file.Files;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40
41 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
42
43
44
45
46
47
48
49
50
51 public final class FileText {
52
53
54
55
56 private static final int READ_BUFFER_SIZE = 1024;
57
58
59
60
61 private static final Pattern LINE_TERMINATOR = Pattern.compile("\\n|\\r\\n?");
62
63
64
65
66
67
68
69
70
71
72 private final File file;
73
74
75
76
77
78 private final Charset charset;
79
80
81
82
83 private final String[] lines;
84
85
86
87
88
89
90
91 private String fullText;
92
93
94
95
96 private int[] lineBreaks;
97
98
99
100
101
102
103 public FileText(FileText fileText) {
104 file = fileText.file;
105 charset = fileText.charset;
106 fullText = fileText.fullText;
107 lines = fileText.lines.clone();
108 if (fileText.lineBreaks != null) {
109 lineBreaks = fileText.lineBreaks.clone();
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124 public FileText(File file, List<String> lines) {
125 final StringBuilder buf = new StringBuilder(1024);
126 for (final String line : lines) {
127 buf.append(line).append('\n');
128 }
129
130 this.file = file;
131 charset = null;
132 fullText = buf.toString();
133 this.lines = lines.toArray(CommonUtil.EMPTY_STRING_ARRAY);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 public FileText(File file, String charsetName) throws IOException {
150 this.file = file;
151
152
153
154 final CharsetDecoder decoder;
155 try {
156 charset = Charset.forName(charsetName);
157 decoder = charset.newDecoder();
158 decoder.onMalformedInput(CodingErrorAction.REPLACE);
159 decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
160 }
161 catch (final UnsupportedCharsetException ex) {
162 final String message = "Unsupported charset: " + charsetName;
163 throw new IllegalStateException(message, ex);
164 }
165
166 fullText = readFile(file, decoder);
167
168
169
170
171 try (BufferedReader reader = new BufferedReader(new StringReader(fullText))) {
172 final ArrayList<String> textLines = new ArrayList<>();
173 while (true) {
174 final String line = reader.readLine();
175 if (line == null) {
176 break;
177 }
178 textLines.add(line);
179 }
180 lines = textLines.toArray(CommonUtil.EMPTY_STRING_ARRAY);
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193 private static String readFile(final File inputFile, final CharsetDecoder decoder)
194 throws IOException {
195 if (!inputFile.exists()) {
196 throw new FileNotFoundException(inputFile.getPath() + " (No such file or directory)");
197 }
198 final StringBuilder buf = new StringBuilder(1024);
199 final InputStream stream = Files.newInputStream(inputFile.toPath());
200 try (Reader reader = new InputStreamReader(stream, decoder)) {
201 final char[] chars = new char[READ_BUFFER_SIZE];
202 while (true) {
203 final int len = reader.read(chars);
204 if (len == -1) {
205 break;
206 }
207 buf.append(chars, 0, len);
208 }
209 }
210 return buf.toString();
211 }
212
213
214
215
216
217
218
219
220 public String get(final int lineNo) {
221 return lines[lineNo];
222 }
223
224
225
226
227
228
229 public File getFile() {
230 return file;
231 }
232
233
234
235
236
237
238
239 public Charset getCharset() {
240 return charset;
241 }
242
243
244
245
246
247
248 public CharSequence getFullText() {
249 return fullText;
250 }
251
252
253
254
255
256
257
258
259 public String[] toLinesArray() {
260 return lines.clone();
261 }
262
263
264
265
266
267
268
269 public LineColumn lineColumn(int pos) {
270 final int[] lineBreakPositions = findLineBreaks();
271 int lineNo = Arrays.binarySearch(lineBreakPositions, pos);
272 if (lineNo < 0) {
273
274
275 lineNo = -lineNo - 2;
276 }
277 final int startOfLine = lineBreakPositions[lineNo];
278 final int columnNo = pos - startOfLine;
279
280 return new LineColumn(lineNo + 1, columnNo);
281 }
282
283
284
285
286
287
288 private int[] findLineBreaks() {
289 if (lineBreaks == null) {
290 final int[] lineBreakPositions = new int[size() + 1];
291 lineBreakPositions[0] = 0;
292 int lineNo = 1;
293 final Matcher matcher = LINE_TERMINATOR.matcher(fullText);
294 while (matcher.find()) {
295 lineBreakPositions[lineNo] = matcher.end();
296 lineNo++;
297 }
298 if (lineNo < lineBreakPositions.length) {
299 lineBreakPositions[lineNo] = fullText.length();
300 }
301 lineBreaks = lineBreakPositions;
302 }
303 return lineBreaks;
304 }
305
306
307
308
309
310
311 public int size() {
312 return lines.length;
313 }
314
315 }