Here you can find the source of encodingIsCorrect(byte[] bytes, String charset)
Parameter | Description |
---|---|
bytes | the bytes to convert to string. |
charset | the charset to convert to |
public static boolean encodingIsCorrect(byte[] bytes, String charset)
//package com.java2s; //License from project: Apache License import java.nio.*; import java.nio.charset.*; public class Main { /**//from w w w. j ava2 s .c om * Used to verify the encoding of a byte array. This method does not actually * convert to string; it merely checks to see if it is possible without errors. * @param bytes the bytes to convert to string. * @param charset the charset to convert to * @return true is the bytes can be encoded into this charset, and false otherwise */ public static boolean encodingIsCorrect(byte[] bytes, String charset) { try { CharsetDecoder decoder = Charset.forName(charset).newDecoder(); decoder.decode(ByteBuffer.wrap(bytes)); } catch (Exception e) { return false; } return true; } }