Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMykola Nikishov2016-03-20 11:05:14 +0000
committerPascal Rapicault2016-04-21 03:24:22 +0000
commit5b2f061cd8ecf96a37783657e7ee7a0110c9d26d (patch)
tree01b67b9966c0537612720540982be69f4632b408
parentb1b3edccf441e5b5edf90e4c9cc08acf6d197f92 (diff)
downloadrt.equinox.p2-5b2f061cd8ecf96a37783657e7ee7a0110c9d26d.tar.gz
rt.equinox.p2-5b2f061cd8ecf96a37783657e7ee7a0110c9d26d.tar.xz
rt.equinox.p2-5b2f061cd8ecf96a37783657e7ee7a0110c9d26d.zip
Bug 490028 - Extract MD5 checksum calculation into a separate class
MD5 checksum is calculated in (almost) the same way in two places: org.eclipse.equinox.p2.internal.repository.tools.RepositoryUtilities and org.eclipse.equinox.spi.p2.publisher.PublisherHelper. The latter uses 4K byte buffer instead of byte-by-byte update which makes MD5 calculation for a 300k jar file 5x faster, see bug #405716 for more details. Due to the duplication, this has been fixed in PublisherHelper only but not in RepositoryUtilities. Eliminate code duplication by extracting MD5 checksum calculation into a separate class, ChecksumProducer. To make it consimable by org.eclipse.equinox.p2.publisher and org.eclipse.equinox.p2.repository.tools bundles but not exposing too much internals, move it to the x-friend'ed org.eclipse.equinox.internal.p2.repository.helpers package of org.eclipse.equinox.p2.repository bundle. Change-Id: Ic8b45a47700f35af1fb69f534dec7b32a2f7714a Signed-off-by: Mykola Nikishov <mn@mn.com.ua>
-rw-r--r--bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF1
-rw-r--r--bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java52
-rw-r--r--bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/RepositoryUtilities.java44
-rw-r--r--bundles/org.eclipse.equinox.p2.repository/META-INF/MANIFEST.MF4
-rw-r--r--bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/ChecksumProducer.java61
5 files changed, 76 insertions, 86 deletions
diff --git a/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF
index 7de84a73f..521ac95cc 100644
--- a/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF
@@ -36,6 +36,7 @@ Import-Package: org.eclipse.equinox.app;version="1.0.0";resolution:=optional,
org.eclipse.equinox.internal.p2.metadata.expression,
org.eclipse.equinox.internal.p2.metadata.index,
org.eclipse.equinox.internal.p2.metadata.repository,
+ org.eclipse.equinox.internal.p2.repository.helpers,
org.eclipse.equinox.p2.core;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.metadata;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.metadata.expression;version="[2.0.0,3.0.0)",
diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java
index c7da22884..96d9ff973 100644
--- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java
+++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2011 IBM Corporation and others.
+ * Copyright (c) 2007, 2016 IBM Corporation 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
@@ -9,16 +9,16 @@
* IBM Corporation - initial API and implementation
* Genuitec, LLC - added license support
* Code 9 - Ongoing development
-# SAP AG - consolidation of publishers for PDE formats
+ * SAP AG - consolidation of publishers for PDE formats
+ * Mykola Nikishov - extract MD5 checksum calculation
*******************************************************************************/
package org.eclipse.equinox.spi.p2.publisher;
-import java.io.*;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
+import java.io.File;
import java.util.*;
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
import org.eclipse.equinox.internal.p2.metadata.BasicVersion;
+import org.eclipse.equinox.internal.p2.repository.helpers.ChecksumProducer;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription;
@@ -108,7 +108,7 @@ public class PublisherHelper {
descriptor.setProperty(IArtifactDescriptor.DOWNLOAD_SIZE, Long.toString(pathOnDisk.length()));
}
if (info == null || (info.getArtifactOptions() & IPublisherInfo.A_NO_MD5) == 0) {
- String md5 = computeMD5(pathOnDisk);
+ String md5 = ChecksumProducer.computeMD5(pathOnDisk);
if (md5 != null)
descriptor.setProperty(IArtifactDescriptor.DOWNLOAD_MD5, md5);
}
@@ -116,46 +116,6 @@ public class PublisherHelper {
return result;
}
- private static String computeMD5(File file) {
- if (file == null || file.isDirectory() || !file.exists())
- return null;
- MessageDigest md5Checker;
- try {
- md5Checker = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
- } catch (NoSuchAlgorithmException e) {
- return null;
- }
- InputStream fis = null;
- try {
- fis = new BufferedInputStream(new FileInputStream(file));
- int read = -1;
- final int bufferSize = 4 * 1024;
- byte[] buffer = new byte[bufferSize];
- while ((read = fis.read(buffer, 0, bufferSize)) != -1) {
- md5Checker.update(buffer, 0, read);
- }
- byte[] digest = md5Checker.digest();
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < digest.length; i++) {
- if ((digest[i] & 0xFF) < 0x10)
- buf.append('0');
- buf.append(Integer.toHexString(digest[i] & 0xFF));
- }
- return buf.toString();
- } catch (FileNotFoundException e) {
- return null;
- } catch (IOException e) {
- return null;
- } finally {
- if (fis != null)
- try {
- fis.close();
- } catch (IOException e) {
- // ignore
- }
- }
- }
-
public static IProvidedCapability makeTranslationCapability(String hostId, Locale locale) {
return MetadataFactory.createProvidedCapability(NAMESPACE_IU_LOCALIZATION, locale.toString(), Version.createOSGi(1, 0, 0));
}
diff --git a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/RepositoryUtilities.java b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/RepositoryUtilities.java
index 11e3e32c6..01d89a2ca 100644
--- a/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/RepositoryUtilities.java
+++ b/bundles/org.eclipse.equinox.p2.repository.tools/src/org/eclipse/equinox/p2/internal/repository/tools/RepositoryUtilities.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009 IBM Corporation and others.
+ * Copyright (c) 2009, 2016 IBM Corporation 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
@@ -7,51 +7,17 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Mykola Nikishov - extract MD5 checksum calculation
*******************************************************************************/
package org.eclipse.equinox.p2.internal.repository.tools;
-import java.io.*;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
+import java.io.File;
+import org.eclipse.equinox.internal.p2.repository.helpers.ChecksumProducer;
public class RepositoryUtilities {
public static String computeMD5(File file) {
- if (file == null || file.isDirectory() || !file.exists())
- return null;
- MessageDigest md5Checker;
- try {
- md5Checker = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
- } catch (NoSuchAlgorithmException e) {
- return null;
- }
- InputStream fis = null;
- try {
- fis = new BufferedInputStream(new FileInputStream(file));
- int read = -1;
- while ((read = fis.read()) != -1) {
- md5Checker.update((byte) read);
- }
- byte[] digest = md5Checker.digest();
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < digest.length; i++) {
- if ((digest[i] & 0xFF) < 0x10)
- buf.append('0');
- buf.append(Integer.toHexString(digest[i] & 0xFF));
- }
- return buf.toString();
- } catch (FileNotFoundException e) {
- return null;
- } catch (IOException e) {
- return null;
- } finally {
- if (fis != null)
- try {
- fis.close();
- } catch (IOException e) {
- // ignore
- }
- }
+ return ChecksumProducer.computeMD5(file);
}
}
diff --git a/bundles/org.eclipse.equinox.p2.repository/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.repository/META-INF/MANIFEST.MF
index 8abfa281f..b513e4936 100644
--- a/bundles/org.eclipse.equinox.p2.repository/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.p2.repository/META-INF/MANIFEST.MF
@@ -19,12 +19,14 @@ Export-Package: org.eclipse.equinox.internal.p2.persistence;
org.eclipse.equinox.p2.repository.tools,
org.eclipse.equinox.p2.transport.ecf,
org.eclipse.equinox.p2.engine,
- org.eclipse.equinox.p2.discovery.compatibility",
+ org.eclipse.equinox.p2.discovery.compatibility,
+ org.eclipse.equinox.p2.publisher",
org.eclipse.equinox.internal.p2.repository.helpers;
x-friends:="org.eclipse.equinox.p2.artifact.repository,
org.eclipse.equinox.p2.exemplarysetup,
org.eclipse.equinox.p2.metadata.repository,
org.eclipse.equinox.p2.operations,
+ org.eclipse.equinox.p2.publisher,
org.eclipse.equinox.p2.repository.tools,
org.eclipse.equinox.p2.ui,
org.eclipse.equinox.p2.updatesite",
diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/ChecksumProducer.java b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/ChecksumProducer.java
new file mode 100644
index 000000000..a9c1c5a9f
--- /dev/null
+++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/ChecksumProducer.java
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2016 IBM Corporation 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:
+ * IBM Corporation - initial API and implementation
+ * Mykola Nikishov - extract MD5 checksum calculation
+ *******************************************************************************/
+
+package org.eclipse.equinox.internal.p2.repository.helpers;
+
+import java.io.*;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class ChecksumProducer {
+
+ public static String computeMD5(File file) {
+ if (file == null || file.isDirectory() || !file.exists())
+ return null;
+ MessageDigest md5Checker;
+ try {
+ md5Checker = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
+ } catch (NoSuchAlgorithmException e) {
+ return null;
+ }
+ InputStream fis = null;
+ try {
+ fis = new BufferedInputStream(new FileInputStream(file));
+ int read = -1;
+ final int bufferSize = 4 * 1024;
+ byte[] buffer = new byte[bufferSize];
+ while ((read = fis.read(buffer, 0, bufferSize)) != -1) {
+ md5Checker.update(buffer, 0, read);
+ }
+ byte[] digest = md5Checker.digest();
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < digest.length; i++) {
+ if ((digest[i] & 0xFF) < 0x10)
+ buf.append('0');
+ buf.append(Integer.toHexString(digest[i] & 0xFF));
+ }
+ return buf.toString();
+ } catch (FileNotFoundException e) {
+ return null;
+ } catch (IOException e) {
+ return null;
+ } finally {
+ if (fis != null)
+ try {
+ fis.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+
+}

Back to the top