private static CharBuffer decodeString ( byte [ ] bytes , Charset charset , int referenceLength ) { try { final Charset autodetectedCharset ; final CharsetDecoder decoder = charset . newDecoder ( ) ; final CharBuffer buffer = decoder . decode ( ByteBuffer . wrap ( bytes ) ) ; if ( ( decoder . isAutoDetecting ( ) ) && ( decoder . isCharsetDetected ( ) ) ) { autodetectedCharset = decoder . detectedCharset ( ) ; if ( charset . contains ( autodetectedCharset ) ) { buffer . position ( 0 ) ; if ( ( referenceLength >= 0 ) && ( buffer . remaining ( ) == referenceLength ) ) { return buffer ; } return null ; } } // Apply a proprietary detection buffer . position ( 0 ) ; char c ; int type ; while ( buffer . hasRemaining ( ) ) { c = buffer . get ( ) ; type = Character . getType ( c ) ; switch ( type ) { case Character . UNASSIGNED : case Character . CONTROL : case Character . FORMAT : case Character . PRIVATE_USE : case Character . SURROGATE : // Character not supported? return null ; default : } } buffer . position ( 0 ) ; if ( ( referenceLength >= 0 ) && ( buffer . remaining ( ) == referenceLength ) ) { return buffer ; } } catch ( CharacterCodingException e ) { // } return null ; }
Decode the specified array of bytes with the specified charset .