From 5b2f061cd8ecf96a37783657e7ee7a0110c9d26d Mon Sep 17 00:00:00 2001 From: Mykola Nikishov Date: Sun, 20 Mar 2016 13:05:14 +0200 Subject: 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 --- .../META-INF/MANIFEST.MF | 1 + .../equinox/spi/p2/publisher/PublisherHelper.java | 52 +++--------------- .../repository/tools/RepositoryUtilities.java | 44 ++-------------- .../META-INF/MANIFEST.MF | 4 +- .../p2/repository/helpers/ChecksumProducer.java | 61 ++++++++++++++++++++++ 5 files changed, 76 insertions(+), 86 deletions(-) create mode 100644 bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/ChecksumProducer.java 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 + } + } + } + +} -- cgit v1.2.3