1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 */
19 package org.apache.rat.header;
20
21 import org.apache.rat.DeprecationReporter;
22
23 import java.io.IOException;
24 import java.io.Reader;
25
26 /** Replacement for {@link java.io.LineNumberReader}. This class
27 * provides a workaround for an incompatibility in the
28 * {@link java.io.LineNumberReader}: If the last line in a file
29 * isn't terminated with LF, or CR, or CRLF, then that line
30 * is counted in Java 16, and beyond, but wasn't counted before.
31 * This implementation is compatible with the latter variant,
32 * thus providing upwards compatibility for RAT.
33 */
34 @Deprecated // since 0.17
35 @DeprecationReporter.Info(since = "0.17", forRemoval = true)
36 public class LineNumberReader {
37 private final Reader parent;
38 private boolean previousCharWasCR = false;
39 private int lineNumber = 0;
40
41 public LineNumberReader(Reader pReader) {
42 DeprecationReporter.logDeprecated(this.getClass());
43 parent = pReader;
44 }
45
46 public int read() throws IOException {
47 final int c = parent.read();
48 switch(c) {
49 case 13:
50 previousCharWasCR = true;
51 ++lineNumber;
52 break;
53 case 10:
54 if (!previousCharWasCR) {
55 ++lineNumber;
56 }
57 previousCharWasCR = false;
58 break;
59 default:
60 previousCharWasCR = false;
61 break;
62 }
63 return c;
64 }
65
66 public int getLineNumber() {
67 return lineNumber;
68 }
69 }