Class CsvUtil
java.lang.Object
org.bgerp.plugin.inventoru.sync1c.service.CsvUtil
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
FieldsModifier and TypeFieldDescriptionstatic final CharsetCharset of most Cyrillic exports that are not UTF-8. -
Method Summary
Modifier and TypeMethodDescriptionstatic CharsetdetectCharset(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 chardetectDelimiter(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.parse(byte[] data) Parses CSV with the delimiter and charset detected from the content.Parses CSV.parse(InputStream in) 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 voidwrite(OutputStream out, List<String[]> rows) Writes rows as UTF-8 CSV with a BOM, comma-separated.static voidwrite(OutputStream out, List<String[]> rows, char delimiter, Charset charset) Writes rows as CSV, quoting fields that contain the delimiter, a quote or a newline.
-
Field Details
-
WINDOWS_1251
Charset of most Cyrillic exports that are not UTF-8.
-
-
Method Details
-
parse
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 contentdelimiter- explicit delimiter,nullto detectcharset- explicit charset,nullto detect- Throws:
IOException
-
parse
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. Preferparse(byte[], Character, Charset).- Throws:
IOException
-
detectCharset
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
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
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
-