Code example for CharsetDecoder

Methods: decodereset

0
    private static Charset detectCharset(byte[] bytes, Charset charset)
    { 
        try 
        { 
            CharsetDecoder decoder = charset.newDecoder();
            decoder.reset();
             
            boolean identified = identify(bytes, decoder);
             
            if (identified)
            { 
                return charset;
            } 
            else 
            { 
                return null; 
            } 
             
        } 
        catch (Exception e)
        { 
            return null; 
        } 
    } 
     
    private static boolean identify(byte[] bytes, CharsetDecoder decoder)
    { 
        try 
        { 
            decoder.decode(ByteBuffer.wrap(bytes));
        } 
        catch (CharacterCodingException e)
        { 
            return false; 
        } 
Stop searching for code, let great code find you!  Add Codota to your java IDE