Class RawTextTools

java.lang.Object
com.saperion.ngc.util.RawTextTools

public final class RawTextTools extends Object
A utility that provides several methods to analyze and convert raw text data.
  • Method Details

    • isPlainTextFile

      public static boolean isPlainTextFile(byte[] bytes, String filename) throws IOException
      Detects if the data contains plain text.
      Parameters:
      bytes - data to analyze
      filename - name of the file the data is contained in
      Returns:
      true if data contains plain text
      Throws:
      IOException - IO exception when trying to detect media type
    • getMediaType

      public static String getMediaType(byte[] bytes, String filename) throws IOException
      Detects the media type of the provided data.
      Parameters:
      bytes - original data
      filename - name of the file the data was contained in
      Returns:
      media type (like 'text/plain' for plain text)
      Throws:
      IOException - IO exception when reading original data
    • convertTextToPdf

      public static byte[] convertTextToPdf(byte[] original, String font, float fontSize) throws com.lowagie.text.DocumentException, IOException
      Converts the provided plain text data to PDF.
      Parameters:
      original - plain text data
      font - font to use (like C:/Windows/Fonts/arial.ttf). This must be a true-type font! If null or empty, a default helvetica font will be used. Note that unicode characters will not work then.
      fontSize - size of the font
      Returns:
      PDF document as array of bytes
      Throws:
      com.lowagie.text.DocumentException - Exception when creating the PDF document
      IOException - IO exception when reading the original data
    • convertTextToPdf

      public static byte[] convertTextToPdf(byte[] original, String font, float fontSize, String defaultCharset) throws com.lowagie.text.DocumentException, IOException
      Converts the provided plain text data to PDF.
      Parameters:
      original - plain text data
      font - font to use (like C:/Windows/Fonts/arial.ttf). This must be a true-type font! If null or empty, a default helvetica font will be used. Note that unicode characters will not work then.
      fontSize - size of the font
      defaultCharset - charset to use when auto-detection of charset failed
      Returns:
      PDF document as array of bytes
      Throws:
      com.lowagie.text.DocumentException - Exception when creating the PDF document
      IOException - IO exception when reading the original data
    • getUnicodeString

      public static String getUnicodeString(byte[] original) throws IOException
      Returns a unicode encoded string corresponding to the provided data. The encoding of the original data is auto-detected. If an unsupported encoding is detected, ISO-8859-1 is used as a fallback.
      Parameters:
      original - original data
      Returns:
      unicode string corresponding to original data
      Throws:
      IOException - IO exception when reading the original data
    • getUnicodeString

      public static String getUnicodeString(byte[] original, String defaultCharset) throws IOException
      Returns a unicode encoded string corresponding to the provided data. The encoding of the original data is auto-detected. If an unsupported encoding is detected, the provided fallback is used.
      Parameters:
      original - original data
      defaultCharset - charset to use when auto-detection of charset failed
      Returns:
      unicode string corresponding to original data
      Throws:
      IOException - IO exception when reading the original data