From 66e566cb4d5fab134568089bb9bf39be7d3b3765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Lalire?= Date: Tue, 31 Jan 2023 16:28:54 +0100 Subject: [PATCH] Add small (4 Gb limit) clob support --- .../parsers/common/AbstractWriter.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/univocity/parsers/common/AbstractWriter.java b/src/main/java/com/univocity/parsers/common/AbstractWriter.java index 6a19562f..74d19c38 100644 --- a/src/main/java/com/univocity/parsers/common/AbstractWriter.java +++ b/src/main/java/com/univocity/parsers/common/AbstractWriter.java @@ -23,6 +23,7 @@ import java.io.*; import java.nio.charset.*; +import java.sql.Clob; import java.util.*; /** @@ -1079,7 +1080,27 @@ protected String getStringValue(Object element) { usingNullOrEmptyValue = true; return nullValue; } - String string = String.valueOf(element); + String string; + if (element instanceof Clob) { + StringWriter sw = new StringWriter(); + try { + Reader reader = ((Clob) element).getCharacterStream(); + try { + char[] buffer = new char[8192]; + int n; + while (-1 != (n = reader.read(buffer))) { + sw.write(buffer, 0, n); + } + } finally { + reader.close(); + } + } catch (Exception e) { + throw new RuntimeException("Unable to convert clob", e); + } + string = sw.toString(); + } else { + string = String.valueOf(element); + } if (string.isEmpty()) { usingNullOrEmptyValue = true; return emptyValue; @@ -2580,4 +2601,4 @@ private String getContent(CharSequence tmp) { protected final boolean allowTrim(int fieldIndex) { return !writingHeaders || fieldIndex >= headerTrimFlags.length || headerTrimFlags[fieldIndex]; } -} \ No newline at end of file +}