FileDocCategorySizeDatePackage
LocaleUtilDecoderFallback.javaAPI DocAzureus 3.0.3.44438Wed May 03 14:57:36 BST 2006org.gudy.azureus2.core3.internat

LocaleUtilDecoderFallback

public class LocaleUtilDecoderFallback extends Object implements LocaleUtilDecoder
author
parg

Fields Summary
public static String
NAME
private static volatile int
max_ok_name_length
private static volatile boolean
max_ok_name_length_determined
private static final String
VALID_CHARS
private int
index
Constructors Summary
protected LocaleUtilDecoderFallback(int _index)

	

	
	
				 
	
		index	= _index;
	
Methods Summary
protected java.lang.Stringdecode(byte[] data)

		if ( data == null ){
			
			return( null );
		}
		
		StringBuffer	res = new StringBuffer( data.length*2 );
		
		for (int i=0;i<data.length;i++){
			
			byte	c = data[i];
			
			if ( VALID_CHARS.indexOf( Character.toLowerCase((char)c)) != -1 ){
				
				res.append((char)c);
				
			}else{
				
				res.append( "_" );
				res.append( ByteFormatter.nicePrint(c));
			}
		}
		
			// more often that not these decoded values are used for filenames. Windows has a limit
			// of 250 (ish) chars, so we do something sensible with longer values
		
		int	len = res.length();
		
		if ( len > max_ok_name_length ){
		
				// could be a file system out there that supports arbitarily long names, so
				// we can't pre-calculate the max
			
			if ( 	( !max_ok_name_length_determined )&&
					fileLengthOK( len )){
			
					// this length is ok, bump up the known limit
				
				max_ok_name_length = len;
				
			}else{
				
					// won't fit
				
				if ( !max_ok_name_length_determined ){
					
					for (int i=max_ok_name_length+16;i<len;i+=16 ){
						
						if ( fileLengthOK( i )){
							
							max_ok_name_length	= i;
							
						}else{
							
							break;
						}
					}
					
					max_ok_name_length_determined	= true;
				}
				
					// try and preserve extension
				
				String	extension = null;
				
				int	pos = res.lastIndexOf(".");
				
				if ( pos != -1 ){
					
						// include the "."
					
					extension = res.substring( pos );
					
					if ( extension.length() == 1 || extension.length() > 4 ){
						
						extension = null;
					}
				}
					// replace the end of the string with a hash value to ensure uniqueness
				
				byte[] hash = new SHA1Hasher().calculateHash( data );
				
				String	hash_str = ByteFormatter.nicePrint( hash, true );
				
				res = new StringBuffer(res.substring( 
							0, 
							max_ok_name_length - hash_str.length() - (extension == null?0:extension.length())));
				
				res.append( hash_str );
				
				if ( extension != null ){
					
					res.append( extension );
				}
			}
		}
		
		return( res.toString());
	
public java.lang.StringdecodeString(byte[] bytes)

		return( decode( bytes ));
	
protected booleanfileLengthOK(int len)

		StringBuffer n = new StringBuffer( len );
		
		for (int i=0;i<len;i++){
			
			n.append( "A" );
		}
				
		try{
			File f = File.createTempFile( n.toString(), "" );
						
			f.delete();
			
			return( true );
			
		}catch( Throwable e ){
			
			return( false );
		}
	
public intgetIndex()

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

		return( NAME );
	
public java.lang.StringtryDecode(byte[] bytes, boolean lax)

		return( decode( bytes ));