Skip to content
Snippets Groups Projects
Commit 3924a1b2 authored by tpylak's avatar tpylak
Browse files

LMS-820 move checksum calculator from bds to common, add possibility to calculate md5 from String

SVN: 10612
parent 8ca3f71c
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
<classpathentry kind="lib" path="/libraries/testng/testng-jdk15.jar" sourcepath="/libraries/testng/src.zip"/> <classpathentry kind="lib" path="/libraries/testng/testng-jdk15.jar" sourcepath="/libraries/testng/src.zip"/>
<classpathentry kind="lib" path="/libraries/jmock/jmock.jar"/> <classpathentry kind="lib" path="/libraries/jmock/jmock.jar"/>
<classpathentry kind="lib" path="/libraries/log4j/log4j.jar" sourcepath="/libraries/log4j/src.zip"/> <classpathentry kind="lib" path="/libraries/log4j/log4j.jar" sourcepath="/libraries/log4j/src.zip"/>
<classpathentry kind="lib" path="/libraries/fast-md5/fast-md5.jar" sourcepath="/libraries/fast-md5/src.zip"/>
<classpathentry kind="lib" path="/libraries/restrictionchecker/restrictions.jar"/> <classpathentry kind="lib" path="/libraries/restrictionchecker/restrictions.jar"/>
<classpathentry kind="lib" path="/libraries/commons-lang/commons-lang.jar" sourcepath="/libraries/commons-lang/src.zip"/> <classpathentry kind="lib" path="/libraries/commons-lang/commons-lang.jar" sourcepath="/libraries/commons-lang/src.zip"/>
<classpathentry kind="lib" path="/libraries/cglib/cglib-nodep.jar"/> <classpathentry kind="lib" path="/libraries/cglib/cglib-nodep.jar"/>
......
...@@ -41,6 +41,8 @@ import ch.systemsx.cisd.common.collections.IToStringConverter; ...@@ -41,6 +41,8 @@ import ch.systemsx.cisd.common.collections.IToStringConverter;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.utilities.IChecksumCalculator;
import ch.systemsx.cisd.common.utilities.MD5ChecksumCalculator;
/** /**
* A <code>IDataStructureHandler</code> implementation for the <code>md5sum</code> directory. * A <code>IDataStructureHandler</code> implementation for the <code>md5sum</code> directory.
......
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
<classpathentry kind="lib" path="/libraries/unix"/> <classpathentry kind="lib" path="/libraries/unix"/>
<classpathentry kind="lib" path="/libraries/cisd-base/cisd-base-test.jar"/> <classpathentry kind="lib" path="/libraries/cisd-base/cisd-base-test.jar"/>
<classpathentry kind="lib" path="/libraries/cisd-base/cisd-base.jar" sourcepath="/libraries/cisd-base/cisd-base-src.zip"/> <classpathentry kind="lib" path="/libraries/cisd-base/cisd-base.jar" sourcepath="/libraries/cisd-base/cisd-base-src.zip"/>
<classpathentry kind="lib" path="/libraries/fast-md5/fast-md5.jar" sourcepath="/libraries/fast-md5/src.zip"/>
<classpathentry kind="output" path="targets/classes"/> <classpathentry kind="output" path="targets/classes"/>
</classpath> </classpath>
...@@ -14,18 +14,18 @@ ...@@ -14,18 +14,18 @@
* limitations under the License. * limitations under the License.
*/ */
package ch.systemsx.cisd.bds.handler; package ch.systemsx.cisd.common.utilities;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** /**
* Implementations know how to compute a <a href="http://en.wikipedia.org/wiki/Checksum">checksum</a> * Implementations know how to compute a <a
* from a file. * href="http://en.wikipedia.org/wiki/Checksum">checksum</a> from a file.
* *
* @author Christian Ribeaud * @author Christian Ribeaud
*/ */
interface IChecksumCalculator public interface IChecksumCalculator
{ {
/** /**
......
...@@ -14,10 +14,11 @@ ...@@ -14,10 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
package ch.systemsx.cisd.bds.handler; package ch.systemsx.cisd.common.utilities;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringReader;
import com.twmacinta.util.MD5; import com.twmacinta.util.MD5;
import com.twmacinta.util.MD5InputStream; import com.twmacinta.util.MD5InputStream;
...@@ -27,20 +28,44 @@ import com.twmacinta.util.MD5InputStream; ...@@ -27,20 +28,44 @@ import com.twmacinta.util.MD5InputStream;
* *
* @author Christian Ribeaud * @author Christian Ribeaud
*/ */
final class MD5ChecksumCalculator implements IChecksumCalculator public final class MD5ChecksumCalculator implements IChecksumCalculator
{ {
//
// IChecksum
//
public String calculateChecksum(InputStream inputStream) throws IOException public String calculateChecksum(InputStream inputStream) throws IOException
{ {
byte[] buf = new byte[4096]; return calculate(inputStream, 4096);
}
private static String calculate(InputStream inputStream, int bufferSize) throws IOException
{
byte[] buf = new byte[bufferSize];
MD5InputStream in = new MD5InputStream(inputStream); MD5InputStream in = new MD5InputStream(inputStream);
while (in.read(buf) != -1) while (in.read(buf) != -1)
{ {
} }
return MD5.asHex(in.hash()); return MD5.asHex(in.hash());
} }
/** Calculates a checksum for a given String */
public static String calculate(String value)
{
assert value != null && value.length() > 0 : "Value cannot be blank.";
final StringReader reader = new StringReader(value);
InputStream inputStream = new InputStream()
{
@Override
public int read() throws IOException
{
return reader.read();
}
};
try
{
return calculate(inputStream, value.length());
} catch (IOException ex)
{
throw new IllegalStateException("This should not happen: " + ex.getMessage());
}
}
} }
\ No newline at end of file
/*
* Copyright 2009 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.utilities;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
/**
* Tests for {@link MD5ChecksumCalculator}.
*
* @author Tomasz Pylak
*/
public class MD5ChecksumCalculatorTest
{
@Test
public void testCalculateForString()
{
String md5 = MD5ChecksumCalculator.calculate("Dataset_200903031234987-321");
AssertJUnit.assertEquals("9b0351271044c1a843d51811984968cd", md5);
md5 = MD5ChecksumCalculator.calculate("x");
AssertJUnit.assertEquals("9dd4e461268c8034f5c8564e155c67a6", md5);
}
@Test(expectedExceptions = AssertionError.class)
public void testCalculateForEmptyString()
{
MD5ChecksumCalculator.calculate("");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment