001 /*
002 * $Id: LineReader.java,v 1.3 2013/11/14 21:47:29 oboehm Exp $
003 *
004 * Copyright (c) 2013 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 13.11.2013 by oliver (ob@oasd.de)
019 */
020
021 package patterntesting.runtime.io;
022
023 import java.io.*;
024
025 /**
026 * If extends {@link BufferedReader} but provides you the line number in which
027 * the reader is reading.
028 *
029 * @author oliver
030 * @since 1.4 (13.11.2013)
031 */
032 public final class LineReader extends BufferedReader {
033
034 private int lineNumber = 0;
035
036 /**
037 * Instantiates a new line reader.
038 *
039 * @param in the in
040 */
041 public LineReader(final Reader in) {
042 super(in);
043 }
044
045 /**
046 * Instantiates a new line reader.
047 *
048 * @param in the in
049 * @param sz the sz
050 */
051 public LineReader(final Reader in, final int sz) {
052 super(in, sz);
053 }
054
055 /**
056 * Gets the line number.
057 *
058 * @return the line number
059 */
060 public int getLineNumber() {
061 return this.lineNumber;
062 }
063
064 /**
065 * Read.
066 *
067 * @return the int
068 * @throws IOException Signals that an I/O exception has occurred.
069 * @see java.io.BufferedReader#read()
070 */
071 @Override
072 public int read() throws IOException {
073 int ch = super.read();
074 if (ch == '\n') {
075 this.lineNumber++;
076 }
077 return ch;
078 }
079
080 /**
081 * Read.
082 *
083 * @param arg0 the arg0
084 * @param arg1 the arg1
085 * @param arg2 the arg2
086 * @return the int
087 * @throws IOException Signals that an I/O exception has occurred.
088 * @see java.io.BufferedReader#read(char[], int, int)
089 */
090 @Override
091 public int read(final char[] arg0, final int arg1, final int arg2) throws IOException {
092 throw new UnsupportedOperationException("not yet implemented");
093 }
094
095 /**
096 * Read line.
097 *
098 * @return the string
099 * @throws IOException Signals that an I/O exception has occurred.
100 * @see java.io.BufferedReader#readLine()
101 */
102 @Override
103 public String readLine() throws IOException {
104 lineNumber++;
105 return super.readLine();
106 }
107
108 /**
109 * Reset.
110 *
111 * @throws IOException Signals that an I/O exception has occurred.
112 * @see java.io.BufferedReader#reset()
113 */
114 @Override
115 public void reset() throws IOException {
116 super.reset();
117 lineNumber = 0;
118 }
119
120 /**
121 * Skip.
122 *
123 * @param arg0 the arg0
124 * @return the long
125 * @throws IOException Signals that an I/O exception has occurred.
126 * @see java.io.BufferedReader#skip(long)
127 */
128 @Override
129 public long skip(final long arg0) throws IOException {
130 throw new UnsupportedOperationException("not supported");
131 }
132
133 /**
134 * Skip lines.
135 *
136 * @param n the number of lines to skip
137 * @throws IOException Signals that an I/O exception has occurred.
138 */
139 public void skipLines(final int n) throws IOException {
140 for (int i = 0; i < n; i++) {
141 if (this.readLine() == null) {
142 break;
143 }
144 }
145 }
146
147 }
148