public final class FileText
extends java.util.AbstractList<java.lang.String>
This class will be passed to instances of class FileSetCheck by Checker. It implements a string list to ensure backwards compatibility, but can be extended in the future to allow more flexible, more powerful or more efficient handling of certain situations.
Modifier and Type | Field and Description |
---|---|
private java.nio.charset.Charset |
charset
The charset used to read the file.
|
private java.io.File |
file
The name of the file.
|
private java.lang.String |
fullText
The full text contents of the file.
|
private static java.util.regex.Pattern |
LINE_TERMINATOR
Regular expression pattern matching all line terminators.
|
private int[] |
lineBreaks
The first position of each line within the full text.
|
private java.lang.String[] |
lines
The lines of the file, without terminators.
|
private static int |
READ_BUFFER_SIZE
The number of characters to read in one go.
|
Modifier | Constructor and Description |
---|---|
private |
FileText(java.io.File file,
java.util.List<java.lang.String> lines)
Compatibility constructor.
|
|
FileText(java.io.File file,
java.lang.String charsetName)
Creates a new file text representation.
|
|
FileText(FileText fileText)
Copy constructor.
|
Modifier and Type | Method and Description |
---|---|
private int[] |
findLineBreaks()
Find positions of line breaks in the full text.
|
static FileText |
fromLines(java.io.File file,
java.util.List<java.lang.String> lines)
Compatibility conversion.
|
java.lang.String |
get(int lineNo)
Retrieves a line of the text by its number.
|
java.nio.charset.Charset |
getCharset()
Get the character set which was used to read the file.
|
java.io.File |
getFile()
Get the name of the file.
|
java.lang.CharSequence |
getFullText()
Retrieve the full text of the file.
|
LineColumn |
lineColumn(int pos)
Determine line and column numbers in full text.
|
private static java.lang.String |
readFile(java.io.File inputFile,
java.nio.charset.CharsetDecoder decoder)
Reads file using specific decoder and returns all its content as a String.
|
int |
size()
Counts the lines of the text.
|
java.lang.String[] |
toLinesArray()
Returns an array of all lines.
|
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
private static final int READ_BUFFER_SIZE
private static final java.util.regex.Pattern LINE_TERMINATOR
private final java.io.File file
null
if no file name is available for whatever reason.private final java.nio.charset.Charset charset
null
if the file was reconstructed from a list of lines.private final java.lang.String fullText
private final java.lang.String[] lines
private int[] lineBreaks
public FileText(java.io.File file, java.lang.String charsetName) throws java.io.IOException
The file will be read using the specified encoding, replacing malformed input and unmappable characters with the default replacement character.
file
- the name of the filecharsetName
- the encoding to use when reading the filejava.lang.NullPointerException
- if the text is nulljava.io.IOException
- if the file could not be readpublic FileText(FileText fileText)
fileText
- to make copy ofprivate FileText(java.io.File file, java.util.List<java.lang.String> lines)
This constructor reconstructs the text of the file by joining lines with linefeed characters. This process does not restore the original line terminators and should therefore be avoided.
file
- the name of the filelines
- the lines of the text, without terminatorsjava.lang.NullPointerException
- if the lines array is nullprivate static java.lang.String readFile(java.io.File inputFile, java.nio.charset.CharsetDecoder decoder) throws java.io.IOException
inputFile
- File to readdecoder
- Charset decoderjava.io.IOException
- Unable to open or read the filepublic static FileText fromLines(java.io.File file, java.util.List<java.lang.String> lines)
This method can be used to convert the arguments passed to
FileSetCheck.process(File,List)
to a FileText
object. If the list of lines already is a FileText, it is
returned as is. Otherwise, a new FileText is constructed by
joining the lines using line feed characters.
file
- the name of the filelines
- the lines of the text, without terminatorspublic java.io.File getFile()
public java.nio.charset.Charset getCharset()
null
for a file reconstructed from its lines.public java.lang.CharSequence getFullText()
public java.lang.String[] toLinesArray()
text.toLinesArray()
is equivalent to
text.toArray(new String[text.size()])
.private int[] findLineBreaks()
public LineColumn lineColumn(int pos)
pos
- the character position in the full textpublic java.lang.String get(int lineNo)
get
in interface java.util.List<java.lang.String>
get
in class java.util.AbstractList<java.lang.String>
lineNo
- the number of the line to get, starting at zeropublic int size()
size
in interface java.util.Collection<java.lang.String>
size
in interface java.util.List<java.lang.String>
size
in class java.util.AbstractCollection<java.lang.String>