Skip to content

Instantly share code, notes, and snippets.

@maerzbow
Created June 29, 2014 14:49
Show Gist options
  • Save maerzbow/b49e9de83e9657b197a7 to your computer and use it in GitHub Desktop.
Save maerzbow/b49e9de83e9657b197a7 to your computer and use it in GitHub Desktop.
EnsureEncoding main part
/**
* will try to convert the given chars to a valid string.
* will check for every valid conversion if the given {@link ContentCheck} is valid, if not
* it will continue with the next encoding
*
* @param chars
* characters in an unknown charset
* @param check
* this instance will be called for every valid conversion
* @return the chars converted to a String decoded by the first matching {@link Charset}
* @throws Exception
* if converting the chars to a String was not possible
*/
public String decode(byte[] chars, ContentCheck check) throws Exception {
for (Charset encodingToTry : encodingsToTry) {
try {
String content = decode(chars, encodingToTry);
if (check.isValidContent(content)) {
return content;
}
} catch (CharacterCodingException e) {
// try with next encoding
}
}
throw new IllegalStateException("was not able to encode string using these encodings"
+ Arrays.toString(encodingsToTry));
}
protected String decode(byte[] chars, Charset encodingToTry) throws CharacterCodingException {
CharsetDecoder decoder = encodingToTry.newDecoder().onMalformedInput(CodingErrorAction.REPORT);
ByteBuffer byteBuffer = ByteBuffer.wrap(chars);
CharBuffer decoded = decoder.decode(byteBuffer);
return decoded.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment