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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Ok. Thank you guys. But this two algorithms will works in grails app? Or need do integration with Groovy and Java class?
jchardet is a java lib, so seamless integration with groovy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.