patterntesting.runtime.io.LineReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
/*
* $Id: LineReader.java,v 1.3 2013/11/14 21:47:29 oboehm Exp $
*
* Copyright (c) 2013 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 13.11.2013 by oliver ([email protected])
*/
package patterntesting.runtime.io;
import java.io.*;
/**
* If extends {@link BufferedReader} but provides you the line number in which
* the reader is reading.
*
* @author oliver
* @since 1.4 (13.11.2013)
*/
public final class LineReader extends BufferedReader {
private int lineNumber = 0;
/**
* Instantiates a new line reader.
*
* @param in the in
*/
public LineReader(final Reader in) {
super(in);
}
/**
* Instantiates a new line reader.
*
* @param in the in
* @param sz the sz
*/
public LineReader(final Reader in, final int sz) {
super(in, sz);
}
/**
* Gets the line number.
*
* @return the line number
*/
public int getLineNumber() {
return this.lineNumber;
}
/**
* Read.
*
* @return the int
* @throws IOException Signals that an I/O exception has occurred.
* @see java.io.BufferedReader#read()
*/
@Override
public int read() throws IOException {
int ch = super.read();
if (ch == '\n') {
this.lineNumber++;
}
return ch;
}
/**
* Read.
*
* @param arg0 the arg0
* @param arg1 the arg1
* @param arg2 the arg2
* @return the int
* @throws IOException Signals that an I/O exception has occurred.
* @see java.io.BufferedReader#read(char[], int, int)
*/
@Override
public int read(final char[] arg0, final int arg1, final int arg2) throws IOException {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Read line.
*
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
* @see java.io.BufferedReader#readLine()
*/
@Override
public String readLine() throws IOException {
lineNumber++;
return super.readLine();
}
/**
* Reset.
*
* @throws IOException Signals that an I/O exception has occurred.
* @see java.io.BufferedReader#reset()
*/
@Override
public void reset() throws IOException {
super.reset();
lineNumber = 0;
}
/**
* Skip.
*
* @param arg0 the arg0
* @return the long
* @throws IOException Signals that an I/O exception has occurred.
* @see java.io.BufferedReader#skip(long)
*/
@Override
public long skip(final long arg0) throws IOException {
throw new UnsupportedOperationException("not supported");
}
/**
* Skip lines.
*
* @param n the number of lines to skip
* @throws IOException Signals that an I/O exception has occurred.
*/
public void skipLines(final int n) throws IOException {
for (int i = 0; i < n; i++) {
if (this.readLine() == null) {
break;
}
}
}
}