You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "saitouena (Jira)" <ji...@apache.org> on 2019/09/04 10:12:00 UTC
[jira] [Created] (CSV-251) CSVPrinter don't handle byte[] correctly
for postgres
saitouena created CSV-251:
-----------------------------
Summary: CSVPrinter don't handle byte[] correctly for postgres
Key: CSV-251
URL: https://issues.apache.org/jira/browse/CSV-251
Project: Commons CSV
Issue Type: Improvement
Components: Printer
Environment: java version
```
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
```
apache.commons.csv: 1.6
clojure: 1.9.0
postgres: 10.7
Reporter: saitouena
I passed byte[] values for CSVPrinter.printRecords with CSVFormat.POSTGRESQL_CSV, but it use Object.toString(printed [B@xxxx in clojure). As a result, data were discarded.
I don't know about other RDBs, but at least postgres supports [https://www.postgresql.org/docs/9.3/datatype-binary.html] hexstring format.
Workaround: Wrap byte[] with the class that has proper toString method like this.
```
public string toString() {
return "\\x" + encodeHexString(this.bytedata);
}
```
I used apache.commons.csv from Clojure. I'll make a minimal repro case in Java if needed. I have clojure code for now.
clojure code:
```
(deftype ByteArrayForCopyIn [^"[B" bs]
Object
(toString [self] (str "\\x" (Hex/encodeHexString bs))))
(let [sb (StringBuilder.)
values [(.getBytes "hoge")]]
(with-open [^CSVPrinter p (CSVPrinter. sb CSVFormat/POSTGRESQL_CSV)]
(.printRecords p values)))
```
--
This message was sent by Atlassian Jira
(v8.3.2#803003)