Class CsvUtil

java.lang.Object
org.bgerp.plugin.inventoru.sync1c.service.CsvUtil

public class CsvUtil extends Object
Minimal RFC4180 CSV reader/writer for the sync1c File protocol — no CSV library is otherwise used in this project, so a dedicated dependency isn't warranted for two simple flat formats. Real exports from 1C are not RFC4180-shaped: the de-facto industry format (e.g. the Cleverence "Склад 15" exchange) is semicolon-separated, because a comma is the decimal separator in the Russian locale, and the file often comes in Windows-1251 rather than UTF-8. Both the delimiter and the charset are therefore detected from the content, with an explicit override available per connector (csv.delimiter / csv.encoding).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Charset
    Charset of most Cyrillic exports that are not UTF-8.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Charset
    detectCharset(byte[] data)
    Detects the charset: an UTF-8 BOM decides it outright, otherwise the content is decoded as strict UTF-8 — a file that is not valid UTF-8 is assumed to be Windows-1251, which is the only other charset seen in practice for these exports.
    static char
    Detects the delimiter by counting candidates outside quoted fields in the first line: the header row is the most reliable sample, since data rows may legitimately contain any of them inside quotes.
    static List<String[]>
    parse(byte[] data)
    Parses CSV with the delimiter and charset detected from the content.
    static List<String[]>
    parse(byte[] data, Character delimiter, Charset charset)
    Parses CSV.
    static List<String[]>
    Deprecated.
    stream variant kept for callers that don't hold the whole content; it buffers the stream anyway, since both charset and delimiter detection need to look at the bytes before decoding.
    static void
    write(OutputStream out, List<String[]> rows)
    Writes rows as UTF-8 CSV with a BOM, comma-separated.
    static void
    write(OutputStream out, List<String[]> rows, char delimiter, Charset charset)
    Writes rows as CSV, quoting fields that contain the delimiter, a quote or a newline.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • WINDOWS_1251

      public static final Charset WINDOWS_1251
      Charset of most Cyrillic exports that are not UTF-8.
  • Method Details

    • parse

      public static List<String[]> parse(byte[] data) throws IOException
      Parses CSV with the delimiter and charset detected from the content.
      Parameters:
      data - whole file content
      Throws:
      IOException
    • parse

      public static List<String[]> parse(byte[] data, Character delimiter, Charset charset) throws IOException
      Parses CSV.
      Parameters:
      data - whole file content
      delimiter - explicit delimiter, null to detect
      charset - explicit charset, null to detect
      Throws:
      IOException
    • parse

      @Deprecated public static List<String[]> parse(InputStream in) throws IOException
      Deprecated.
      stream variant kept for callers that don't hold the whole content; it buffers the stream anyway, since both charset and delimiter detection need to look at the bytes before decoding. Prefer parse(byte[], Character, Charset).
      Throws:
      IOException
    • detectCharset

      public static Charset detectCharset(byte[] data)
      Detects the charset: an UTF-8 BOM decides it outright, otherwise the content is decoded as strict UTF-8 — a file that is not valid UTF-8 is assumed to be Windows-1251, which is the only other charset seen in practice for these exports. Pure ASCII decodes as UTF-8, and for ASCII the two charsets agree anyway.
    • detectDelimiter

      public static char detectDelimiter(String text)
      Detects the delimiter by counting candidates outside quoted fields in the first line: the header row is the most reliable sample, since data rows may legitimately contain any of them inside quotes. Falls back to a comma when the line holds no candidate at all.
    • write

      public static void write(OutputStream out, List<String[]> rows) throws IOException
      Writes rows as UTF-8 CSV with a BOM, comma-separated.
      Throws:
      IOException
    • write

      public static void write(OutputStream out, List<String[]> rows, char delimiter, Charset charset) throws IOException
      Writes rows as CSV, quoting fields that contain the delimiter, a quote or a newline. A BOM is prepended for UTF-8 so Excel opens Cyrillic text correctly.
      Throws:
      IOException