These are chat archives for nrinaudo/kantan.csv

13th
Dec 2016
Eugene Apollonsky
@chessman
Dec 13 2016 08:16
but I want to write to string instead of file (using asCsv). is it possible? implicit Codec doesn't work in this case.
Nicolas Rinaudo
@nrinaudo
Dec 13 2016 08:30
there is no notion of encoding at the string level though, it's when you write the string as bytes that you need an encoding
I don't remember whether the JVM stores strings as utf-8 or utf-16 (I believe it's the later), but you don't get to choose until you serialise the string
(I'm in meetings all day so I might not answer straight away, but I definitely am interested in your issue and will try to respond as best I can)
Eugene Apollonsky
@chessman
Dec 13 2016 09:13
I need to send this string as http response with akka-http. So is it more correctly to change encoding on akka-http level?
Eugene Apollonsky
@chessman
Dec 13 2016 09:20
Now I have a workaround:
  private def convertToCp1251(str: String): String =
    new String(Charset.forName("cp1251").encode(CharBuffer.wrap(str)).array)
Eugene Apollonsky
@chessman
Dec 13 2016 10:05
oh, no. Workaround isn't working.
Nicolas Rinaudo
@nrinaudo
Dec 13 2016 10:32
no, that workaround cannot really work - akka-http will take your string and serialise it using whatever encoding it's configured for
I don't know anything about akka-http, so I can't tell you how to do it, but there must be a way to specify the encoding used when writing a request or response entity
Eugene Apollonsky
@chessman
Dec 13 2016 14:33

Thank you for advices! Finaly, I did it with implicit Codec:

  implicit val windowsEncoding = new Codec(Charset.forName("cp1251"))
  val byteArrayOutputStream = new ByteArrayOutputStream()
  val writer = byteArrayOutputStream.asCsvWriter[...](...)
  for (row <- ...) writer.write(row)
  writer.close()
  byteArrayOutputStream.toByteArray()

BTW, Is it possible to do it in more functional way?

Nicolas Rinaudo
@nrinaudo
Dec 13 2016 15:31
Assuming that input is a TraversableOnce[A], where A has an instance of RowEncoder:
input.asCsv(',').getBytes(Charset.forName("cp1251"))
Eugene Apollonsky
@chessman
Dec 13 2016 15:39
oh, getBytes! unfortunately, I'm not familiar with Java API.
Nicolas Rinaudo
@nrinaudo
Dec 13 2016 18:49
yeah, that's a common enough problem. The various kantan libraries usually do not require knowledge of the underlying Java APIs, but they do rely on them for low level IO stuff