Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 641a30b15c4878b6d4127e23dadf7a163bdd3fa1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*******************************************************************************
 * Copyright (c) 2007 compeople AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  	compeople AG (Stefan Liebig) - initial API and implementation
 * 	IBM Corporation - ongoing development
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.artifact.optimizers.jbdiff;

import ie.wombat.jbdiff.JBDiff;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils;
import org.eclipse.equinox.internal.p2.sar.DirectByteArrayOutputStream;
import org.eclipse.equinox.internal.p2.sar.SarUtil;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.ArtifactDescriptor;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository;

public class JBDiffZipStep extends JBDiffStep {

	public JBDiffZipStep() {
		super();
	}

	// TODO We need a different way of injecting the base artifacts.  This approach forces
	// the target and base to live in the same repo.  Typical but not really required.
	protected JBDiffZipStep(IArtifactRepository repository) {
		super(repository);
	}

	protected void performProcessing() throws IOException {
		DirectByteArrayOutputStream sarredCurrent = new DirectByteArrayOutputStream();
		SarUtil.zipToSar(((DirectByteArrayOutputStream) incomingStream).getInputStream(), sarredCurrent);
		incomingStream = null;
		DirectByteArrayOutputStream predecessor = fetchPredecessorBytes(new ArtifactDescriptor(key));
		byte[] diff = JBDiff.bsdiff(predecessor.getBuffer(), predecessor.getBufferLength(), sarredCurrent.getBuffer(), sarredCurrent.getBufferLength());
		// free up the memory as soon as possible.
		predecessor = null;
		incomingStream = null;
		sarredCurrent = null;

		// copy the result of the optimization to the destination.
		FileUtils.copyStream(new ByteArrayInputStream(diff), true, destination, false);
	}

	private DirectByteArrayOutputStream fetchPredecessorBytes(ArtifactDescriptor artifactDescriptor) throws IOException {
		DirectByteArrayOutputStream zippedPredecessor = new DirectByteArrayOutputStream();

		status = repository.getArtifact(artifactDescriptor, zippedPredecessor, monitor);
		if (!status.isOK())
			throw (IOException) new IOException(status.getMessage()).initCause(status.getException());

		DirectByteArrayOutputStream result = new DirectByteArrayOutputStream();
		SarUtil.zipToSar(zippedPredecessor.getInputStream(), result);
		return result;
	}

}

Back to the top