Android UTF8 Encode isValidUTF8(byte[] input)

Here you can find the source of isValidUTF8(byte[] input)

Description

is Valid UTF

License

Open Source License

Declaration

public static boolean isValidUTF8(byte[] input) 

Method Source Code

//package com.java2s;
/*/*w ww . j  a v a 2  s  .  co m*/
 * Copyright (C) 2014 Dominik Sch?rmann <dominik@dominikschuermann.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Main {
    public static boolean isValidUTF8(byte[] input) {
        CharsetDecoder cs = Charset.forName("UTF-8").newDecoder();

        try {
            cs.decode(ByteBuffer.wrap(input));
            return true;
        } catch (CharacterCodingException e) {
            return false;
        }
    }
}

Related

  1. bytesUTF8len(byte[] buf, int bufOffset, int bufLength)
  2. char2ByteUTF8(String input, int inOff, int inEnd, byte[] output, int outOff, int outEnd, boolean getLengthFlag)
  3. getStringInUtf8(final String str)
  4. stringToUtf8Bytes(String string)
  5. utf8encode(String str)
  6. string2BytesUTF8(String str)
  7. toUtf8Bytes(String data)
  8. toUtf8String(byte[] data)
  9. stringFromUtf8ByteArray(byte[] data)