Class TextFileContent
java.lang.Object
net.sourceforge.pmd.lang.document.TextFileContent
Contents of a text file.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe normalized line ending used to replace platform-specific line endings in the normalized text.static final charThe normalized line ending as a char. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull TextFileContentfromCharSeq(CharSequence text) Normalize the line endings of the text to "\n", returns aTextFileContentcontaining the original line ending.static TextFileContentfromInputStream(InputStream inputStream, Charset sourceEncoding) Reads the contents of the input stream into a TextFileContent.static TextFileContentfromReader(Reader reader) Read the reader fully and produce aTextFileContent.longReturns a checksum for the contents of the file.Returns the original line terminator found in the file.The text of the file, with the following normalizations: Line endings are normalized to "\n".
-
Field Details
-
NORMALIZED_LINE_TERM
The normalized line ending used to replace platform-specific line endings in the normalized text.- See Also:
-
NORMALIZED_LINE_TERM_CHAR
public static final char NORMALIZED_LINE_TERM_CHARThe normalized line ending as a char.- See Also:
-
-
Method Details
-
getNormalizedText
The text of the file, with the following normalizations:- Line endings are normalized to "\n".
For this purpose, a line ending is either
\r,\r\nor\n(CR, CRLF or LF), not the full range of unicode line endings. This is consistent withBufferedReader.readLine(), and the JLS, for example. - An initial byte-order mark is removed, if any.
- Line endings are normalized to "\n".
For this purpose, a line ending is either
-
getLineTerminator
Returns the original line terminator found in the file. This is the terminator that should be used to write the file back to disk. If the original file either has mixed line terminators, or has no line terminator at all, the line terminator defaults to the platform-specific one (System.lineSeparator()). -
getCheckSum
public long getCheckSum()Returns a checksum for the contents of the file. The checksum is computed on the unnormalized bytes, so may be affected by a change line terminators. This is why twoTextFileContents with the same normalized content may have different checksums. -
fromCharSeq
Normalize the line endings of the text to "\n", returns aTextFileContentcontaining the original line ending. If the text does not contain any line terminators, or if it contains a mix of different terminators, falls back to the platform-specific line separator.- Parameters:
text- Text content of a file- Returns:
- A text file content
-
fromReader
Read the reader fully and produce aTextFileContent. This closes the reader. This takes care of buffering.- Parameters:
reader- A reader- Returns:
- A text file content
- Throws:
IOException- If an IO exception occurs
-
fromInputStream
public static TextFileContent fromInputStream(InputStream inputStream, Charset sourceEncoding) throws IOException Reads the contents of the input stream into a TextFileContent. This closes the input stream. This takes care of buffering.- Parameters:
inputStream- Input streamsourceEncoding- Encoding to use to read from the data source- Throws:
IOException
-