is Data UTF - Java java.lang

Java examples for java.lang:String UTF

Description

is Data UTF

Demo Code


//package com.java2s;

public class Main {
    public static boolean isDataUTF8(byte[] array, int start, int length) {
        if (0 == length) {
            return false;
        }/* w  ww .j av  a 2s  . c o m*/

        for (int i = start, len = length; len > 0;) {
            byte bt = array[i++];
            len--;

            int seqLen = 0;
            if ((bt & 0xE0) == 0xC0)
                seqLen = 1;
            else if ((bt & 0xF0) == 0xE0)
                seqLen = 2;
            else if ((bt & 0xF8) == 0xF0)
                seqLen = 3;
            else if ((bt & 0xFC) == 0xF8)
                seqLen = 4;
            else if ((bt & 0xFE) == 0xFC)
                seqLen = 5;

            if (0 == seqLen) {
                if ((bt & 0x80) == 0x80) {
                    return false;
                }
                continue;
            }

            for (int j = 0; j < seqLen; ++j) {
                if (len == 0)
                    return false;
                bt = array[i++];
                if ((bt & 0xC0) != 0x80)
                    return false;
                len--;
            }
        }
        return true;
    }
}

Related Tutorials