Class CsvEscaper

java.lang.Object
org.cthing.escapers.CsvEscaper

public final class CsvEscaper extends Object
Escapes string and character arrays according to RFC-4180. If a string contains a comma, carriage return, line feed, or a double quote, the string must be surrounded by double quotes and any double quotes within the string must be replaced with two consecutive double quotes (i.e. a single " becomes "").
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    escape(char[] charArr)
    Applies CSV escaping to the specified character array.
    static String
    escape(char[] charArr, int offset, int length)
    Applies CSV escaping to the specified character array.
    static void
    escape(char[] charArr, int offset, int length, Writer writer)
    Applies CSV escaping to the specified character array and writes the result to the specified writer.
    static void
    escape(char[] charArr, Writer writer)
    Applies CSV escaping to the specified character array and writes the result to the specified writer.
    static String
    escape(CharSequence charSequence)
    Applies CSV escaping to the specified string.
    static void
    escape(CharSequence charSequence, Writer writer)
    Applies CSV escaping to the specified string and writes the result to the specified writer.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • escape

      public static String escape(CharSequence charSequence)
      Applies CSV escaping to the specified string.
      Parameters:
      charSequence - String to escape
      Returns:
      Escaped string or null if null was passed in.
    • escape

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer) throws IOException
      Applies CSV escaping to the specified string and writes the result to the specified writer.
      Parameters:
      charSequence - String to escape
      writer - Writer to which the escaped string is written
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      public static String escape(char[] charArr)
      Applies CSV escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(char[] charArr, int offset, int length)
      Applies CSV escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      Returns:
      Escaped string or null if null was passed in.
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer) throws IOException
      Applies CSV escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      writer - Writer to which the escaped string is written
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer) throws IOException
      Applies CSV escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      writer - Writer to which the escaped string is written
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null