Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-251

CSVPrinter don't handle byte[] correctly for postgres

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • None
    • None
    • Printer
    • None

    Description

      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)))

      ```

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            saitouena saitouena
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: