Bugs/2674/utf8

It's fixing #2674. It also significantly increases performance of UTF encoding.
Power of 2 check can be done very cheap

```
 private boolean powerof2(int x) {
        return (x & (x - 1)) == 0;
    }
```

Should not impact performance much. 

You could also move it to an assert and then it can be disabled if someone wants the best speed.