Class FileText
java.lang.Object
com.puppycrawl.tools.checkstyle.api.FileText
Represents the text contents of a file of arbitrary plain text type.
This class will be passed to instances of class FileSetCheck by Checker.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Charset
The charset used to read the file.private final File
The name of the file.private String
The full text contents of the file.private static final Pattern
Regular expression pattern matching all line terminators.private int[]
The first position of each line within the full text.private final String[]
The lines of the file, without terminators.private static final int
The number of characters to read in one go. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate int[]
Find positions of line breaks in the full text.get
(int lineNo) Retrieves a line of the text by its number.Get the character set which was used to read the file.getFile()
Get the name of the file.Retrieve the full text of the file.lineColumn
(int pos) Determine line and column numbers in full text.private static String
readFile
(File inputFile, CharsetDecoder decoder) Reads file using specific decoder and returns all its content as a String.int
size()
Counts the lines of the text.String[]
Returns an array of all lines.
-
Field Details
-
READ_BUFFER_SIZE
The number of characters to read in one go.- See Also:
-
LINE_TERMINATOR
Regular expression pattern matching all line terminators. -
file
The name of the file.null
if no file name is available for whatever reason. -
charset
The charset used to read the file.null
if the file was reconstructed from a list of lines. -
lines
The lines of the file, without terminators. -
fullText
The full text contents of the file. -
lineBreaks
The first position of each line within the full text.
-
-
Constructor Details
-
FileText
Copy constructor.- Parameters:
fileText
- to make copy of
-
FileText
Compatibility constructor.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.
- Parameters:
file
- the name of the filelines
- the lines of the text, without terminators- Throws:
NullPointerException
- if the lines array is null
-
FileText
Creates a new file text representation.The file will be read using the specified encoding, replacing malformed input and unmappable characters with the default replacement character.
- Parameters:
file
- the name of the filecharsetName
- the encoding to use when reading the file- Throws:
NullPointerException
- if the text is nullIllegalStateException
- if the charset is not supported.IOException
- if the file could not be read
-
-
Method Details
-
readFile
Reads file using specific decoder and returns all its content as a String.- Parameters:
inputFile
- File to readdecoder
- Charset decoder- Returns:
- File's text
- Throws:
IOException
- Unable to open or read the fileFileNotFoundException
- when inputFile does not exist
-
get
Retrieves a line of the text by its number. The returned line will not contain a trailing terminator.- Parameters:
lineNo
- the number of the line to get, starting at zero- Returns:
- the line with the given number
-
getFile
Get the name of the file.- Returns:
- an object containing the name of the file
-
getCharset
Get the character set which was used to read the file. Will benull
for a file reconstructed from its lines.- Returns:
- the charset used when the file was read
-
getFullText
Retrieve the full text of the file.- Returns:
- the full text of the file
-
toLinesArray
Returns an array of all lines.text.toLinesArray()
is equivalent totext.toArray(new String[text.size()])
.- Returns:
- an array of all lines of the text
-
lineColumn
Determine line and column numbers in full text.- Parameters:
pos
- the character position in the full text- Returns:
- the line and column numbers of this character
-
findLineBreaks
Find positions of line breaks in the full text.- Returns:
- an array giving the first positions of each line.
-
size
Counts the lines of the text.- Returns:
- the number of lines in the text
-