FileDocCategorySizeDatePackage
UpdateJarPatcher.javaAPI DocAzureus 3.0.3.44030Thu May 31 16:36:34 BST 2007org.gudy.azureus2.update

UpdateJarPatcher

public class UpdateJarPatcher extends Object
author
parg

Fields Summary
private static String
MANIFEST_NAME
protected Map
patch_entries
Constructors Summary
protected UpdateJarPatcher(InputStream input_file, InputStream patch_file, OutputStream output_file, org.gudy.azureus2.plugins.logging.LoggerChannel log)


	
	
				
				
			
			 
	
		 
	
		readPatchEntries( patch_file );
		
		JarInputStream	jis = new JarInputStream(input_file);
		
		JarOutputStream	jos = new JarOutputStream(output_file);

		boolean 	manifest_found	= false;
		
		while( true ){
			
			JarEntry is_entry = jis.getNextJarEntry();
			
			if ( is_entry == null ){
				
				break;
			}
			
			if ( is_entry.isDirectory()){
				
				continue;
			}
			
			String	name = is_entry.getName();
			
			if ( name.equalsIgnoreCase( MANIFEST_NAME )){
				
				manifest_found	= true;
			}
			
			InputStream	eis = getPatch( name);
			
			if ( eis != null ){
				
				log.log( "patch - replace: " + name );
				
			}else{
				
				eis = jis;
			}
			
			JarEntry os_entry = new JarEntry(name);
			
			writeEntry( jos, os_entry, eis );
		}
		
			// write any new entries
		
		Iterator	it = patch_entries.keySet().iterator();
		
		while( it.hasNext()){
			
			String	name = (String)it.next();
			
			if ( name.equalsIgnoreCase( MANIFEST_NAME )){
				
				manifest_found = true;
			}
		
			log.log( "patch - add: " + name);
		
			InputStream	eis = (InputStream)patch_entries.get(name);
			
			JarEntry os_entry = new JarEntry(name);
			
			writeEntry( jos, os_entry, eis );			
		}
		
		if ( !manifest_found ){
			
			JarEntry entry = new JarEntry( MANIFEST_NAME );
		
			ByteArrayInputStream bais = new ByteArrayInputStream("Manifest-Version: 1.0\r\n\r\n".getBytes());
		
			writeEntry( jos, entry, bais );
		}
		
		jos.finish();	// FLUSH is not sufficient!!!!
	
Methods Summary
public java.io.InputStreamgetPatch(java.lang.String name)

		return((InputStream)patch_entries.remove(name));
	
protected voidreadPatchEntries(java.io.InputStream is)

		JarInputStream	jis = new JarInputStream(is );
			
		while( true ){
			
			JarEntry ent = jis.getNextJarEntry();
			
			if ( ent == null ){
				
				break;
			}
			
			if ( ent.isDirectory()){
				
				continue;
			}
			
			ByteArrayOutputStream	baos = new ByteArrayOutputStream();
				
			byte[]	buffer = new byte[8192];
				
			while( true ){
					
				int	l = jis.read( buffer );
					
				if ( l <= 0 ){
						
					break;
				}
					
				baos.write( buffer, 0, l );
			}
				
			patch_entries.put( ent.getName(), new ByteArrayInputStream( baos.toByteArray()));
		}
	
private static voidwriteEntry(java.util.jar.JarOutputStream jos, java.util.jar.JarEntry entry, java.io.InputStream data)

		jos.putNextEntry(entry);

		byte[]	newBytes = new byte[4096];
		
		int size = data.read(newBytes);

		while (size != -1){
			
			jos.write(newBytes, 0, size);
			
			size = data.read(newBytes);
		}