FileDocCategorySizeDatePackage
LocaleUtilDecoderReal.javaAPI DocAzureus 3.0.3.43969Thu Feb 09 19:42:46 GMT 2006org.gudy.azureus2.core3.internat

LocaleUtilDecoderReal

public class LocaleUtilDecoderReal extends Object implements LocaleUtilDecoder
author
parg

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.StringdecodeString(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 intgetIndex()

		return( index );
	
public java.lang.StringgetName()

		return( decoder.charset().name());
	
public java.lang.StringtryDecode(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 );
		}