public static int[] bufferStringChunkUTF8(CharSequence s, int start, byte[] byteBuffer) throws IOException {
        int bufferPos = 0;
        int stringPos = start;
        while (stringPos < s.length()) {
            char ch = s.charAt(stringPos);
            int encodingSize = utf8EncodingSize(ch);
            if ((bufferPos + encodingSize) > byteBuffer.length) {
                break;
            }

            switch (encodingSize) {
                case 1:
                    byteBuffer[bufferPos++] = (byte) ch;
                    break;
                case 2:
                    byteBuffer[bufferPos++] = (byte) (0xc0 | ch >> 6 & 0x1f);
                    byteBuffer[bufferPos++] = (byte) (0x80 | ch >> 0 & 0x3f);
                    break;
                case 3:
                    byteBuffer[bufferPos++] = (byte) (0xe0 | ch >> 12 & 0x0f);
                    byteBuffer[bufferPos++] = (byte) (0x80 | ch >> 6 & 0x3f);
                    byteBuffer[bufferPos++] = (byte) (0x80 | ch >> 0 & 0x3f);
                    break;
            }
            stringPos++;
        }
        return new int[] {stringPos, bufferPos};
    }