// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.nio.*;
import java.nio.charset.*;

public class TestCharSet {
    public static boolean test(byte[] b, String csnam) {
        CharsetDecoder cd = Charset.availableCharsets().get(csnam).newDecoder();
        try {
            cd.decode(ByteBuffer.wrap(b));
        } catch (CharacterCodingException e) {
            return false;
        }
        return true;
    }
    public static void main(String[] args) throws Exception {
        byte[] b1 = { 0x41, 0x42, 0x43 };
        System.out.println(test(b1, "UTF-8"));
        byte[] b2 = { (byte)0xF1, (byte)0xF2, (byte)0xF3 };
        System.out.println(test(b2, "UTF-8"));
    }
}
