Remove UTF-8 checking duplication in Config lib subclasses

Change-Id: Ib9f0ae8207000a36c5bf1a92fcc2c32efc4c0984
Signed-off-by: Marco Miller <marco.miller@ericsson.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Marco Miller 2016-05-09 12:36:00 -04:00 committed by Matthias Sohn
parent 3bc46dfed5
commit e5a9915a92
3 changed files with 15 additions and 4 deletions

View File

@ -80,8 +80,7 @@ public BlobBasedConfig(Config base, final byte[] blob)
throws ConfigInvalidException {
super(base);
final String decoded;
if (blob.length >= 3 && blob[0] == (byte) 0xEF
&& blob[1] == (byte) 0xBB && blob[2] == (byte) 0xBF) {
if (isUtf8(blob)) {
decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET,
blob, 3, blob.length);
} else {

View File

@ -1108,6 +1108,19 @@ protected void clear() {
state.set(newState());
}
/**
* Check if bytes should be treated as UTF-8 or not.
*
* @param bytes
* the bytes to check encoding for.
* @return true if bytes should be treated as UTF-8, false otherwise.
* @since 4.4
*/
protected boolean isUtf8(final byte[] bytes) {
return bytes.length >= 3 && bytes[0] == (byte) 0xEF
&& bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF;
}
private static String readSectionName(final StringReader in)
throws ConfigInvalidException {
final StringBuilder name = new StringBuilder();

View File

@ -147,8 +147,7 @@ public void load() throws IOException, ConfigInvalidException {
snapshot = newSnapshot;
} else {
final String decoded;
if (in.length >= 3 && in[0] == (byte) 0xEF
&& in[1] == (byte) 0xBB && in[2] == (byte) 0xBF) {
if (isUtf8(in)) {
decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET,
in, 3, in.length);
utf8Bom = true;