FileDocCategorySizeDatePackage
DDBaseHelpers.javaAPI DocAzureus 3.0.3.43841Thu Feb 09 19:42:46 GMT 2006org.gudy.azureus2.pluginsimpl.local.ddb

DDBaseHelpers

public class DDBaseHelpers extends Object
author
parg

Fields Summary
Constructors Summary
Methods Summary
protected static java.lang.Objectdecode(java.lang.Class target, byte[] data)

		if ( target == byte[].class ){
			
			return( data );
			
		}else if ( target == String.class ){
			
			try{
				
				return( new String( data, "UTF-8" ));
				
			}catch( UnsupportedEncodingException e ){
				
				throw( new DistributedDatabaseException( "charset error", e ));
			}
		}else{
			
			try{
				ObjectInputStream	iis = new ObjectInputStream( new ByteArrayInputStream( data ));
				
				Object	res = iis.readObject();
				
				if ( target.isInstance( res )){
					
					return( res );
					
				}else{
					
					throw( new DistributedDatabaseException( "decoding fails, incompatible type" ));
				}

			}catch( DistributedDatabaseException e ){
				
				throw( e );
				
			}catch( Throwable e ){
				
				throw( new DistributedDatabaseException( "decoding fails", e ));
			}		
		}
	
protected static byte[]encode(java.lang.Object obj)

		byte[]	res;
		
		if ( obj == null ){
			
			throw( new DistributedDatabaseException( "null not supported" ));
			
		}else if ( obj instanceof byte[] ){
			
			res = (byte[])obj;
			
		}else if ( obj instanceof String ){
			
			try{
				res = ((String)obj).getBytes("UTF-8");
			
			}catch( UnsupportedEncodingException e ){
				
				throw( new DistributedDatabaseException( "charset error", e ));
			}
		}else if (	obj instanceof Byte ||
					obj instanceof Short ||
					obj instanceof Integer ||
					obj instanceof Long ||
					obj instanceof Float ||
					obj instanceof Double ||
					obj instanceof Boolean ){
			
			throw( new DistributedDatabaseException( "not supported yet!" ));
					
		}else{
			
			try{
				ByteArrayOutputStream	baos = new ByteArrayOutputStream();
				
				ObjectOutputStream	oos = new ObjectOutputStream( baos );
				
				oos.writeObject( obj );
				
				oos.close();
				
				res = baos.toByteArray();
				
			}catch( Throwable e ){
				
				throw( new DistributedDatabaseException( "encoding fails", e ));
			}
		}
		
		return( res );
	
protected static org.gudy.azureus2.core3.util.HashWrappergetKey(java.lang.Class c)

		String	name = c.getName();
		
		if ( name == null ){
			
			throw( new DistributedDatabaseException( "name doesn't exist for '" + c.getName() + "'" ));
		}
			
		return( new HashWrapper(new SHA1Simple().calculateHash(name.getBytes())));