LocaleUtilDecoderRealpublic class LocaleUtilDecoderReal extends Object implements LocaleUtilDecoder
Fields Summary |
---|
protected CharsetDecoder | decoder | protected int | index |
Constructors Summary |
---|
protected LocaleUtilDecoderReal(int _index, CharsetDecoder _decoder)
index = _index;
decoder = _decoder;
|
Methods Summary |
---|
public java.lang.String | decodeString(byte[] bytes)
if ( bytes == null ){
return( null );
}
ByteBuffer bb = ByteBuffer.wrap(bytes);
CharBuffer cb = CharBuffer.allocate(bytes.length);
CoderResult cr = decoder.decode(bb,cb, true);
try{
if ( !cr.isError() ){
cb.flip();
String str = cb.toString();
byte[] b2 = str.getBytes(decoder.charset().name());
// make sure the conversion is symetric (there are cases where it appears
// to work but in fact converting back to bytes leads to a different
// result
/*
for (int k=0;k<str.length();k++){
System.out.print( Integer.toHexString(str.charAt(k)));
}
System.out.println("");
*/
if ( Arrays.equals( bytes, b2 )){
return( str );
}
}
}catch( Throwable e ){
// Throwable here as we can get "classdefnotfound" + others if the decoder
// isn't available
// ignore
}
try{
// no joy, default
return( new String( bytes, Constants.DEFAULT_ENCODING ));
}catch( UnsupportedEncodingException e ){
Debug.printStackTrace( e );
return( new String( bytes ));
}
| public int | getIndex()
return( index );
| public java.lang.String | getName()
return( decoder.charset().name());
| public java.lang.String | tryDecode(byte[] array, boolean lax)
try{
ByteBuffer bb = ByteBuffer.wrap(array);
CharBuffer cb = CharBuffer.allocate(array.length);
CoderResult cr = decoder.decode(bb,cb, true);
if ( !cr.isError() ){
cb.flip();
String str = cb.toString();
// lax means that as long as the conversion works we consider it usable
// as opposed to strict which requires reverse-conversion equivalence
if ( lax ){
return( str );
}
byte[] b2 = str.getBytes( getName() );
// make sure the conversion is symetric (there are cases where it appears
// to work but in fact converting back to bytes leads to a different
// result
/*
for (int k=0;k<str.length();k++){
System.out.print( Integer.toHexString(str.charAt(k)));
}
System.out.println("");
*/
if ( Arrays.equals( array, b2 )){
return( str );
}
}
return( null );
}catch( Throwable e ){
// Throwable here as we can get "classdefnotfound" + others if the decoder
// isn't available
return( null );
}
|
|