Class TextFileContent
- java.lang.Object
-
- net.sourceforge.pmd.lang.document.TextFileContent
-
public final class TextFileContent extends Object
Contents of a text file.
-
-
Field Summary
Fields Modifier and Type Field Description static String
NORMALIZED_LINE_TERM
The normalized line ending used to replace platform-specific line endings in the normalized text.static char
NORMALIZED_LINE_TERM_CHAR
The normalized line ending as a char.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static @NonNull TextFileContent
fromCharSeq(CharSequence text)
Normalize the line endings of the text to "\n", returns aTextFileContent
containing the original line ending.static TextFileContent
fromInputStream(InputStream inputStream, Charset sourceEncoding)
Reads the contents of the input stream into a TextFileContent.static TextFileContent
fromReader(Reader reader)
Read the reader fully and produce aTextFileContent
.long
getCheckSum()
Returns a checksum for the contents of the file.String
getLineTerminator()
Returns the original line terminator found in the file.Chars
getNormalizedText()
The text of the file, with the following normalizations: Line endings are normalized to "\n".
-
-
-
Field Detail
-
NORMALIZED_LINE_TERM
public static final String NORMALIZED_LINE_TERM
The normalized line ending used to replace platform-specific line endings in the normalized text.- See Also:
- Constant Field Values
-
NORMALIZED_LINE_TERM_CHAR
public static final char NORMALIZED_LINE_TERM_CHAR
The normalized line ending as a char.- See Also:
- Constant Field Values
-
-
Method Detail
-
getNormalizedText
public Chars 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\n
or\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
public String 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 twoTextFileContent
s with the same normalized content may have different checksums.
-
fromCharSeq
public static @NonNull TextFileContent fromCharSeq(CharSequence text)
Normalize the line endings of the text to "\n", returns aTextFileContent
containing 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
public static TextFileContent fromReader(Reader reader) throws IOException
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
-
-