0

There are ways to determine content encoding inside byte array in Groovy/Grails?
I has only file content stored inside database in blob and groovy side is stored as:

byte[] fileData

1 Answers1

1

There are some ways, but they are more like workarounds. You could transform it into a String, but its constructors still need a second parameter which is the encoding. String constructors don't "detect", but rather "use" the passed encoding.

You could try some encoding detecting algorithms, like mozilla's or jchardet.

This answer sums nicely why detecting encoding from a byte array is wrong:

You cannot determine the encoding of a arbitrary byte stream. This is the nature of encodings. A encoding means a mapping between a byte value and its representation. So every encoding "could" be the right.

Community
  • 1
  • 1
Will
  • 14,348
  • 1
  • 42
  • 44
  • I agree completely. You should try and find out the encoding from whoever stores it in the database. If thats not possible for whatever reason, encoding detection algorithms are your best bet. – OsaSoft Aug 15 '16 at 16:35
  • Ok. Thank you guys. But this two algorithms will works in grails app? Or need do integration with Groovy and Java class? – Sergey Tarasov Aug 16 '16 at 06:29
  • jchardet is a java lib, so seamless integration with groovy. – Will Aug 16 '16 at 13:40