Class TextFileContent

java.lang.Object
net.sourceforge.pmd.lang.document.TextFileContent

public final class TextFileContent extends Object
Contents of a text file.
  • Field Details

    • 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:
    • NORMALIZED_LINE_TERM_CHAR

      public static final char NORMALIZED_LINE_TERM_CHAR
      The normalized line ending as a char.
      See Also:
  • Method Details

    • 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 with BufferedReader.readLine(), and the JLS, for example.
      • An initial byte-order mark is removed, if any.
    • 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 two TextFileContents 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 a TextFileContent 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 a TextFileContent. 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 stream
      sourceEncoding - Encoding to use to read from the data source
      Throws:
      IOException